name: release-rust

# Trigger: any "v*" tag created by `npm run release` (standard-version).
# The tag push is the canonical signal that a release has been cut.
on:
  push:
    tags:
      - "v*"

jobs:
  build-windows:
    name: Build & publish capslockx-windows (x86_64-pc-windows-msvc)
    runs-on: windows-latest
    if: github.repository == 'snolab/CapsLockX'

    permissions:
      contents: write   # create release + upload assets + push binaries

    steps:
      - uses: actions/checkout@v4
        with:
          ref: main   # check out main so we can push binaries back

      # Stable MSVC toolchain – link.exe and the Windows SDK are already
      # present on windows-latest; no cross-compilation needed.
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc

      - name: Cache Rust build artefacts
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: rs -> rs/target

      # [profile.release] in rs/Cargo.toml: opt-level=3, lto=true, codegen-units=1
      - name: Build all Windows binaries --release
        working-directory: rs
        run: cargo build -p capslockx-windows -p clx-screen-reader --release

      # Copy binaries to repo root (as users would see them after unzip).
      - name: Stage release binaries
        shell: pwsh
        run: |
          Copy-Item rs\target\release\capslockx.exe capslockx-windows-x86_64.exe
          Copy-Item rs\target\release\clx.exe       CLX.exe
          Copy-Item rs\target\release\clx-screen-reader.exe clx-screen-reader.exe

      # Commit built binaries to main so zip-downloaders get them.
      - name: Commit binaries to main
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add CLX.exe clx-screen-reader.exe
          git diff --cached --quiet || git commit -m "chore: update prebuilt binaries for ${{ github.ref_name }}"
          git push origin main

      # Create (or update) the GitHub Release for this tag and attach binaries.
      - name: Create GitHub Release and upload assets
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          generate_release_notes: true
          fail_on_unmatched_files: true
          files: |
            capslockx-windows-x86_64.exe
            CLX.exe
            clx-screen-reader.exe
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
