flexi 0.11.1

A minimal CLI tool for tracking your flexi-time balance
name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build
        run: |
          if [ "${{ matrix.cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi

      - name: Package
        run: |
          BINARY=target/${{ matrix.target }}/release/flexi
          ARCHIVE=flexi-${{ github.ref_name }}-${{ matrix.target }}.tar.gz
          tar -czf "$ARCHIVE" -C "$(dirname $BINARY)" "$(basename $BINARY)"
          echo "ARCHIVE=$ARCHIVE" >> $GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.target }}
          path: ${{ env.ARCHIVE }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          generate_release_notes: true

  update-homebrew:
    name: Update Homebrew Formula
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Compute SHA256s
        run: |
          VERSION="${{ github.ref_name }}"
          BASE="https://github.com/thombruce/flexi/releases/download/${VERSION}"
          echo "VERSION=${VERSION#v}" >> $GITHUB_ENV
          echo "SHA256_X86_MAC=$(curl -sL ${BASE}/flexi-${VERSION}-x86_64-apple-darwin.tar.gz | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
          echo "SHA256_ARM_MAC=$(curl -sL ${BASE}/flexi-${VERSION}-aarch64-apple-darwin.tar.gz | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
          echo "SHA256_X86_LINUX=$(curl -sL ${BASE}/flexi-${VERSION}-x86_64-unknown-linux-gnu.tar.gz | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
          echo "SHA256_ARM_LINUX=$(curl -sL ${BASE}/flexi-${VERSION}-aarch64-unknown-linux-gnu.tar.gz | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV

      - name: Clone homebrew-tap
        run: |
          git clone https://x-access-token:${{ secrets.HOMEBREW_TAP_TOKEN }}@github.com/thombruce/homebrew-tap.git

      - name: Update formula
        run: |
          cat > homebrew-tap/Formula/flexi.rb << EOF
          class Flexi < Formula
            desc "A minimal CLI tool for tracking your flexi-time balance"
            homepage "https://github.com/thombruce/flexi"
            version "${{ env.VERSION }}"
            license "MIT"

            on_macos do
              on_arm do
                url "https://github.com/thombruce/flexi/releases/download/v#{version}/flexi-v#{version}-aarch64-apple-darwin.tar.gz"
                sha256 "${{ env.SHA256_ARM_MAC }}"
              end
              on_intel do
                url "https://github.com/thombruce/flexi/releases/download/v#{version}/flexi-v#{version}-x86_64-apple-darwin.tar.gz"
                sha256 "${{ env.SHA256_X86_MAC }}"
              end
            end

            on_linux do
              on_arm do
                url "https://github.com/thombruce/flexi/releases/download/v#{version}/flexi-v#{version}-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "${{ env.SHA256_ARM_LINUX }}"
              end
              on_intel do
                url "https://github.com/thombruce/flexi/releases/download/v#{version}/flexi-v#{version}-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "${{ env.SHA256_X86_LINUX }}"
              end
            end

            def install
              bin.install "flexi"
            end

            test do
              assert_match "0 min", shell_output("#{bin}/flexi")
            end
          end
          EOF

      - name: Commit and push
        run: |
          cd homebrew-tap
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git config user.name "github-actions[bot]"
          git add Formula/flexi.rb
          git commit -m "chore: update flexi to v${{ env.VERSION }}"
          git push