name: Release

on:
  release:
    types: [published]

permissions:
  contents: write

jobs:
  test:
    uses: ./.github/workflows/test.yml

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

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Strip binary
        if: runner.os != 'Windows'
        run: strip target/${{ matrix.target }}/release/stormdrain

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: cp target/${{ matrix.target }}/release/stormdrain stormdrain-${{ matrix.target }}

      - name: Package (Windows)
        if: runner.os == 'Windows'
        run: cp target/${{ matrix.target }}/release/stormdrain.exe stormdrain-${{ matrix.target }}.exe

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: stormdrain-${{ matrix.target }}*
