name: Release

permissions:
  contents: write

on:
  release:
    types:
      - published
env:
  PROGRAM: effy

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - filename: x86_64-linux.tar.gz
            target: x86_64-unknown-linux-gnu
            build_with: cross
          - filename: x86_64-windows.zip
            target: x86_64-pc-windows-msvc
            os: windows-latest
            build_with: cargo
          - filename: aarch64-linux.tar.gz
            target: aarch64-unknown-linux-gnu
            build_with: cross
          - filename: x86_64-apple-darwin.tar.gz
            target: x86_64-apple-darwin
            os: macos-latest
            build_with: cargo
          - filename: aarch64-apple-darwin.tar.gz
            target: aarch64-apple-darwin
            os: macos-latest
            build_with: cargo

    runs-on: ${{ matrix.os || 'ubuntu-latest' }}
    timeout-minutes: 60
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: "${{ matrix.target }}"
      - name: Install cross
        run: cargo install cross
        if: matrix.build_with == 'cross'

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

      - name: Package
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          case ${{ matrix.target }} in
          *-pc-windows-*)
            7z -y a ${{ env.PROGRAM }}-${{ matrix.filename }} ${{ env.PROGRAM }}.exe
            ;;
          *)
            tar czf ${{ env.PROGRAM }}-${{ matrix.filename }} ${{ env.PROGRAM }}
            ;;
          esac;

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: target/${{ matrix.target }}/release/${{ env.PROGRAM }}-*
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
