.github/workflows/release.yml

name: Release

# Every merge to main publishes a release. The version is package.json's when
# that has not been released yet (bump it in a PR for a minor or major
# release); otherwise the patch after the newest v* tag. The workflow tags the
# merged commit itself, because main only accepts changes through PRs.
# A pushed v* tag still releases that exact version, and a manual run only
# builds the installer as an artifact.

on:
  push:
    branches: [main]
    tags: ["v*"]
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: release
  cancel-in-progress: false

jobs:
  version:
    name: Pick the version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.pick.outputs.version }}
      publish: ${{ steps.pick.outputs.publish }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Pick the version
        id: pick
        shell: bash
        run: |
          declared=$(node -p "require('./package.json').version")
          if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
            version="${GITHUB_REF_NAME#v}"
            if [[ "$version" != "$declared" ]]; then
              echo "::error::Tag $GITHUB_REF_NAME does not match package.json version $declared"
              exit 1
            fi
            publish=true
          elif [[ "$GITHUB_REF" == refs/heads/main ]]; then
            tagged=$(git tag --points-at HEAD --list 'v*' | head -n 1)
            latest=$(git tag --list 'v*' --sort=-v:refname | head -n 1)
            # Use package.json when it is ahead of every release, else the next patch.
            version=$(node -e '
              const parse = (v) => (v || "0.0.0").replace(/^v/, "").split(".").map(Number);
              const declared = parse(process.argv[1]);
              const latest = parse(process.argv[2]);
              const ahead = (declared[0] - latest[0] || declared[1] - latest[1] || declared[2] - latest[2]) > 0;
              console.log((ahead ? declared : [latest[0], latest[1], latest[2] + 1]).join("."));
            ' "$declared" "$latest")
            publish=true
            if [[ -n "$tagged" ]]; then
              # A pushed tag already releases this commit; don't publish it twice.
              version="${tagged#v}"
              publish=false
            fi
          else
            version="$declared"
            publish=false
          fi
          echo "Version $version (package.json $declared, publish $publish)"
          echo "version=$version" >> "$GITHUB_OUTPUT"
          echo "publish=$publish" >> "$GITHUB_OUTPUT"

  windows:
    name: Windows installer
    needs: version
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Run the tracker tests
        run: npm test

      - name: Stamp the version
        shell: pwsh
        run: npm version "${{ needs.version.outputs.version }}" --no-git-tag-version --allow-same-version

      - name: Build the installer
        run: npm run dist
        env:
          CSC_IDENTITY_AUTO_DISCOVERY: "false"

      - name: Upload the build
        uses: actions/upload-artifact@v4
        with:
          name: tally-setup
          path: |
            dist/Tally-Setup-*.exe
            dist/Tally-Setup-*.exe.blockmap
            dist/latest.yml
          if-no-files-found: error

      - name: Publish the release
        if: needs.version.outputs.publish == 'true'
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.version.outputs.version }}
          target_commitish: ${{ github.sha }}
          # latest.yml and the blockmap are what installed copies read to update themselves.
          files: |
            dist/Tally-Setup-*.exe
            dist/Tally-Setup-*.exe.blockmap
            dist/latest.yml
          fail_on_unmatched_files: true
          generate_release_notes: true