name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag to build (e.g. v3.0.0)"
        required: true
        type: string
      create_release:
        description: "Create a GitHub release"
        required: false
        default: false
        type: boolean

jobs:
  build-macos-aarch64:
    name: Build macOS ARM
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ inputs.tag || github.sha }}
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target aarch64-apple-darwin
      - uses: actions/upload-artifact@v7
        with:
          name: aarch64-apple-darwin
          path: target/aarch64-apple-darwin/release/petname
          if-no-files-found: error

  build-macos-x86_64:
    name: Build macOS Intel
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ inputs.tag || github.sha }}
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-apple-darwin
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target x86_64-apple-darwin
      - uses: actions/upload-artifact@v7
        with:
          name: x86_64-apple-darwin
          path: target/x86_64-apple-darwin/release/petname
          if-no-files-found: error

  build-linux-x86_64:
    name: Build Linux x86_64
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ inputs.tag || github.sha }}
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-musl
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target x86_64-unknown-linux-musl
      - uses: actions/upload-artifact@v7
        with:
          name: linux-x86_64
          path: target/x86_64-unknown-linux-musl/release/petname
          if-no-files-found: error

  build-linux-aarch64:
    name: Build Linux aarch64
    runs-on: ubuntu-22.04-arm
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ inputs.tag || github.sha }}
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-unknown-linux-musl
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target aarch64-unknown-linux-musl
      - uses: actions/upload-artifact@v7
        with:
          name: linux-aarch64
          path: target/aarch64-unknown-linux-musl/release/petname
          if-no-files-found: error

  create-release:
    name: Create release
    needs: [build-macos-aarch64, build-macos-x86_64, build-linux-x86_64, build-linux-aarch64]
    runs-on: ubuntu-latest
    if: github.event_name == 'push' || inputs.create_release
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v8
        with:
          name: aarch64-apple-darwin
          path: aarch64-apple-darwin
      - uses: actions/download-artifact@v8
        with:
          name: x86_64-apple-darwin
          path: x86_64-apple-darwin
      - uses: actions/download-artifact@v8
        with:
          name: linux-x86_64
          path: linux-x86_64
      - uses: actions/download-artifact@v8
        with:
          name: linux-aarch64
          path: linux-aarch64
      - name: Rename executables
        run: |
          mv -v aarch64-apple-darwin/petname petname-aarch64-apple-darwin
          mv -v x86_64-apple-darwin/petname petname-x86_64-apple-darwin
          mv -v linux-x86_64/petname petname-x86_64-unknown-linux-musl
          mv -v linux-aarch64/petname petname-aarch64-unknown-linux-musl
          find .
      - uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ inputs.tag || github.ref_name }}
          files: |
            petname-aarch64-apple-darwin
            petname-x86_64-apple-darwin
            petname-x86_64-unknown-linux-musl
            petname-aarch64-unknown-linux-musl
          fail_on_unmatched_files: true
