name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip

    steps:
      - uses: actions/checkout@v7

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          profile: minimal

      - uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --target ${{ matrix.target }}

      - name: Prepare archive
        shell: bash
        run: |
          BINARY="target/${{ matrix.target }}/release/react-auditor${{ matrix.target == 'x86_64-pc-windows-msvc' && '.exe' || '' }}"
          ARCHIVE_NAME="react-auditor-${{ matrix.target }}-${{ github.ref_name }}${{ matrix.archive == 'zip' && '.zip' || '.tar.gz' }}"
          if [ "${{ matrix.archive }}" = "zip" ]; then
            cp "$BINARY" react-auditor.exe
            7z a "$ARCHIVE_NAME" react-auditor.exe
          else
            cp "$BINARY" react-auditor
            tar czf "$ARCHIVE_NAME" react-auditor
          fi

      - uses: actions/upload-artifact@v7
        with:
          name: react-auditor-${{ matrix.target }}
          path: react-auditor-*

  create_release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v8
        with:
          pattern: react-auditor-*
          merge-multiple: true

      - uses: softprops/action-gh-release@v3
        with:
          files: react-auditor-*
