Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / check.bat
check.bat251 lines · 9.7 KiBRaw
| 1 | <# : |
| 2 | @echo off |
| 3 | setlocal |
| 4 | where pwsh >nul 2>nul |
| 5 | if %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'))) %*" |
| 7 | exit /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 | |
| 33 | function Show-CheckHelp { |
| 34 | @' |
| 35 | Usage: |
| 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 | |
| 41 | Module notation mirrors install.bat: slash == colon, games:/personal: shorthand, |
| 42 | 'all' keyword, deduped. Tasks run with --continue so one broken module does not |
| 43 | block the rest; exit code is nonzero if anything failed. |
| 44 | |
| 45 | Rust: a module with src/main/rust/Cargo.toml is checked with `cargo check |
| 46 | --locked`: workspace members together via `-p` from the repo root, standalone |
| 47 | crates (own [workspace]) individually in their own dir (host target: fast smoke |
| 48 | check; the real aarch64 cross-build runs inside the Gradle build via |
| 49 | cargoNdkBuild). Lint-sweep Phase 4 adds, per in-scope crate: `cargo clippy |
| 50 | --locked --all-targets` (blocking on deny/error lints), `cargo fmt --check` |
| 51 | (advisory until the tree is fmt-clean), `cargo doc --locked --no-deps`. |
| 52 | No rust modules -> cargo steps skipped. |
| 53 | '@ |
| 54 | } |
| 55 | |
| 56 | function Normalize-CheckModule([string]$M) { |
| 57 | $M = $M.Trim() |
| 58 | $M = [regex]::Replace($M, '^[/:]+', '') |
| 59 | $M = [regex]::Replace($M, '[/:]+$', '') |
| 60 | $M = $M.Replace('/', ':') |
| 61 | while ($M.Contains('::')) { $M = $M.Replace('::', ':') } |
| 62 | return $M.ToLowerInvariant() |
| 63 | } |
| 64 | |
| 65 | # all = app + library convention modules (both apply dev.detekt; pure-JVM |
| 66 | # modules like youpipe:extractor do not, so they are excluded here but still |
| 67 | # accepted when named explicitly and left for Gradle to report on). |
| 68 | function Get-CheckAllModuleDirs { |
| 69 | $CheckSettings = Join-Path $CheckRoot 'settings.gradle.kts' |
| 70 | $CheckFound = @() |
| 71 | if (-not (Test-Path -LiteralPath $CheckSettings)) { return $CheckFound } |
| 72 | foreach ($CheckLine in [System.IO.File]::ReadLines($CheckSettings)) { |
| 73 | if ($CheckLine -match 'include\(":([^"]+)"\)') { |
| 74 | $CheckP = $Matches[1].Replace(':', '/') |
| 75 | if (-not $CheckP) { continue } |
| 76 | $CheckBuild = Join-Path $CheckRoot "$CheckP/build.gradle.kts" |
| 77 | if ((Test-Path -LiteralPath $CheckBuild) -and |
| 78 | (Select-String -LiteralPath $CheckBuild -Pattern 'common-conventions-(app|library)' -Quiet)) { |
| 79 | $CheckFound += $CheckP |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return @($CheckFound | Sort-Object -Unique) |
| 84 | } |
| 85 | |
| 86 | function Get-CheckCargoBin { |
| 87 | $CheckCargoCmd = Get-Command cargo -ErrorAction SilentlyContinue |
| 88 | if ($CheckCargoCmd) { return $CheckCargoCmd.Source } |
| 89 | $CheckCargoHome = if ($env:CARGO_HOME) { $env:CARGO_HOME } else { Join-Path $HOME '.cargo' } |
| 90 | $CheckCand = Join-Path $CheckCargoHome 'bin/cargo' |
| 91 | if (Test-Path -LiteralPath $CheckCand -PathType Leaf) { return $CheckCand } |
| 92 | $CheckCandExe = "$CheckCand.exe" |
| 93 | if (Test-Path -LiteralPath $CheckCandExe -PathType Leaf) { return $CheckCandExe } |
| 94 | return '' |
| 95 | } |
| 96 | |
| 97 | if ($args.Count -eq 0) { |
| 98 | Show-CheckHelp |
| 99 | Write-Output '' |
| 100 | Write-Output 'Error: no modules specified.' |
| 101 | exit 1 |
| 102 | } |
| 103 | foreach ($CheckA in $args) { |
| 104 | if ($CheckA -eq '--help' -or $CheckA -eq '-h' -or $CheckA -eq 'help') { |
| 105 | Show-CheckHelp |
| 106 | exit 0 |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $CheckMods = @() |
| 111 | $CheckAllExp = $false |
| 112 | foreach ($CheckRaw in $args) { |
| 113 | $CheckNorm = Normalize-CheckModule ([string]$CheckRaw) |
| 114 | if (-not $CheckNorm) { continue } |
| 115 | if ($CheckNorm -eq 'all') { |
| 116 | if (-not $CheckAllExp) { |
| 117 | foreach ($CheckSm in (Get-CheckAllModuleDirs)) { |
| 118 | if ($CheckSm) { $CheckMods += $CheckSm } |
| 119 | } |
| 120 | $CheckAllExp = $true |
| 121 | } |
| 122 | continue |
| 123 | } |
| 124 | if (-not $CheckNorm.Contains(':')) { |
| 125 | foreach ($CheckPfx in @('games', 'personal')) { |
| 126 | if (Test-Path -LiteralPath (Join-Path $CheckRoot "$CheckPfx/$CheckNorm/build.gradle.kts")) { |
| 127 | $CheckNorm = "$CheckPfx`:$CheckNorm" |
| 128 | break |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | $CheckFs = $CheckNorm.Replace(':', '/') |
| 133 | if (-not (Test-Path -LiteralPath (Join-Path $CheckRoot "$CheckFs/build.gradle.kts"))) { |
| 134 | [Console]::Error.WriteLine("Error: unknown module '$CheckRaw' (-> $CheckFs/, no build.gradle.kts).") |
| 135 | exit 1 |
| 136 | } |
| 137 | if ($CheckMods -notcontains $CheckFs) { $CheckMods += $CheckFs } |
| 138 | } |
| 139 | if ($CheckMods.Count -eq 0) { |
| 140 | [Console]::Error.WriteLine('Error: no valid modules.') |
| 141 | exit 1 |
| 142 | } |
| 143 | |
| 144 | $CheckTasks = @() |
| 145 | foreach ($CheckMod in $CheckMods) { |
| 146 | $CheckGmod = $CheckMod.Replace('/', ':') |
| 147 | $CheckTasks += ":${CheckGmod}:detekt" |
| 148 | $CheckTasks += ":${CheckGmod}:lint" |
| 149 | } |
| 150 | Write-Output "Modules ($($CheckMods.Count)): $($CheckMods -join ' ')" |
| 151 | Write-Output "Gradle tasks: $($CheckTasks -join ' ')" |
| 152 | Write-Output '' |
| 153 | |
| 154 | # gradlew: prefer the extensionless wrapper via bash when present |
| 155 | # (keeps one code path), else gradlew.bat on Windows. |
| 156 | $CheckGradlewSh = Join-Path $CheckRoot 'gradlew' |
| 157 | $CheckGradlewBat = Join-Path $CheckRoot 'gradlew.bat' |
| 158 | $CheckBashCmd = Get-Command bash -ErrorAction SilentlyContinue |
| 159 | if ($CheckBashCmd -and (Test-Path -LiteralPath $CheckGradlewSh)) { |
| 160 | $CheckPrevPref = $ErrorActionPreference |
| 161 | $ErrorActionPreference = 'Continue' |
| 162 | & $CheckBashCmd.Source $CheckGradlewSh @CheckTasks --continue --console=plain |
| 163 | $CheckDetektRc = $LASTEXITCODE |
| 164 | $ErrorActionPreference = $CheckPrevPref |
| 165 | } |
| 166 | elseif (Test-Path -LiteralPath $CheckGradlewBat) { |
| 167 | $CheckPrevPref = $ErrorActionPreference |
| 168 | $ErrorActionPreference = 'Continue' |
| 169 | & $CheckGradlewBat @CheckTasks --continue --console=plain |
| 170 | $CheckDetektRc = $LASTEXITCODE |
| 171 | $ErrorActionPreference = $CheckPrevPref |
| 172 | } |
| 173 | else { |
| 174 | [Console]::Error.WriteLine('Error: no Gradle wrapper found (gradlew / gradlew.bat).') |
| 175 | exit 1 |
| 176 | } |
| 177 | Write-Output '' |
| 178 | if ($CheckDetektRc -ne 0) { Write-Output "Warning: detekt/lint reported failures (exit $CheckDetektRc)." } |
| 179 | |
| 180 | # --- cargo check (rust modules only) --- |
| 181 | # Workspace members share one root `cargo check -p ...` invocation; standalone |
| 182 | # crates (own [workspace] table + lockfile, e.g. games:voxels, code) are checked |
| 183 | # individually with cwd set to the crate dir. |
| 184 | $CheckRootCrates = @() |
| 185 | $CheckStandaloneDirs = @() |
| 186 | foreach ($CheckMod in $CheckMods) { |
| 187 | $CheckRdir = Join-Path $CheckRoot "$CheckMod/src/main/rust" |
| 188 | $CheckCt = Join-Path $CheckRdir 'Cargo.toml' |
| 189 | if (Test-Path -LiteralPath $CheckCt -PathType Leaf) { |
| 190 | if (Select-String -LiteralPath $CheckCt -Pattern '^\[workspace\]\s*$' -Quiet) { |
| 191 | $CheckStandaloneDirs += $CheckRdir |
| 192 | Write-Output "Rust: $CheckMod -> standalone crate ($CheckRdir)" |
| 193 | } |
| 194 | else { |
| 195 | $CheckName = $null |
| 196 | foreach ($CheckLine in [System.IO.File]::ReadLines($CheckCt)) { |
| 197 | if ($CheckLine -match '^\s*name\s*=\s*"([^"]+)"') { $CheckName = $Matches[1]; break } |
| 198 | } |
| 199 | if ($CheckName) { |
| 200 | $CheckRootCrates += $CheckName |
| 201 | Write-Output "Rust: $CheckMod -> workspace crate $CheckName" |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | $CheckCargoRc = 0 |
| 207 | $CheckCargoBin = Get-CheckCargoBin |
| 208 | if (-not $CheckCargoBin) { |
| 209 | [Console]::Error.WriteLine('Error: cargo not found (install Rust or set CARGO_HOME).') |
| 210 | exit 1 |
| 211 | } |
| 212 | if ($CheckRootCrates.Count -gt 0) { |
| 213 | Write-Output '' |
| 214 | $CheckCargoArgs = @() |
| 215 | foreach ($CheckC in $CheckRootCrates) { $CheckCargoArgs += @('-p', $CheckC) } |
| 216 | Write-Output "Running: cargo check --locked $($CheckCargoArgs -join ' ')" |
| 217 | $CheckPrevPref = $ErrorActionPreference |
| 218 | $ErrorActionPreference = 'Continue' |
| 219 | Push-Location -LiteralPath $CheckRoot |
| 220 | try { |
| 221 | & $CheckCargoBin check --locked @CheckCargoArgs |
| 222 | $CheckCargoRc = $LASTEXITCODE |
| 223 | } |
| 224 | finally { |
| 225 | Pop-Location |
| 226 | $ErrorActionPreference = $CheckPrevPref |
| 227 | } |
| 228 | if ($CheckCargoRc -ne 0) { Write-Output "Warning: cargo check (workspace) reported failures (exit $CheckCargoRc)." } |
| 229 | } |
| 230 | foreach ($CheckRdir in $CheckStandaloneDirs) { |
| 231 | Write-Output '' |
| 232 | Write-Output "Running: cargo check --locked (in $CheckRdir)" |
| 233 | $CheckPrevPref = $ErrorActionPreference |
| 234 | $ErrorActionPreference = 'Continue' |
| 235 | Push-Location -LiteralPath $CheckRdir |
| 236 | try { |
| 237 | & $CheckCargoBin check --locked |
| 238 | $CheckCargoRc = $LASTEXITCODE |
| 239 | } |
| 240 | finally { |
| 241 | Pop-Location |
| 242 | $ErrorActionPreference = $CheckPrevPref |
| 243 | } |
| 244 | if ($CheckCargoRc -ne 0) { Write-Output "Warning: cargo check ($CheckRdir) reported failures (exit $CheckCargoRc)." } |
| 245 | } |
| 246 | if (($CheckRootCrates.Count -eq 0) -and ($CheckStandaloneDirs.Count -eq 0)) { |
| 247 | Write-Output 'No Rust crates in scope; cargo step skipped.' |
| 248 | } |
| 249 | |
| 250 | if (($CheckDetektRc -ne 0) -or ($CheckCargoRc -ne 0)) { exit 1 } |
| 251 | exit 0 |