name: Release

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

permissions:
  contents: write

env:
  BIN_NAME: aven

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-15
            target: aarch64-apple-darwin
            artifact: aven-darwin-arm64
            use_cross: false
          - os: macos-15
            target: x86_64-apple-darwin
            artifact: aven-darwin-amd64
            use_cross: false
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: aven-linux-amd64
            use_cross: true
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact: aven-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
      - name: Smoke test packaged binary
        if: matrix.target != 'aarch64-unknown-linux-musl'
        run: |
          package_dir="package-${{ matrix.artifact }}"
          mkdir "$package_dir"
          tar -xzf ${{ matrix.artifact }}.tar.gz -C "$package_dir"
          "$package_dir/${BIN_NAME}" --help
      - 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: ${{ github.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
      - uses: actions/checkout@v5
        with:
          repository: raine/homebrew-aven
          token: ${{ secrets.RELEASE_TOKEN }}
          path: tap
      - name: Generate formula and push to tap
        env:
          VERSION: ${{ github.ref_name }}
        run: |
          VERSION="${VERSION#v}"
          SHA_MAC_ARM=$(awk '{print $1}' aven-darwin-arm64.sha256)
          SHA_MAC_INTEL=$(awk '{print $1}' aven-darwin-amd64.sha256)
          SHA_LINUX_ARM=$(awk '{print $1}' aven-linux-arm64.sha256)
          SHA_LINUX_INTEL=$(awk '{print $1}' aven-linux-amd64.sha256)

          cat > tap/Formula/aven.rb << EOF
          class Aven < Formula
            desc "Local-first task manager CLI and sync server"
            homepage "https://github.com/raine/aven"
            version "${VERSION}"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/raine/aven/releases/download/v${VERSION}/aven-darwin-arm64.tar.gz"
                sha256 "${SHA_MAC_ARM}"
              else
                url "https://github.com/raine/aven/releases/download/v${VERSION}/aven-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/aven/releases/download/v${VERSION}/aven-linux-arm64.tar.gz"
                sha256 "${SHA_LINUX_ARM}"
              else
                url "https://github.com/raine/aven/releases/download/v${VERSION}/aven-linux-amd64.tar.gz"
                sha256 "${SHA_LINUX_INTEL}"
              end
            end

            def install
              bin.install "aven"
            end

            test do
              assert_match "Local-first task manager", shell_output("#{bin}/aven --help")
            end
          end
          EOF
          sed -i 's/^          //' tap/Formula/aven.rb

          cd tap
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/aven.rb
          if git diff --cached --quiet; then
            echo "Formula is up to date"
          else
            git commit -m "aven ${VERSION}"
            git pull --rebase
            git push
          fi
