# Homebrew Update workflow
name: Homebrew Update
on:
  workflow_run:
    workflows: ["Release"]
    types: [completed]
concurrency:
  group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
  cancel-in-progress: true
permissions:
  contents: write
env:
  FORMULA_DEADLINE: "2027-12-31"
jobs:
  update-homebrew:
    name: Update Homebrew Formula and Cask
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
      - name: Get version
        id: version
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          TAG=$(gh api repos/${{ github.repository }}/releases/latest --jq '.tag_name')
          VERSION="${TAG#v}"
          echo "TAG=${TAG}" >> $GITHUB_OUTPUT
          echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
      - name: Download release archives
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release download "${{ steps.version.outputs.TAG }}" --repo "${{ github.repository }}" --pattern "grpctestify-linux-amd64.tar.gz" --clobber
          gh release download "${{ steps.version.outputs.TAG }}" --repo "${{ github.repository }}" --pattern "grpctestify-linux-arm64.tar.gz" --clobber
          gh release download "${{ steps.version.outputs.TAG }}" --repo "${{ github.repository }}" --pattern "grpctestify-macos-arm64.tar.gz" --clobber
          gh release download "${{ steps.version.outputs.TAG }}" --repo "${{ github.repository }}" --pattern "grpctestify-macos-amd64.tar.gz" --clobber
      - name: Calculate checksums
        id: checksum
        run: |
          LINUX_AMD64_CHECKSUM=$(sha256sum grpctestify-linux-amd64.tar.gz | awk '{print $1}')
          LINUX_ARM64_CHECKSUM=$(sha256sum grpctestify-linux-arm64.tar.gz | awk '{print $1}')
          MACOS_ARM64_CHECKSUM=$(sha256sum grpctestify-macos-arm64.tar.gz | awk '{print $1}')
          MACOS_AMD64_CHECKSUM=$(sha256sum grpctestify-macos-amd64.tar.gz | awk '{print $1}')
          echo "LINUX_AMD64_CHECKSUM=$LINUX_AMD64_CHECKSUM" >> $GITHUB_OUTPUT
          echo "LINUX_ARM64_CHECKSUM=$LINUX_ARM64_CHECKSUM" >> $GITHUB_OUTPUT
          echo "MACOS_ARM64_CHECKSUM=$MACOS_ARM64_CHECKSUM" >> $GITHUB_OUTPUT
          echo "MACOS_AMD64_CHECKSUM=$MACOS_AMD64_CHECKSUM" >> $GITHUB_OUTPUT
      - name: Clone tap repo
        run: |
          git clone https://${{ github.actor }}:${{ secrets.HOMEBREW_TOKEN }}@github.com/gripmock/homebrew-tap.git
          cd homebrew-tap
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
      - name: Update formula and cask
        run: |
          VERSION="${{ steps.version.outputs.VERSION }}"
          LINUX_AMD64_CHECKSUM="${{ steps.checksum.outputs.LINUX_AMD64_CHECKSUM }}"
          LINUX_ARM64_CHECKSUM="${{ steps.checksum.outputs.LINUX_ARM64_CHECKSUM }}"
          MACOS_ARM64_CHECKSUM="${{ steps.checksum.outputs.MACOS_ARM64_CHECKSUM }}"
          MACOS_AMD64_CHECKSUM="${{ steps.checksum.outputs.MACOS_AMD64_CHECKSUM }}"
          DEADLINE="${{ env.FORMULA_DEADLINE }}"

          cat > homebrew-tap/Formula/grpctestify.rb << EOF
          # typed: false
          # frozen_string_literal: true

          # This file was generated by GitHub Actions. DO NOT EDIT.
          class Grpctestify < Formula
            desc "gRPC testing utility written in Rust"
            homepage "https://github.com/gripmock/grpctestify-rust"
            version "${VERSION}"
            license "MIT"

            on_macos do
              if Hardware::CPU.intel?
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v${VERSION}/grpctestify-macos-amd64.tar.gz"
                sha256 "${MACOS_AMD64_CHECKSUM}"

                def install
                  bin.install "grpctestify"
                end
              end

              if Hardware::CPU.arm?
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v${VERSION}/grpctestify-macos-arm64.tar.gz"
                sha256 "${MACOS_ARM64_CHECKSUM}"

                def install
                  bin.install "grpctestify"
                end
              end
            end

            on_linux do
              if Hardware::CPU.intel? && Hardware::CPU.is_64_bit?
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v${VERSION}/grpctestify-linux-amd64.tar.gz"
                sha256 "${LINUX_AMD64_CHECKSUM}"

                def install
                  bin.install "grpctestify"
                end
              end

              if Hardware::CPU.arm? && Hardware::CPU.is_64_bit?
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v${VERSION}/grpctestify-linux-arm64.tar.gz"
                sha256 "${LINUX_ARM64_CHECKSUM}"

                def install
                  bin.install "grpctestify"
                end
              end
            end

            deprecate! date: "${DEADLINE}", because: "moved to cask. Please run `brew install --cask grpctestify`"

            def caveats
              <<~EOS
                The 'grpctestify' formula is deprecated and will be removed in future versions.
                Please migrate to the cask version:
                  brew uninstall grpctestify
                  brew install --cask grpctestify
              EOS
            end

            test do
              assert_match "#{version}", shell_output("#{bin}/grpctestify --version")
            end
          end
          EOF

          cat > homebrew-tap/Casks/grpctestify.rb << EOF
          # This file was generated by GitHub Actions. DO NOT EDIT.
          cask "grpctestify" do
            name "grpctestify"
            desc "gRPC testing utility written in Rust"
            homepage "https://github.com/gripmock/grpctestify-rust"
            version "${VERSION}"

            livecheck do
              skip "Auto-generated on release."
            end

            binary "grpctestify"

            on_macos do
              on_intel do
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v#{version}/grpctestify-macos-amd64.tar.gz",
                  verified: "github.com/gripmock/grpctestify-rust"
                sha256 "${MACOS_AMD64_CHECKSUM}"
              end

              on_arm do
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v#{version}/grpctestify-macos-arm64.tar.gz",
                  verified: "github.com/gripmock/grpctestify-rust"
                sha256 "${MACOS_ARM64_CHECKSUM}"
              end
            end

            on_linux do
              on_intel do
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v#{version}/grpctestify-linux-amd64.tar.gz",
                  verified: "github.com/gripmock/grpctestify-rust"
                sha256 "${LINUX_AMD64_CHECKSUM}"
              end

              on_arm do
                url "https://github.com/gripmock/grpctestify-rust/releases/download/v#{version}/grpctestify-linux-arm64.tar.gz",
                  verified: "github.com/gripmock/grpctestify-rust"
                sha256 "${LINUX_ARM64_CHECKSUM}"
              end
            end

            postflight do
              if OS.mac?
                system_command "/usr/bin/xattr", args: ["-dr", "com.apple.quarantine", "#{staged_path}/grpctestify"]
              end
            end

            # No zap stanza required
          end
          EOF
      - name: Commit tap updates
        run: |
          cd homebrew-tap
          git add Formula/grpctestify.rb Casks/grpctestify.rb
          if ! git diff --cached --quiet; then
            git commit -m "chore: update homebrew to ${{ steps.version.outputs.VERSION }}"
            git push origin master
          fi
