Repository navigation
Expand file tree
/
Copy pathrunner.bat.template
More file actions
107 lines (97 loc) · 4.9 KB
/
Copy pathrunner.bat.template
File metadata and controls
107 lines (97 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
@echo off
setlocal DisableDelayedExpansion
set "BUILDIFIER_SHORT_PATH={BUILDIFIER_SHORT_PATH}"
REM When --enable_runfiles, we should be able to locate buildifier directly through symlink.
set "buildifier_path=%BUILDIFIER_SHORT_PATH:/=\%"
REM Find the runfiles manifest. Tests also use it to resolve the workspace.
set "manifest_path="
for %%M in ("%RUNFILES_MANIFEST_FILE%" "%~f0.runfiles_manifest" "%~f0.exe.runfiles_manifest" "MANIFEST" "..\MANIFEST") do (
if not defined manifest_path if exist "%%~M" set "manifest_path=%%~M"
)
REM Otherwise, locate the buildifier through the runfiles manifest.
if exist "%buildifier_path%" goto buildifier_found
if not defined manifest_path (
echo A MANIFEST file cannot be found
exit /b 1
)
for /F "tokens=1,* delims= " %%F IN ('findstr /l /c:"buildifier.exe" "%manifest_path%"') DO set "buildifier_path=%%G"
:buildifier_found
for %%I in ("%buildifier_path%") do set "buildifier_path=%%~fsI"
if not exist "%buildifier_path%" (
echo Failed to find buildifier at "%buildifier_path%"
exit /b 1
)
REM find the workspace directory in manifest.
REM No good way in bat to read path of a symlink's target
set "workspace_dir="
if defined manifest_path for /F "tokens=1,* delims= " %%F IN ('findstr /l /c:"WORKSPACE" "%manifest_path%"') DO set "workspace_dir=%%~dpG"
if defined BUILD_WORKSPACE_DIRECTORY goto build_workspace
if defined TEST_WORKSPACE goto test_workspace
echo Failed to find workspace to check
exit /b 1
:build_workspace
REM Either a build, or a run/build inside a test.
cd /d "%BUILD_WORKSPACE_DIRECTORY%" || exit /b 1
goto workspace_found
:test_workspace
if not defined workspace_dir (
set "include_reparse_points=1"
goto workspace_found
)
cd /d "%workspace_dir%" || exit /b 1
:workspace_found
REM Keep discovered paths inside PowerShell so cmd.exe never reparses them.
REM Walk the workspace without entering excluded directories, matching each
REM path like find's -path.
REM Process files in batches to stay below CreateProcess' command-line limit,
REM merging per-batch diagnostics when JSON output is requested.
set "BUILDIFIER_EXECUTABLE=%buildifier_path%"
powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference = 'Stop';" ^
"$buildifierArgs = @({POWERSHELL_ARGS});" ^
"$files = @({SOURCE_FILES});" ^
"if ({SCAN_WORKSPACE}) {" ^
"$excludePatterns = @({POWERSHELL_EXCLUDE_PATTERNS});" ^
"$root = (Get-Location).Path;" ^
"$found = New-Object 'System.Collections.Generic.List[string]';" ^
"$pending = New-Object 'System.Collections.Generic.Stack[string]';" ^
"$pending.Push('');" ^
"while ($pending.Count -gt 0) {" ^
"$relativeDir = $pending.Pop();" ^
"foreach ($entry in [IO.DirectoryInfo]::new([IO.Path]::Combine($root, $relativeDir)).EnumerateFileSystemInfos()) {" ^
"if (-not $env:include_reparse_points -and ($entry.Attributes -band [IO.FileAttributes]::ReparsePoint)) { continue };" ^
"$relativePath = if ($relativeDir) { $relativeDir + '/' + $entry.Name } else { $entry.Name };" ^
"$excluded = $false;" ^
"foreach ($pattern in $excludePatterns) { if (('./' + $relativePath) -clike $pattern) { $excluded = $true; break } };" ^
"if ($excluded) { continue };" ^
"if ($entry.Attributes -band [IO.FileAttributes]::Directory) { $pending.Push($relativePath) }" ^
"elseif ($entry.Name -cmatch '^(BUILD|WORKSPACE|WORKSPACE\.bzlmod|WORKSPACE\.oss|BUILD\..*\.oss|WORKSPACE\..*\.oss)$|\.(bzl|sky|star|bazel|BUILD)$') { $found.Add($relativePath.Replace('/', '\')) }" ^
"}" ^
"};" ^
"$found.Sort([StringComparer]::Ordinal);" ^
"$files = @($found);" ^
"};" ^
"if ($files.Count -eq 0) { Write-Error 'Failed to find files to analyze'; exit 1 };" ^
"$jsonOutput = '{FORMAT}' -eq 'json';" ^
"$exitCode = 0;" ^
"$jsonSuccess = $true;" ^
"$jsonFiles = @();" ^
"for ($i = 0; $i -lt $files.Count; $i += 100) {" ^
"$batch = @($files[$i..([Math]::Min($i + 99, $files.Count - 1))]);" ^
"if ($jsonOutput) {" ^
"$batchOutput = & $env:BUILDIFIER_EXECUTABLE @buildifierArgs @batch;" ^
"if ($LASTEXITCODE -eq 0) {" ^
"$batchReport = ($batchOutput -join [Environment]::NewLine) | ConvertFrom-Json;" ^
"$jsonSuccess = $jsonSuccess -and [bool]$batchReport.success;" ^
"$jsonFiles += @($batchReport.files);" ^
"}" ^
"} else {" ^
"& $env:BUILDIFIER_EXECUTABLE @buildifierArgs @batch;" ^
"};" ^
"if ($exitCode -eq 0) { $exitCode = $LASTEXITCODE };" ^
"};" ^
"if ($jsonOutput -and $exitCode -eq 0) {" ^
"[ordered]@{ success = $jsonSuccess; files = [object[]]$jsonFiles } | ConvertTo-Json -Depth 10 -Compress:('{VERBOSE}' -ne 'true');" ^
"};" ^
"exit $exitCode"
exit /b %ERRORLEVEL%