Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ahk-format.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions .github/workflows/rust-helpers.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions Helper/Format-Ahk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
Expand All @@ -55,6 +60,7 @@ param(
[string[]]$Path,
[switch]$Check,
[switch]$Staged,
[string]$Base,
[switch]$Test,
[switch]$Stdin
)
Expand All @@ -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.
Expand Down Expand Up @@ -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) })
Expand Down Expand Up @@ -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))
Expand All @@ -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) {
Expand Down