name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  test:
    name: test (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-24.04
          - windows-2022
          - macos-14
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
      - name: Run tests
        run: cargo test --all-targets --quiet

  package:
    name: package (${{ matrix.target }})
    needs: test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-2022
            target: x86_64-pc-windows-msvc
            archive_name: paceflow-x86_64-pc-windows-msvc
            binary_name: paceflow.exe
            archive_ext: zip
          - os: ubuntu-24.04
            target: x86_64-unknown-linux-gnu
            archive_name: paceflow-x86_64-unknown-linux-gnu
            binary_name: paceflow
            archive_ext: tar.gz
          - os: macos-14
            target: aarch64-apple-darwin
            archive_name: paceflow-aarch64-apple-darwin
            binary_name: paceflow
            archive_ext: tar.gz
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
      - name: Build release binary
        run: cargo build --release --bin paceflow
      - name: Package archive (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $archiveName = "${{ matrix.archive_name }}"
          $stageDir = Join-Path "dist" $archiveName
          New-Item -ItemType Directory -Force -Path $stageDir | Out-Null
          Copy-Item "target/release/${{ matrix.binary_name }}" $stageDir
          Copy-Item "packaging/INSTALL.md" $stageDir
          $archivePath = Join-Path "dist" "$archiveName.zip"
          Compress-Archive -Path "$stageDir/*" -DestinationPath $archivePath -Force
          $hash = (Get-FileHash $archivePath -Algorithm SHA256).Hash.ToLower()
          "$hash  $(Split-Path $archivePath -Leaf)" | Out-File "$archivePath.sha256" -Encoding ascii
      - name: Package archive (Unix)
        if: runner.os != 'Windows'
        shell: bash
        run: |
          set -euo pipefail
          archive_name="${{ matrix.archive_name }}"
          stage_dir="dist/${archive_name}"
          mkdir -p "${stage_dir}"
          cp "target/release/${{ matrix.binary_name }}" "${stage_dir}/"
          cp packaging/INSTALL.md "${stage_dir}/"
          tar -C dist -czf "dist/${archive_name}.tar.gz" "${archive_name}"
          shasum -a 256 "dist/${archive_name}.tar.gz" > "dist/${archive_name}.tar.gz.sha256"
      - name: Upload packaged artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.archive_name }}
          path: |
            dist/*.${{ matrix.archive_ext }}
            dist/*.${{ matrix.archive_ext }}.sha256

  publish:
    name: publish release
    needs: package
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5
      - uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true
      - name: Publish GitHub release
        uses: softprops/action-gh-release@v2
        with:
          body_path: packaging/RELEASE_NOTES.md
          files: dist/*
