name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build - ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: alcove-x86_64-unknown-linux-musl.tar.gz
            features: alcove-server
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: alcove-aarch64-apple-darwin.tar.gz
            features: full
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: alcove-x86_64-pc-windows-msvc.zip
            features: alcove-server

    steps:
      - uses: actions/checkout@v4

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

      - name: Cache cargo
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ matrix.target }}-cargo-

      - name: Install musl-tools (Linux)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get install -y musl-tools

      - name: Build
        env:
          POSTHOG_KEY: ${{ secrets.POSTHOG_KEY }}
          SENTRY_DSN_ALCOVE: ${{ secrets.SENTRY_DSN_ALCOVE }}
        run: cargo build --release --target ${{ matrix.target }} --no-default-features --features ${{ matrix.features }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          tar -czf ${{ matrix.artifact }} \
            -C target/${{ matrix.target }}/release alcove

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive `
            -Path target/${{ matrix.target }}/release/alcove.exe `
            -DestinationPath ${{ matrix.artifact }}

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

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Publish Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: |
            alcove-*.tar.gz
            alcove-*.zip

  publish-crates:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }} --allow-dirty

  update-homebrew:
    name: Update Homebrew Tap
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Compute SHA256 for macOS tarball
        id: sha
        run: |
          BASE="https://github.com/epicsagas/alcove/releases/download/${GITHUB_REF_NAME}"
          curl -sLf --retry 5 --retry-delay 10 "${BASE}/alcove-aarch64-apple-darwin.tar.gz" -o arm.tar.gz
          echo "arm=$(sha256sum arm.tar.gz | awk '{print $1}')" >> $GITHUB_OUTPUT

      - name: Checkout homebrew tap
        uses: actions/checkout@v4
        with:
          repository: epicsagas/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Update formula
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          sed -i "s/version \".*\"/version \"${VERSION}\"/" Formula/alcove.rb
          sed -i "0,/sha256 \".*\"/s//sha256 \"${{ steps.sha.outputs.arm }}\"/" Formula/alcove.rb

      - name: Commit and push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/alcove.rb
          git commit -m "alcove ${GITHUB_REF_NAME#v}"
          git push
