diff --git a/.github/workflows/ahk-format.yml b/.github/workflows/ahk-format.yml new file mode 100644 index 00000000..0a865827 --- /dev/null +++ b/.github/workflows/ahk-format.yml @@ -0,0 +1,48 @@ +name: AHK format + +on: + pull_request: + paths: + - "**.ahk" + - "Helper/Format-Ahk.ps1" + - "Helper/ahkfmt-exclude.list" + - "Helper/ahkfmt-tests/**" + - ".github/workflows/ahk-format.yml" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + format: + name: Check AHK formatting + runs-on: windows-latest + + steps: + - name: Check out pull request + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Run formatter tests + shell: powershell + run: ./Helper/Format-Ahk.ps1 -Test + + - name: Check changed AHK files + shell: powershell + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + run: | + # A changed formatter or exclude list can affect every file, so check them all. + $toolChanged = git diff --name-only "$env:BASE_SHA...HEAD" -- Helper/Format-Ahk.ps1 Helper/ahkfmt-exclude.list + if ($toolChanged) { + Write-Host 'Formatter changed: checking all tracked .ahk files.' + ./Helper/Format-Ahk.ps1 -Check + } + else { + ./Helper/Format-Ahk.ps1 -Check -Base $env:BASE_SHA + } + exit $LASTEXITCODE diff --git a/.github/workflows/rust-helpers.yml b/.github/workflows/rust-helpers.yml new file mode 100644 index 00000000..e2e973f2 --- /dev/null +++ b/.github/workflows/rust-helpers.yml @@ -0,0 +1,51 @@ +name: Rust helpers + +on: + pull_request: + paths: + - "Helper/carddb_src/**" + - "Helper/cardimage_src/**" + - ".github/workflows/rust-helpers.yml" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build: + name: Build ${{ matrix.crate }} + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + crate: [carddb, cardimage] + env: + CARGO_TARGET_DIR: ${{ github.workspace }}/target + + steps: + - name: Check out pull request + uses: actions/checkout@v6 + + - name: Install Rust stable + id: toolchain + uses: dtolnay/rust-toolchain@stable + with: + targets: x86_64-pc-windows-msvc + + - name: Restore Cargo cache + uses: actions/cache@v5 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + ${{ env.CARGO_TARGET_DIR }} + key: ${{ runner.os }}-${{ runner.arch }}-rust-pr-${{ matrix.crate }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles(format('Helper/{0}_src/Cargo.toml', matrix.crate)) }} + restore-keys: | + ${{ runner.os }}-${{ runner.arch }}-rust-pr-${{ matrix.crate }}-${{ steps.toolchain.outputs.cachekey }}- + + - name: Build ${{ matrix.crate }} + working-directory: Helper/${{ matrix.crate }}_src + run: cargo build --target x86_64-pc-windows-msvc diff --git a/Helper/Format-Ahk.ps1 b/Helper/Format-Ahk.ps1 index c129fbfe..5f66265c 100644 --- a/Helper/Format-Ahk.ps1 +++ b/Helper/Format-Ahk.ps1 @@ -30,6 +30,10 @@ Use the .ahk files staged for commit. With -Check, the staged content is checked (used by Helper\hooks\pre-commit). +.PARAMETER Base + Use the .ahk files changed since the branch left this git ref, for example + origin/main (used by .github/workflows/ahk-format.yml). + .PARAMETER Test Run the formatter's own tests in Helper\ahkfmt-tests. @@ -44,6 +48,7 @@ .\Helper\Format-Ahk.ps1 Scripts\1.ahk # format one file .\Helper\Format-Ahk.ps1 -Check # report files that need formatting .\Helper\Format-Ahk.ps1 -Staged # format the files staged for commit + .\Helper\Format-Ahk.ps1 -Check -Base origin/main # check the files this branch changed .NOTES Exit codes: 0 = ok, 1 = files need formatting (-Check) or tests failed, @@ -55,6 +60,7 @@ param( [string[]]$Path, [switch]$Check, [switch]$Staged, + [string]$Base, [switch]$Test, [switch]$Stdin ) @@ -65,6 +71,7 @@ $ErrorActionPreference = 'Stop' $RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path $ExcludeFile = Join-Path $PSScriptRoot 'ahkfmt-exclude.list' $TestDir = Join-Path $PSScriptRoot 'ahkfmt-tests' +$GitHubActions = $env:GITHUB_ACTIONS -eq 'true' $IndentUnit = ' ' # Analysis only works on the code part of a line: strings emptied, comment removed. @@ -483,6 +490,10 @@ function Get-TargetFiles { $names = Invoke-Git @('diff', '--cached', '--name-only', '--diff-filter=ACMR', '--', '*.ahk') return @($names | Where-Object { $_ -and -not (Test-Excluded $_ $excludes) }) } + if ($Base) { + $names = Invoke-Git @('diff', '--name-only', '--diff-filter=ACMR', "$Base...HEAD", '--', '*.ahk') + return @($names | Where-Object { $_ -and -not (Test-Excluded $_ $excludes) }) + } if (-not $Path) { $names = Invoke-Git @('ls-files', '--', '*.ahk') return @($names | Where-Object { $_ -and -not (Test-Excluded $_ $excludes) }) @@ -590,6 +601,7 @@ foreach ($relative in Get-TargetFiles) { if ($Check) { $needsFormat++ Write-Host "Needs formatting: $relative (line $line)" + if ($GitHubActions) { Write-Host "::error file=$relative,line=$line::Not formatted. Run .\Helper\Format-Ahk.ps1 $relative" } } else { [IO.File]::WriteAllBytes($fullPath, (Get-AhkBytes $file $lines)) @@ -600,12 +612,14 @@ foreach ($relative in Get-TargetFiles) { catch { $errors++ Write-Host "Skipped: ${relative}: $($_.Exception.Message)" + if ($GitHubActions) { Write-Host "::error file=$relative::Could not be formatted: $($_.Exception.Message)" } } } if ($Check -and $needsFormat) { $hint = '.\Helper\Format-Ahk.ps1' if ($Staged) { $hint += ' -Staged' } + if ($Base) { $hint += " -Base $Base" } Write-Host "$needsFormat file(s) need formatting. Run $hint and add the changes." } elseif (-not $Check) {