name: Release

on:
  release:
    types: [published]

permissions:
  contents: write

jobs:
  build:
    runs-on: ${{ matrix.runs-on }}

    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            runs-on: ubuntu-latest
            binary: notarai-x86_64-linux-gnu
          - target: x86_64-unknown-linux-musl
            runs-on: ubuntu-latest
            binary: notarai-x86_64-linux-musl
            musl: true
          - target: aarch64-unknown-linux-gnu
            runs-on: ubuntu-24.04-arm
            binary: notarai-aarch64-linux-gnu
          - target: aarch64-unknown-linux-musl
            runs-on: ubuntu-24.04-arm
            binary: notarai-aarch64-linux-musl
            musl: true
          - target: x86_64-apple-darwin
            runs-on: macos-14
            binary: notarai-x86_64-macos
          - target: aarch64-apple-darwin
            runs-on: macos-latest
            binary: notarai-aarch64-macos
          - target: x86_64-pc-windows-gnu
            runs-on: windows-latest
            binary: notarai-x86_64-windows.exe

    steps:
      - uses: actions/checkout@v6

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

      - name: Install musl tools
        if: matrix.musl
        run: sudo apt-get install -y musl-tools

      - uses: Swatinem/rust-cache@v2

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

      - name: Rename binary
        shell: bash
        run: |
          src="target/${{ matrix.target }}/release/notarai"
          if [[ "${{ matrix.target }}" == *"windows"* ]]; then
            src="${src}.exe"
          fi
          cp "$src" "${{ matrix.binary }}"

      - uses: softprops/action-gh-release@v2
        with:
          files: ${{ matrix.binary }}

  publish-crate:
    runs-on: ubuntu-latest
    needs: build

    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable

      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
