name: Release

on:
  push:
    tags: ['v*']

permissions:
  contents: write

env:
  BIN_NAME: sideagent

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-15
            target: aarch64-apple-darwin
            artifact: sideagent-darwin-arm64
            use_cross: false
          - os: macos-15
            target: x86_64-apple-darwin
            artifact: sideagent-darwin-amd64
            use_cross: false
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: sideagent-linux-amd64
            use_cross: true
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact: sideagent-linux-arm64
            use_cross: true
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@cross
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - name: Build
        run: |
          if [ "${{ matrix.use_cross }}" == "true" ]; then
            cross build --release --locked --target ${{ matrix.target }}
          else
            cargo build --release --locked --target ${{ matrix.target }}
          fi
      - name: Package
        run: |
          bin="target/${{ matrix.target }}/release/${BIN_NAME}"
          tar -C "$(dirname "$bin")" -czf ${{ matrix.artifact }}.tar.gz ${BIN_NAME}
          shasum -a 256 ${{ matrix.artifact }}.tar.gz > ${{ matrix.artifact }}.sha256
      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.artifact }}
          path: |
            ${{ matrix.artifact }}.tar.gz
            ${{ matrix.artifact }}.sha256

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v8
        with:
          merge-multiple: true
      - uses: softprops/action-gh-release@v3
        with:
          token: ${{ secrets.RELEASE_TOKEN }}
          generate_release_notes: true
          files: |
            *.tar.gz
            *.sha256

  update-tap:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v8
        with:
          merge-multiple: true
      - name: Clone tap repository
        env:
          RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          git clone "https://x-access-token:${RELEASE_TOKEN}@github.com/raine/homebrew-sideagent.git" tap
          cd tap
          git switch main || git switch -c main
      - name: Generate formula and push to tap
        env:
          VERSION: ${{ github.ref_name }}
        run: |
          VERSION="${VERSION#v}"
          SHA_MAC_ARM=$(awk '{print $1}' sideagent-darwin-arm64.sha256)
          SHA_MAC_INTEL=$(awk '{print $1}' sideagent-darwin-amd64.sha256)
          SHA_LINUX_INTEL=$(awk '{print $1}' sideagent-linux-amd64.sha256)
          SHA_LINUX_ARM=$(awk '{print $1}' sideagent-linux-arm64.sha256)

          mkdir -p tap/Formula
          cat > tap/Formula/sideagent.rb << EOF
          class Sideagent < Formula
            desc "Launch coding agents in tmux panes and wait for completion"
            homepage "https://github.com/raine/sideagent"
            version "${VERSION}"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/raine/sideagent/releases/download/v${VERSION}/sideagent-darwin-arm64.tar.gz"
                sha256 "${SHA_MAC_ARM}"
              else
                url "https://github.com/raine/sideagent/releases/download/v${VERSION}/sideagent-darwin-amd64.tar.gz"
                sha256 "${SHA_MAC_INTEL}"
              end
            end

            on_linux do
              if Hardware::CPU.arm? && Hardware::CPU.is_64_bit?
                url "https://github.com/raine/sideagent/releases/download/v${VERSION}/sideagent-linux-arm64.tar.gz"
                sha256 "${SHA_LINUX_ARM}"
              else
                url "https://github.com/raine/sideagent/releases/download/v${VERSION}/sideagent-linux-amd64.tar.gz"
                sha256 "${SHA_LINUX_INTEL}"
              end
            end

            def install
              bin.install "sideagent"
            end

            test do
              assert_match version.to_s, shell_output("#{bin}/sideagent --version")
            end
          end
          EOF
          sed -i 's/^          //' tap/Formula/sideagent.rb

          cd tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/sideagent.rb
          git commit -m "sideagent ${VERSION}"
          if git ls-remote --exit-code --heads origin main; then
            git pull --rebase origin main
          fi
          git push origin main
