srz 0.3.0

Fast, modular CLI to summarize webpages, documents, books, and live news using local LLMs
name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: write
  actions: write

jobs:
  verify-version:
    name: Verify Tag & Cargo.toml Version Match
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Verify Git Tag matches Cargo.toml
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          CARGO_VER=$(cargo metadata --no-deps --format-version 1 | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
          TAG_VER="${GITHUB_REF_NAME#v}"
          if [ "$CARGO_VER" != "$TAG_VER" ]; then
            echo "::error::Tag version ($TAG_VER) does not match Cargo.toml version ($CARGO_VER)!"
            exit 1
          fi
          echo "Verified: Git tag ($TAG_VER) matches Cargo.toml version ($CARGO_VER)"

  build-release:
    name: Build release (${{ matrix.target }})
    needs: verify-version
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Set up sccache
        uses: mozilla/sccache-action@v0.0.8

      - name: Configure sccache
        shell: bash
        run: |
          echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV
          echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          targets: ${{ matrix.target }}

      - name: Install Linux build dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev build-essential cmake

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

      - name: Show sccache stats
        if: always()
        run: sccache --show-stats

      - name: Package asset (Unix)
        if: matrix.archive == 'tar.gz'
        run: |
          CARGO_VER=$(cargo metadata --no-deps --format-version 1 | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
          VERSION="${GITHUB_REF_NAME:-v${CARGO_VER}}"
          ARCHIVE_VERSION="srz-${VERSION}-${{ matrix.target }}.tar.gz"
          ARCHIVE_LATEST="srz-${{ matrix.target }}.tar.gz"
          cd target/${{ matrix.target }}/release
          tar -czf "../../../${ARCHIVE_VERSION}" srz
          cp "../../../${ARCHIVE_VERSION}" "../../../${ARCHIVE_LATEST}"
          cd ../../..
          shasum -a 256 "${ARCHIVE_VERSION}" > "${ARCHIVE_VERSION}.sha256" 2>/dev/null || sha256sum "${ARCHIVE_VERSION}" > "${ARCHIVE_VERSION}.sha256"
          shasum -a 256 "${ARCHIVE_LATEST}" > "${ARCHIVE_LATEST}.sha256" 2>/dev/null || sha256sum "${ARCHIVE_LATEST}" > "${ARCHIVE_LATEST}.sha256"

      - name: Package asset (Windows)
        if: matrix.archive == 'zip'
        shell: bash
        run: |
          CARGO_VER=$(cargo metadata --no-deps --format-version 1 | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
          VERSION="${GITHUB_REF_NAME:-v${CARGO_VER}}"
          ARCHIVE_VERSION="srz-${VERSION}-${{ matrix.target }}.zip"
          ARCHIVE_LATEST="srz-${{ matrix.target }}.zip"
          cd target/${{ matrix.target }}/release
          7z a "../../../${ARCHIVE_VERSION}" srz.exe
          cp "../../../${ARCHIVE_VERSION}" "../../../${ARCHIVE_LATEST}"
          cd ../../..
          shasum -a 256 "${ARCHIVE_VERSION}" > "${ARCHIVE_VERSION}.sha256" 2>/dev/null || sha256sum "${ARCHIVE_VERSION}" > "${ARCHIVE_VERSION}.sha256"
          shasum -a 256 "${ARCHIVE_LATEST}" > "${ARCHIVE_LATEST}.sha256" 2>/dev/null || sha256sum "${ARCHIVE_LATEST}" > "${ARCHIVE_LATEST}.sha256"

      - name: Upload Release Assets
        uses: softprops/action-gh-release@v2
        with:
          files: |
            srz-*.tar.gz
            srz-*.zip
            srz-*.sha256
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crates-io:
    name: Publish to Crates.io
    needs: verify-version
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install Linux build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev build-essential cmake

      - name: Publish to Crates.io
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
        env:
          CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

  bump-homebrew:
    name: Bump Homebrew Formula
    needs: build-release
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Check if HOMEBREW_TAP_TOKEN is configured
        id: check-token
        run: |
          if [ -z "${{ secrets.HOMEBREW_TAP_TOKEN }}" ]; then
            echo "skip=true" >> $GITHUB_OUTPUT
            echo "::warning::HOMEBREW_TAP_TOKEN secret is not set. Skipping Homebrew formula bump."
          else
            echo "skip=false" >> $GITHUB_OUTPUT
          fi

      - name: Update Homebrew formula
        if: steps.check-token.outputs.skip != 'true'
        uses: mislav/bump-homebrew-formula-action@v3
        with:
          formula-name: srz
          formula-path: Formula/srz.rb
          homebrew-tap: NewGuy012/homebrew-tap
          base-branch: main
          download-url: https://github.com/NewGuy012/srz/releases/download/${{ github.ref_name }}/srz-${{ github.ref_name }}-aarch64-apple-darwin.tar.gz
        env:
          COMMITTER_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}