name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build-and-release:
    name: Build and Release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: dupfinder
            asset_name: dupfinder-linux-x86_64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: dupfinder
            asset_name: dupfinder-linux-x86_64-musl
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: dupfinder
            asset_name: dupfinder-macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: dupfinder
            asset_name: dupfinder-macos-aarch64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: dupfinder.exe
            asset_name: dupfinder-windows-x86_64.exe

    steps:
      - uses: actions/checkout@v4
      
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      
      - name: Install musl-tools (Linux musl only)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get update && sudo apt-get install -y musl-tools
      
      - name: Build
        run: cargo build --release --target ${{ matrix.target }}
      
      - name: Strip binary (Unix)
        if: runner.os != 'Windows'
        run: strip target/${{ matrix.target }}/release/${{ matrix.artifact_name }} || true
      
      - name: Rename binary
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          if [ "${{ runner.os }}" = "Windows" ]; then
            mv ${{ matrix.artifact_name }} ${{ matrix.asset_name }}
          else
            mv ${{ matrix.artifact_name }} ${{ matrix.asset_name }}
          fi
      
      - name: Upload Release Asset
        uses: softprops/action-gh-release@v2
        with:
          files: target/${{ matrix.target }}/release/${{ matrix.asset_name }}
          draft: false
          prerelease: false
          # 只在第一个 job (Linux x86_64) 生成 release notes，避免重复
          generate_release_notes: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}

