name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  compile:
    name: Compile (${{ matrix.target }})
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            runner: macos-15
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-latest
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable
        with:
          targets: ${{ matrix.target }}

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

      - name: Test
        run: cargo test --target ${{ matrix.target }} --release

      - name: Package
        run: |
          mkdir fabro-${{ matrix.target }}
          cp target/${{ matrix.target }}/release/fabro fabro-${{ matrix.target }}/
          tar czf fabro-${{ matrix.target }}.tar.gz fabro-${{ matrix.target }}
          shasum -a 256 fabro-${{ matrix.target }}.tar.gz > fabro-${{ matrix.target }}.tar.gz.sha256

      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: artifacts-fabro-${{ matrix.target }}
          path: |
            fabro-${{ matrix.target }}.tar.gz
            fabro-${{ matrix.target }}.tar.gz.sha256

  release:
    name: Release
    needs: compile
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          path: target/distrib
          pattern: artifacts-fabro-*
          merge-multiple: true

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
          TAG_NAME: ${{ github.ref_name }}
        run: gh release create "$TAG_NAME" target/distrib/* --generate-notes
