name: Release

on:
  push:
    tags:
      - "v*.*.*"

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2.8.0
        with:
          key: ${{ matrix.target }}

      - name: Install musl tools
        if: contains(matrix.target, 'musl')
        run: |
          sudo apt-get update -q
          sudo apt-get install -y musl-tools
          if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-musl" ]]; then
            sudo apt-get install -y gcc-aarch64-linux-gnu
            echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
          fi

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

      - name: Package
        shell: bash
        run: |
          VERSION=${{ github.ref_name }}
          ARCHIVE="ecluse-${VERSION}-${{ matrix.target }}.tar.gz"
          tar czf "$ARCHIVE" -C "target/${{ matrix.target }}/release" ecluse
          echo "ARCHIVE=$ARCHIVE" >> $GITHUB_ENV

      - uses: actions/upload-artifact@v4
        with:
          name: ecluse-${{ github.ref_name }}-${{ matrix.target }}.tar.gz
          path: ecluse-${{ github.ref_name }}-${{ matrix.target }}.tar.gz

  release:
    needs: build
    runs-on: ubuntu-latest
    outputs:
      x86_64_mac_sha: ${{ steps.shas.outputs.x86_64_mac }}
      aarch64_mac_sha: ${{ steps.shas.outputs.aarch64_mac }}
      x86_64_linux_sha: ${{ steps.shas.outputs.x86_64_linux }}
      aarch64_linux_sha: ${{ steps.shas.outputs.aarch64_linux }}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          path: dist/

      - name: Compute SHAs
        id: shas
        run: |
          VERSION=${{ github.ref_name }}
          echo "x86_64_mac=$(shasum -a 256 dist/ecluse-${VERSION}-x86_64-apple-darwin.tar.gz | cut -d' ' -f1)" >> $GITHUB_OUTPUT
          echo "aarch64_mac=$(shasum -a 256 dist/ecluse-${VERSION}-aarch64-apple-darwin.tar.gz | cut -d' ' -f1)" >> $GITHUB_OUTPUT
          echo "x86_64_linux=$(shasum -a 256 dist/ecluse-${VERSION}-x86_64-unknown-linux-musl.tar.gz | cut -d' ' -f1)" >> $GITHUB_OUTPUT
          echo "aarch64_linux=$(shasum -a 256 dist/ecluse-${VERSION}-aarch64-unknown-linux-musl.tar.gz | cut -d' ' -f1)" >> $GITHUB_OUTPUT

      - name: Extract changelog section
        id: changelog
        run: |
          VERSION=${{ github.ref_name }}
          NOTES=$(awk "/^## \[${VERSION#v}\]/{found=1; next} found && /^## \[/{exit} found{print}" CHANGELOG.md)
          {
            echo "notes<<EOF"
            echo "$NOTES"
            echo "EOF"
          } >> $GITHUB_OUTPUT

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          files: "dist/*.tar.gz"
          body: ${{ steps.changelog.outputs.notes }}

  publish-crates:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2.8.0
      - run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }} || true

  update-homebrew:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout homebrew tap
        uses: actions/checkout@v4
        with:
          repository: hefgi/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-tap

      - name: Update formula
        env:
          VERSION: ${{ github.ref_name }}
          X86_64_MAC_SHA: ${{ needs.release.outputs.x86_64_mac_sha }}
          AARCH64_MAC_SHA: ${{ needs.release.outputs.aarch64_mac_sha }}
          X86_64_LINUX_SHA: ${{ needs.release.outputs.x86_64_linux_sha }}
          AARCH64_LINUX_SHA: ${{ needs.release.outputs.aarch64_linux_sha }}
        run: |
          mkdir -p homebrew-tap/Formula
          cat > homebrew-tap/Formula/ecluse.rb << FORMULA
          class Ecluse < Formula
            desc "Ephemeral local environments for coding agents — any stack."
            homepage "https://github.com/hefgi/ecluse"
            version "${VERSION#v}"
            license "Apache-2.0"

            on_macos do
              on_arm do
                url "https://github.com/hefgi/ecluse/releases/download/${VERSION}/ecluse-${VERSION}-aarch64-apple-darwin.tar.gz"
                sha256 "${AARCH64_MAC_SHA}"
              end
              on_intel do
                url "https://github.com/hefgi/ecluse/releases/download/${VERSION}/ecluse-${VERSION}-x86_64-apple-darwin.tar.gz"
                sha256 "${X86_64_MAC_SHA}"
              end
            end

            on_linux do
              on_arm do
                url "https://github.com/hefgi/ecluse/releases/download/${VERSION}/ecluse-${VERSION}-aarch64-unknown-linux-musl.tar.gz"
                sha256 "${AARCH64_LINUX_SHA}"
              end
              on_intel do
                url "https://github.com/hefgi/ecluse/releases/download/${VERSION}/ecluse-${VERSION}-x86_64-unknown-linux-musl.tar.gz"
                sha256 "${X86_64_LINUX_SHA}"
              end
            end

            def install
              bin.install "ecluse"
            end

            test do
              assert_match version.to_s, shell_output("#{bin}/ecluse --version")
            end
          end
          FORMULA

      - name: Commit and push formula
        run: |
          cd homebrew-tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/ecluse.rb
          git commit -m "ecluse ${VERSION#v}"
          git pull --rebase
          git push
