Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / search.bat
search.bat382 lines · 14.2 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 | # search.bat - Scoped string search for the Modern-Apps monorepo. |
| 10 | # (PowerShell twin of ./search.) Uses rg when available, else Select-String. |
| 11 | # |
| 12 | # Usage (cmd): |
| 13 | # search.bat PATTERN [-m MODULE ...] [-p PACKAGE ...] [-i] [--max N] [--files-only] |
| 14 | $ErrorActionPreference = 'Stop' |
| 15 | |
| 16 | $SearchRoot = if ($PSScriptRoot) { $PSScriptRoot } else { $env:_BATROOT.TrimEnd('\') } |
| 17 | $SearchMaxCols = 220 |
| 18 | $SearchDefaultMax = 200 |
| 19 | $SearchKnownPackages = @('ui', 'data', 'domain', 'platform', 'network', 'intents', 'service', 'provider', 'widget', 'notifications', 'auth', 'sync', 'telephony') |
| 20 | $SearchExcludeDirs = @('target', 'build', '.gradle', '.kotlin', '.git', '.llms', 'analysis') |
| 21 | |
| 22 | function Show-SearchHelp { |
| 23 | @' |
| 24 | Usage: |
| 25 | search.bat PATTERN [-m MODULE ...] [-p PACKAGE ...] [-i] [--max N] [--files-only] |
| 26 | |
| 27 | -m/--module Repeatable, comma-separated. Slash == colon |
| 28 | (games/voxels == games:voxels), shorthand (voxels -> games:voxels), |
| 29 | or 'all' (every dir with build.gradle.kts). |
| 30 | -p/--package Repeatable, comma-separated root package (ui data domain platform |
| 31 | network intents service provider widget notifications auth sync |
| 32 | telephony). Searches only <module>/src/main/{java,kotlin}/**/<pkg>/. |
| 33 | Needs -m; without it a warning is printed and it is ignored. |
| 34 | -i Case-insensitive. --max N caps output lines (default 200). |
| 35 | --files-only Print matching file paths, no contents. |
| 36 | |
| 37 | Always excluded: target/ build/ .gradle/ .kotlin/ .git/ .llms/ analysis/ |
| 38 | *.log *.onnx metadata_data/photos/. Exit code is 0 on success even with no |
| 39 | matches; 1 when every -m value is unknown; 2 on bad regex. |
| 40 | '@ |
| 41 | } |
| 42 | |
| 43 | function Normalize-SearchModule([string]$M) { |
| 44 | $M = $M.Trim() |
| 45 | $M = [regex]::Replace($M, '^[/:]+', '') |
| 46 | $M = [regex]::Replace($M, '[/:]+$', '') |
| 47 | $M = $M.Replace('/', ':') |
| 48 | while ($M.Contains('::')) { $M = $M.Replace('::', ':') } |
| 49 | return $M.ToLowerInvariant() |
| 50 | } |
| 51 | |
| 52 | function Get-SearchAllModuleDirs { |
| 53 | $SearchSettings = Join-Path $SearchRoot 'settings.gradle.kts' |
| 54 | $SearchFound = @() |
| 55 | if (-not (Test-Path -LiteralPath $SearchSettings)) { return $SearchFound } |
| 56 | foreach ($SearchLine in [System.IO.File]::ReadLines($SearchSettings)) { |
| 57 | if ($SearchLine -match 'include\(":([^"]+)"\)') { |
| 58 | $SearchP = $Matches[1] |
| 59 | $SearchP = [regex]::Replace($SearchP, '^:', '') |
| 60 | $SearchP = $SearchP.Replace(':', '/') |
| 61 | if ($SearchP -and (Test-Path -LiteralPath (Join-Path $SearchRoot "$SearchP/build.gradle.kts"))) { |
| 62 | $SearchFound += $SearchP |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return @($SearchFound | Sort-Object -Unique) |
| 67 | } |
| 68 | |
| 69 | function ConvertTo-SearchRelative([string]$FullPath) { |
| 70 | $SearchSlash = $FullPath.Replace('\', '/') |
| 71 | $SearchRootSlash = $SearchRoot.Replace('\', '/').TrimEnd('/') |
| 72 | if ($SearchSlash.StartsWith("$SearchRootSlash/")) { |
| 73 | return $SearchSlash.Substring($SearchRootSlash.Length + 1) |
| 74 | } |
| 75 | return $SearchSlash |
| 76 | } |
| 77 | |
| 78 | function Get-SearchPkgDirs([string]$Mod, [string[]]$Pkgs) { |
| 79 | $SearchPkgFound = @() |
| 80 | foreach ($SearchLang in @('java', 'kotlin')) { |
| 81 | $SearchBase = Join-Path $SearchRoot "$Mod/src/main/$SearchLang" |
| 82 | if (-not (Test-Path -LiteralPath $SearchBase)) { continue } |
| 83 | foreach ($SearchPkg in $Pkgs) { |
| 84 | # repo-relative paths (bash prints find-relatives too); absolute paths |
| 85 | # would resurface as drive-colon lines that break path:line parsing. |
| 86 | $SearchPkgFound += @(Get-ChildItem -LiteralPath $SearchBase -Recurse -Directory -Force -ErrorAction SilentlyContinue | |
| 87 | Where-Object { $_.Name -ceq $SearchPkg } | |
| 88 | ForEach-Object { ConvertTo-SearchRelative $_.FullName }) |
| 89 | } |
| 90 | } |
| 91 | # drop dirs subsumed by another match (e.g. data/ inside data/) |
| 92 | $SearchKept = @() |
| 93 | foreach ($SearchA in $SearchPkgFound) { |
| 94 | $SearchASlash = $SearchA.Replace('\', '/') |
| 95 | $SearchSkip = $false |
| 96 | foreach ($SearchB in $SearchPkgFound) { |
| 97 | $SearchBSlash = $SearchB.Replace('\', '/') |
| 98 | if (($SearchASlash -ne $SearchBSlash) -and $SearchASlash.StartsWith("$SearchBSlash/")) { |
| 99 | $SearchSkip = $true |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | if (-not $SearchSkip) { $SearchKept += $SearchA } |
| 104 | } |
| 105 | return @($SearchKept | Sort-Object -Unique) |
| 106 | } |
| 107 | |
| 108 | function Test-SearchExcluded([string]$FullPath) { |
| 109 | $SearchSlash = $FullPath.Replace('\', '/') |
| 110 | $SearchRootSlash = $SearchRoot.Replace('\', '/').TrimEnd('/') |
| 111 | if ($SearchSlash.StartsWith("$SearchRootSlash/")) { |
| 112 | $SearchSlash = $SearchSlash.Substring($SearchRootSlash.Length + 1) |
| 113 | } |
| 114 | $SearchSegs = $SearchSlash.Split('/') |
| 115 | for ($SearchI = 0; $SearchI -lt $SearchSegs.Count - 1; $SearchI++) { |
| 116 | if ($SearchExcludeDirs -contains $SearchSegs[$SearchI]) { return $true } |
| 117 | } |
| 118 | if ($SearchSlash -like 'metadata_data/photos/*') { return $true } |
| 119 | $SearchLeaf = $SearchSegs[$SearchSegs.Count - 1] |
| 120 | if ($SearchLeaf -like '*.log' -or $SearchLeaf -like '*.onnx') { return $true } |
| 121 | return $false |
| 122 | } |
| 123 | |
| 124 | $SearchHasRg = $null -ne (Get-Command rg -ErrorAction SilentlyContinue) |
| 125 | |
| 126 | function Invoke-SearchRg([string]$Pattern, [string[]]$Dirs) { |
| 127 | $SearchRgArgs = @('--no-heading', '--line-number', '--color', 'never', '--hidden', '--path-separator', '/', |
| 128 | '-g', '!target/**', '-g', '!build/**', '-g', '!.gradle/**', '-g', '!.kotlin/**', |
| 129 | '-g', '!.git/**', '-g', '!.llms/**', '-g', '!analysis/**', |
| 130 | '-g', '!*.log', '-g', '!*.onnx', '-g', '!metadata_data/photos/**', |
| 131 | '--max-columns', "$SearchMaxCols") |
| 132 | if ($SearchIgnoreCase) { $SearchRgArgs += '-i' } |
| 133 | $SearchRgArgs += @('-e', $Pattern, '--') + $Dirs |
| 134 | $SearchPrevPref = $ErrorActionPreference |
| 135 | $ErrorActionPreference = 'Continue' |
| 136 | $SearchOut = & rg @SearchRgArgs 2>$null |
| 137 | $SearchRc = $LASTEXITCODE |
| 138 | $ErrorActionPreference = $SearchPrevPref |
| 139 | if ($SearchRc -eq 2) { |
| 140 | [Console]::Error.WriteLine("error: bad regex: $Pattern") |
| 141 | exit 2 |
| 142 | } |
| 143 | $SearchLines = @() |
| 144 | $SearchRootSlash = $SearchRoot.Replace('\', '/').TrimEnd('/') |
| 145 | foreach ($SearchLine in @($SearchOut)) { |
| 146 | $SearchNorm = ([string]$SearchLine).Replace('\', '/') |
| 147 | $SearchNorm = [regex]::Replace($SearchNorm, '^\./', '') |
| 148 | # defensive: if rg printed an absolute path (e.g. caller passed one), |
| 149 | # relativize so path:line parsing and --files-only splits stay correct. |
| 150 | if ($SearchNorm.StartsWith("$SearchRootSlash/")) { |
| 151 | $SearchNorm = $SearchNorm.Substring($SearchRootSlash.Length + 1) |
| 152 | } |
| 153 | $SearchLines += $SearchNorm |
| 154 | } |
| 155 | return $SearchLines |
| 156 | } |
| 157 | |
| 158 | function Invoke-SearchSelectString([string]$Pattern, [string[]]$Dirs) { |
| 159 | try { |
| 160 | $SearchOptions = [System.Text.RegularExpressions.RegexOptions]::None |
| 161 | if ($SearchIgnoreCase) { $SearchOptions = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase } |
| 162 | $null = New-Object System.Text.RegularExpressions.Regex($Pattern, $SearchOptions) |
| 163 | } |
| 164 | catch { |
| 165 | [Console]::Error.WriteLine("error: bad regex: $Pattern") |
| 166 | exit 2 |
| 167 | } |
| 168 | $SearchFiles = @() |
| 169 | foreach ($SearchDir in $Dirs) { |
| 170 | $SearchAbs = if ([System.IO.Path]::IsPathRooted($SearchDir)) { $SearchDir } else { Join-Path $SearchRoot $SearchDir } |
| 171 | if (-not (Test-Path -LiteralPath $SearchAbs)) { continue } |
| 172 | $SearchItem = Get-Item -LiteralPath $SearchAbs -Force -ErrorAction SilentlyContinue |
| 173 | if ($null -eq $SearchItem) { continue } |
| 174 | if ($SearchItem -is [System.IO.DirectoryInfo]) { |
| 175 | $SearchFiles += @(Get-ChildItem -LiteralPath $SearchAbs -Recurse -File -Force -ErrorAction SilentlyContinue | |
| 176 | Where-Object { -not (Test-SearchExcluded $_.FullName) }) |
| 177 | } |
| 178 | elseif (-not (Test-SearchExcluded $SearchItem.FullName)) { |
| 179 | $SearchFiles += $SearchItem |
| 180 | } |
| 181 | } |
| 182 | $SearchLines = @() |
| 183 | $SearchSelectOpts = @{} |
| 184 | if ($SearchIgnoreCase) { $SearchSelectOpts['CaseSensitive'] = $false } else { $SearchSelectOpts['CaseSensitive'] = $true } |
| 185 | foreach ($SearchFile in $SearchFiles) { |
| 186 | $SearchMatches = Select-String -LiteralPath $SearchFile.FullName -Pattern $Pattern @SearchSelectOpts 2>$null |
| 187 | foreach ($SearchM in @($SearchMatches)) { |
| 188 | $SearchText = [string]$SearchM.Line |
| 189 | if ($SearchText.Length -gt $SearchMaxCols) { $SearchText = $SearchText.Substring(0, $SearchMaxCols) } |
| 190 | $SearchLines += "$(ConvertTo-SearchRelative $SearchFile.FullName):$($SearchM.LineNumber):$SearchText" |
| 191 | } |
| 192 | } |
| 193 | return $SearchLines |
| 194 | } |
| 195 | |
| 196 | function Invoke-SearchDirs([string]$Pattern, [string[]]$Dirs) { |
| 197 | if ($SearchHasRg) { return Invoke-SearchRg $Pattern $Dirs } |
| 198 | return Invoke-SearchSelectString $Pattern $Dirs |
| 199 | } |
| 200 | |
| 201 | # --- arg parsing --- |
| 202 | $SearchPattern = '' |
| 203 | $SearchIgnoreCase = $false |
| 204 | $SearchMax = $SearchDefaultMax |
| 205 | $SearchFilesOnly = $false |
| 206 | $SearchModsRaw = @() |
| 207 | $SearchPkgsRaw = @() |
| 208 | $SearchIdx = 0 |
| 209 | while ($SearchIdx -lt $args.Count) { |
| 210 | $SearchArg = [string]$args[$SearchIdx] |
| 211 | if ($SearchArg -eq '-h' -or $SearchArg -eq '--help' -or $SearchArg -eq 'help') { |
| 212 | Show-SearchHelp |
| 213 | exit 0 |
| 214 | } |
| 215 | elseif ($SearchArg -eq '-m' -or $SearchArg -eq '--module') { |
| 216 | $SearchIdx++ |
| 217 | while (($SearchIdx -lt $args.Count) -and (-not ([string]$args[$SearchIdx]).StartsWith('-'))) { |
| 218 | $SearchModsRaw += [string]$args[$SearchIdx] |
| 219 | $SearchIdx++ |
| 220 | } |
| 221 | } |
| 222 | elseif ($SearchArg -eq '-p' -or $SearchArg -eq '--package') { |
| 223 | $SearchIdx++ |
| 224 | while (($SearchIdx -lt $args.Count) -and (-not ([string]$args[$SearchIdx]).StartsWith('-'))) { |
| 225 | $SearchPkgsRaw += [string]$args[$SearchIdx] |
| 226 | $SearchIdx++ |
| 227 | } |
| 228 | } |
| 229 | elseif ($SearchArg -eq '-i' -or $SearchArg -eq '--ignore-case') { |
| 230 | $SearchIgnoreCase = $true |
| 231 | $SearchIdx++ |
| 232 | } |
| 233 | elseif ($SearchArg -eq '--max') { |
| 234 | if ($SearchIdx + 1 -ge $args.Count) { |
| 235 | [Console]::Error.WriteLine('Error: --max needs a value.') |
| 236 | exit 1 |
| 237 | } |
| 238 | $SearchMax = [int]$args[$SearchIdx + 1] |
| 239 | $SearchIdx += 2 |
| 240 | } |
| 241 | elseif ($SearchArg -like '--max=*') { |
| 242 | $SearchMax = [int]$SearchArg.Substring('--max='.Length) |
| 243 | $SearchIdx++ |
| 244 | } |
| 245 | elseif ($SearchArg -eq '--files-only') { |
| 246 | $SearchFilesOnly = $true |
| 247 | $SearchIdx++ |
| 248 | } |
| 249 | elseif ($SearchArg -eq '--') { |
| 250 | $SearchIdx++ |
| 251 | if ((-not $SearchPattern) -and ($SearchIdx -lt $args.Count)) { |
| 252 | $SearchPattern = [string]$args[$SearchIdx] |
| 253 | $SearchIdx++ |
| 254 | } |
| 255 | } |
| 256 | elseif ($SearchArg.StartsWith('-')) { |
| 257 | [Console]::Error.WriteLine("Error: unknown flag '$SearchArg'.") |
| 258 | exit 1 |
| 259 | } |
| 260 | else { |
| 261 | if (-not $SearchPattern) { |
| 262 | $SearchPattern = $SearchArg |
| 263 | } |
| 264 | else { |
| 265 | [Console]::Error.WriteLine("Error: unexpected arg '$SearchArg' (pattern already set).") |
| 266 | exit 1 |
| 267 | } |
| 268 | $SearchIdx++ |
| 269 | } |
| 270 | } |
| 271 | if (-not $SearchPattern) { |
| 272 | [Console]::Error.WriteLine('Error: no pattern. Usage: search.bat PATTERN [-m MODULE] [-p PACKAGE]') |
| 273 | exit 1 |
| 274 | } |
| 275 | |
| 276 | # split comma-separated filter values |
| 277 | $SearchModsSplit = @() |
| 278 | foreach ($SearchM in $SearchModsRaw) { |
| 279 | foreach ($SearchPart in $SearchM.Split(',')) { |
| 280 | if ($SearchPart) { $SearchModsSplit += $SearchPart } |
| 281 | } |
| 282 | } |
| 283 | $SearchPkgsSplit = @() |
| 284 | foreach ($SearchM in $SearchPkgsRaw) { |
| 285 | foreach ($SearchPart in $SearchM.Split(',')) { |
| 286 | if ($SearchPart) { $SearchPkgsSplit += $SearchPart.ToLowerInvariant() } |
| 287 | } |
| 288 | } |
| 289 | foreach ($SearchP in $SearchPkgsSplit) { |
| 290 | if ($SearchKnownPackages -notcontains $SearchP) { |
| 291 | [Console]::Error.WriteLine("warning: unknown root package '$SearchP'.") |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | # resolve modules |
| 296 | $SearchMods = @() |
| 297 | $SearchAllExp = $false |
| 298 | foreach ($SearchRaw in $SearchModsSplit) { |
| 299 | $SearchNorm = Normalize-SearchModule $SearchRaw |
| 300 | if ($SearchNorm -eq 'all') { |
| 301 | if (-not $SearchAllExp) { |
| 302 | foreach ($SearchSm in (Get-SearchAllModuleDirs)) { |
| 303 | if ($SearchSm) { $SearchMods += $SearchSm } |
| 304 | } |
| 305 | $SearchAllExp = $true |
| 306 | } |
| 307 | continue |
| 308 | } |
| 309 | if (-not $SearchNorm.Contains(':')) { |
| 310 | foreach ($SearchPfx in @('games', 'personal')) { |
| 311 | if (Test-Path -LiteralPath (Join-Path $SearchRoot "$SearchPfx/$SearchNorm/build.gradle.kts")) { |
| 312 | $SearchNorm = "$SearchPfx`:$SearchNorm" |
| 313 | break |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | $SearchFs = $SearchNorm.Replace(':', '/') |
| 318 | if (-not (Test-Path -LiteralPath (Join-Path $SearchRoot $SearchFs))) { |
| 319 | [Console]::Error.WriteLine("warning: skipping unknown module '$SearchRaw' (-> $SearchFs/).") |
| 320 | continue |
| 321 | } |
| 322 | if ($SearchMods -notcontains $SearchFs) { $SearchMods += $SearchFs } |
| 323 | } |
| 324 | if (($SearchModsRaw.Count -gt 0) -and ($SearchMods.Count -eq 0)) { |
| 325 | [Console]::Error.WriteLine('error: no valid modules (all -m values unknown)') |
| 326 | exit 1 |
| 327 | } |
| 328 | |
| 329 | Set-Location -LiteralPath $SearchRoot # backends then print repo-relative paths |
| 330 | # NOTE: state lives in a hashtable (reference type) rather than plain vars: |
| 331 | # this file also runs via & ([scriptblock]::Create(...)), where $script: |
| 332 | # scope modifiers leak outward, so plain-variable writes from inside |
| 333 | # functions would miss the scriptblock-level variable. |
| 334 | $SearchState = @{ Total = 0; Max = $SearchMax; FilesOnly = $SearchFilesOnly; Seen = @{} } |
| 335 | function Emit-SearchLines($Result) { |
| 336 | foreach ($SearchLine in @($Result)) { |
| 337 | if ($null -eq $SearchLine) { continue } |
| 338 | if ($SearchState.Total -ge $SearchState.Max) { break } |
| 339 | if ($SearchState.FilesOnly) { |
| 340 | $SearchF = ([string]$SearchLine -split ':', 2)[0] |
| 341 | if ($SearchState.Seen.ContainsKey($SearchF)) { continue } |
| 342 | $SearchState.Seen[$SearchF] = $true |
| 343 | Write-Output $SearchF |
| 344 | } |
| 345 | else { |
| 346 | Write-Output $SearchLine |
| 347 | } |
| 348 | $SearchState.Total++ |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if ($SearchMods.Count -gt 0) { |
| 353 | foreach ($SearchMod in $SearchMods) { |
| 354 | $SearchDirsList = @() |
| 355 | if ($SearchPkgsSplit.Count -gt 0) { |
| 356 | $SearchDirsList = @(Get-SearchPkgDirs $SearchMod $SearchPkgsSplit) |
| 357 | if ($SearchDirsList.Count -eq 0) { |
| 358 | [Console]::Error.WriteLine("warning: $SearchMod`: no $($SearchPkgsSplit -join '/') package under src/main/{java,kotlin}, skipped") |
| 359 | continue |
| 360 | } |
| 361 | } |
| 362 | else { |
| 363 | if (Test-Path -LiteralPath (Join-Path $SearchRoot "$SearchMod/src")) { |
| 364 | $SearchDirsList = @("$SearchMod/src") |
| 365 | } |
| 366 | else { |
| 367 | $SearchDirsList = @($SearchMod) |
| 368 | } |
| 369 | } |
| 370 | Write-Output "=== $SearchMod ===" |
| 371 | Emit-SearchLines (Invoke-SearchDirs $SearchPattern $SearchDirsList) |
| 372 | if ($SearchState.Total -ge $SearchMax) { break } |
| 373 | } |
| 374 | } |
| 375 | else { |
| 376 | if ($SearchPkgsSplit.Count -gt 0) { |
| 377 | [Console]::Error.WriteLine('warning: no -m given: -p needs a module scope, package filter ignored (repo-wide search)') |
| 378 | } |
| 379 | Emit-SearchLines (Invoke-SearchDirs $SearchPattern @('.')) |
| 380 | } |
| 381 | [Console]::Error.WriteLine("--- $($SearchState.Total) match(es) ---") |
| 382 | exit 0 |