Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / check.bat

check.bat276 lines · 10.8 KiBRaw
1<# :
2@echo off
3setlocal
4where pwsh >nul 2>nul
5if %errorlevel%==0 ( set "PSBIN=pwsh" ) else ( set "PSBIN=powershell" )
6%PSBIN% -NoProfile -ExecutionPolicy Bypass -Command "$env:_BATROOT='%~dp0'; $env:_BATPATH='%~f0'; & ([scriptblock]::Create((Get-Content -Raw '%~f0'))) %*"
7exit /b %errorlevel%
8: end batch #>
9# check.bat - Static verification without building or installing.
10# (PowerShell twin of ./check.)
11# check.bat <module> [module...] # detekt + lint + cargo check/clippy/fmt/doc
12# check.bat games/voxels maps # multi-module, one gradlew invocation
13# check.bat voxels # shorthand -> games:voxels (same as install.bat)
14# check.bat all # every app + library module
15#
16# Module notation mirrors install.bat: slash == colon, games:/personal: shorthand,
17# 'all' keyword, deduped. Tasks run with --continue so one broken module does not
18# block the rest; exit code is nonzero if anything failed.
19#
20# Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check
21# --locked`: workspace members together via `-p` from the repo root, standalone
22# crates (own [workspace]) individually in their own dir (host target: fast smoke
23# check; the real aarch64 cross-build runs inside the Gradle build via
24# cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy
25# --locked --all-targets` (blocking on deny/error lints; warnings never fail),
26# `cargo fmt --check` (advisory report only until the tree is fmt-clean), and
27# `cargo doc --locked --no-deps` (blocking on rustdoc errors). No rust modules
28# -> cargo steps skipped.
29$ErrorActionPreference = 'Stop'
30
31$CheckRoot = if ($PSScriptRoot) { $PSScriptRoot } else { $env:_BATROOT.TrimEnd('\') }
32
33function Show-CheckHelp {
34 @'
35Usage:
36 check.bat <module> [module...] # detekt (detekt.yml) + lint + cargo check/clippy/fmt/doc
37 check.bat games/voxels maps # multi-module, one gradlew invocation
38 check.bat voxels # shorthand -> games:voxels (same as install.bat)
39 check.bat all # every app + library module
40 check.bat structure # only the structure ratchets (below)
41
42Structure ratchets (docs/structure/README.md): tools/structure_check.py
43checks Wear protocol-pair drift, the generated-source manifest, and the
44exception-marker inventory. They run on every check.bat invocation (cheap,
45no build) and their failure fails this script.
46
47Module notation mirrors install.bat: slash == colon, games:/personal: shorthand,
48'all' keyword, deduped. Tasks run with --continue so one broken module does not
49block the rest; exit code is nonzero if anything failed.
50
51Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check
52--locked`: workspace members together via `-p` from the repo root, standalone
53crates (own [workspace]) individually in their own dir (host target: fast smoke
54check; the real aarch64 cross-build runs inside the Gradle build via
55cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy
56--locked --all-targets` (blocking on deny/error lints), `cargo fmt --check`
57(advisory until the tree is fmt-clean), `cargo doc --locked --no-deps`.
58No rust modules -> cargo steps skipped.
59'@
60}
61
62function Normalize-CheckModule([string]$M) {
63 $M = $M.Trim()
64 $M = [regex]::Replace($M, '^[/:]+', '')
65 $M = [regex]::Replace($M, '[/:]+$', '')
66 $M = $M.Replace('/', ':')
67 while ($M.Contains('::')) { $M = $M.Replace('::', ':') }
68 return $M.ToLowerInvariant()
69}
70
71# all = app + library convention modules (both apply dev.detekt; pure-JVM
72# modules like youpipe:extractor do not, so they are excluded here but still
73# accepted when named explicitly and left for Gradle to report on).
74function Get-CheckAllModuleDirs {
75 $CheckSettings = Join-Path $CheckRoot 'settings.gradle.kts'
76 $CheckFound = @()
77 if (-not (Test-Path -LiteralPath $CheckSettings)) { return $CheckFound }
78 foreach ($CheckLine in [System.IO.File]::ReadLines($CheckSettings)) {
79 if ($CheckLine -match 'include\(":([^"]+)"\)') {
80 $CheckP = $Matches[1].Replace(':', '/')
81 if (-not $CheckP) { continue }
82 $CheckBuild = Join-Path $CheckRoot "$CheckP/build.gradle.kts"
83 if ((Test-Path -LiteralPath $CheckBuild) -and
84 (Select-String -LiteralPath $CheckBuild -Pattern 'common-conventions-(app|library)' -Quiet)) {
85 $CheckFound += $CheckP
86 }
87 }
88 }
89 return @($CheckFound | Sort-Object -Unique)
90}
91
92function Get-CheckCargoBin {
93 $CheckCargoCmd = Get-Command cargo -ErrorAction SilentlyContinue
94 if ($CheckCargoCmd) { return $CheckCargoCmd.Source }
95 $CheckCargoHome = if ($env:CARGO_HOME) { $env:CARGO_HOME } else { Join-Path $HOME '.cargo' }
96 $CheckCand = Join-Path $CheckCargoHome 'bin/cargo'
97 if (Test-Path -LiteralPath $CheckCand -PathType Leaf) { return $CheckCand }
98 $CheckCandExe = "$CheckCand.exe"
99 if (Test-Path -LiteralPath $CheckCandExe -PathType Leaf) { return $CheckCandExe }
100 return ''
101}
102
103if ($args.Count -eq 0) {
104 Show-CheckHelp
105 Write-Output ''
106 Write-Output 'Error: no modules specified.'
107 exit 1
108}
109foreach ($CheckA in $args) {
110 if ($CheckA -eq '--help' -or $CheckA -eq '-h' -or $CheckA -eq 'help') {
111 Show-CheckHelp
112 exit 0
113 }
114}
115
116function Invoke-CheckStructure {
117 $CheckPy = Get-Command python3 -ErrorAction SilentlyContinue
118 if (-not $CheckPy) { $CheckPy = Get-Command python -ErrorAction SilentlyContinue }
119 if (-not $CheckPy) {
120 [Console]::Error.WriteLine('Error: python3 not found; structure checks (tools/structure_check.py) cannot run.')
121 return 1
122 }
123 & $CheckPy.Source (Join-Path $CheckRoot 'tools/structure_check.py') all
124 return $LASTEXITCODE
125}
126
127foreach ($CheckA in $args) {
128 if ($CheckA -eq 'structure') { exit (Invoke-CheckStructure) }
129}
130
131$CheckMods = @()
132$CheckAllExp = $false
133foreach ($CheckRaw in $args) {
134 $CheckNorm = Normalize-CheckModule ([string]$CheckRaw)
135 if (-not $CheckNorm) { continue }
136 if ($CheckNorm -eq 'all') {
137 if (-not $CheckAllExp) {
138 foreach ($CheckSm in (Get-CheckAllModuleDirs)) {
139 if ($CheckSm) { $CheckMods += $CheckSm }
140 }
141 $CheckAllExp = $true
142 }
143 continue
144 }
145 if (-not $CheckNorm.Contains(':')) {
146 foreach ($CheckPfx in @('games', 'personal')) {
147 if (Test-Path -LiteralPath (Join-Path $CheckRoot "$CheckPfx/$CheckNorm/build.gradle.kts")) {
148 $CheckNorm = "$CheckPfx`:$CheckNorm"
149 break
150 }
151 }
152 }
153 $CheckFs = $CheckNorm.Replace(':', '/')
154 if (-not (Test-Path -LiteralPath (Join-Path $CheckRoot "$CheckFs/build.gradle.kts"))) {
155 [Console]::Error.WriteLine("Error: unknown module '$CheckRaw' (-> $CheckFs/, no build.gradle.kts).")
156 exit 1
157 }
158 if ($CheckMods -notcontains $CheckFs) { $CheckMods += $CheckFs }
159}
160if ($CheckMods.Count -eq 0) {
161 [Console]::Error.WriteLine('Error: no valid modules.')
162 exit 1
163}
164
165$CheckTasks = @()
166foreach ($CheckMod in $CheckMods) {
167 $CheckGmod = $CheckMod.Replace('/', ':')
168 $CheckTasks += ":${CheckGmod}:detekt"
169 $CheckTasks += ":${CheckGmod}:lint"
170}
171Write-Output "Modules ($($CheckMods.Count)): $($CheckMods -join ' ')"
172Write-Output "Gradle tasks: $($CheckTasks -join ' ')"
173Write-Output ''
174
175$CheckStructureRc = Invoke-CheckStructure
176if ($CheckStructureRc -ne 0) { Write-Output "Warning: structure checks reported failures (exit $CheckStructureRc)." }
177Write-Output ''
178
179# gradlew: prefer the extensionless wrapper via bash when present
180# (keeps one code path), else gradlew.bat on Windows.
181$CheckGradlewSh = Join-Path $CheckRoot 'gradlew'
182$CheckGradlewBat = Join-Path $CheckRoot 'gradlew.bat'
183$CheckBashCmd = Get-Command bash -ErrorAction SilentlyContinue
184if ($CheckBashCmd -and (Test-Path -LiteralPath $CheckGradlewSh)) {
185 $CheckPrevPref = $ErrorActionPreference
186 $ErrorActionPreference = 'Continue'
187 & $CheckBashCmd.Source $CheckGradlewSh @CheckTasks --continue --console=plain
188 $CheckDetektRc = $LASTEXITCODE
189 $ErrorActionPreference = $CheckPrevPref
190}
191elseif (Test-Path -LiteralPath $CheckGradlewBat) {
192 $CheckPrevPref = $ErrorActionPreference
193 $ErrorActionPreference = 'Continue'
194 & $CheckGradlewBat @CheckTasks --continue --console=plain
195 $CheckDetektRc = $LASTEXITCODE
196 $ErrorActionPreference = $CheckPrevPref
197}
198else {
199 [Console]::Error.WriteLine('Error: no Gradle wrapper found (gradlew / gradlew.bat).')
200 exit 1
201}
202Write-Output ''
203if ($CheckDetektRc -ne 0) { Write-Output "Warning: detekt/lint reported failures (exit $CheckDetektRc)." }
204
205# --- cargo check (rust modules only) ---
206# Workspace members share one root `cargo check -p ...` invocation; standalone
207# crates (own [workspace] table + lockfile, e.g. games:voxels, code) are checked
208# individually with cwd set to the crate dir.
209$CheckRootCrates = @()
210$CheckStandaloneDirs = @()
211foreach ($CheckMod in $CheckMods) {
212 $CheckRdir = Join-Path $CheckRoot "$CheckMod/src/main/rust"
213 $CheckCt = Join-Path $CheckRdir 'Cargo.toml'
214 if (Test-Path -LiteralPath $CheckCt -PathType Leaf) {
215 if (Select-String -LiteralPath $CheckCt -Pattern '^\[workspace\]\s*$' -Quiet) {
216 $CheckStandaloneDirs += $CheckRdir
217 Write-Output "Rust: $CheckMod -> standalone crate ($CheckRdir)"
218 }
219 else {
220 $CheckName = $null
221 foreach ($CheckLine in [System.IO.File]::ReadLines($CheckCt)) {
222 if ($CheckLine -match '^\s*name\s*=\s*"([^"]+)"') { $CheckName = $Matches[1]; break }
223 }
224 if ($CheckName) {
225 $CheckRootCrates += $CheckName
226 Write-Output "Rust: $CheckMod -> workspace crate $CheckName"
227 }
228 }
229 }
230}
231$CheckCargoRc = 0
232$CheckCargoBin = Get-CheckCargoBin
233if (-not $CheckCargoBin) {
234 [Console]::Error.WriteLine('Error: cargo not found (install Rust or set CARGO_HOME).')
235 exit 1
236}
237if ($CheckRootCrates.Count -gt 0) {
238 Write-Output ''
239 $CheckCargoArgs = @()
240 foreach ($CheckC in $CheckRootCrates) { $CheckCargoArgs += @('-p', $CheckC) }
241 Write-Output "Running: cargo check --locked $($CheckCargoArgs -join ' ')"
242 $CheckPrevPref = $ErrorActionPreference
243 $ErrorActionPreference = 'Continue'
244 Push-Location -LiteralPath $CheckRoot
245 try {
246 & $CheckCargoBin check --locked @CheckCargoArgs
247 $CheckCargoRc = $LASTEXITCODE
248 }
249 finally {
250 Pop-Location
251 $ErrorActionPreference = $CheckPrevPref
252 }
253 if ($CheckCargoRc -ne 0) { Write-Output "Warning: cargo check (workspace) reported failures (exit $CheckCargoRc)." }
254}
255foreach ($CheckRdir in $CheckStandaloneDirs) {
256 Write-Output ''
257 Write-Output "Running: cargo check --locked (in $CheckRdir)"
258 $CheckPrevPref = $ErrorActionPreference
259 $ErrorActionPreference = 'Continue'
260 Push-Location -LiteralPath $CheckRdir
261 try {
262 & $CheckCargoBin check --locked
263 $CheckCargoRc = $LASTEXITCODE
264 }
265 finally {
266 Pop-Location
267 $ErrorActionPreference = $CheckPrevPref
268 }
269 if ($CheckCargoRc -ne 0) { Write-Output "Warning: cargo check ($CheckRdir) reported failures (exit $CheckCargoRc)." }
270}
271if (($CheckRootCrates.Count -eq 0) -and ($CheckStandaloneDirs.Count -eq 0)) {
272 Write-Output 'No Rust crates in scope; cargo step skipped.'
273}
274
275if (($CheckStructureRc -ne 0) -or ($CheckDetektRc -ne 0) -or ($CheckCargoRc -ne 0)) { exit 1 }
276exit 0