blob: f45e963665161da14dfb7961d4a2d9e0609506fb [file] [edit]
@echo off
setlocal EnableDelayedExpansion
set BUILDIFIER_SHORT_PATH={BUILDIFIER_SHORT_PATH}
set EXTRA_ARGS={ARGS}
set WORKSPACE={WORKSPACE}
REM When --enable_runfiles, we should be able to locate buildifier directly through symlink.
set buildifier_path=%BUILDIFIER_SHORT_PATH:/=\%
for %%I in (%buildifier_path%) do (
set "buildifier_path=%%~fI"
)
set manifest_path=MANIFEST
if not exist %manifest_path% (
set manifest_path=..\MANIFEST
)
if not exist %manifest_path% (
echo a MANIFEST file cannot be found
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=
for /F "tokens=2 delims= " %%F IN ('findstr /l /c:"WORKSPACE" %manifest_path%') DO (
set "workspace_dir=%%~dpF"
)
REM If the buildifier isn't immediately available via short_path from
REM current directory, then parse the MANIFEST file to find buildifier.
if not exist !buildifier_path! (
for /F "tokens=2 delims= " %%F IN ('findstr /l /c:"buildifier.exe" %manifest_path%') DO (
set "buildifier_path=%%F"
)
)
if not exist !buildifier_path! (
echo Failed to find buildifier at !buildifier_path!
exit /b 1
)
if defined BUILD_WORKSPACE_DIRECTORY (
rem either a build, or a run/build inside a test
echo cd %BUILD_WORKSPACE_DIRECTORY%
cd %BUILD_WORKSPACE_DIRECTORY%
) else if defined TEST_WORKSPACE (
if defined workspace_dir (
echo cd: %workspace_dir%
cd %workspace_dir%
) else (
REM this path doesn't seem required in bazel 7.2; manifest is always present
echo set follow_links=1
set follow_links=1
)
) else (
echo Failed to find workspace to check
)
call :collectFiles .
if !files_to_analyze! == "" (
echo Failed to find files to analyze
exit /b 1
)
echo !buildifier_path! %EXTRA_ARGS% !files_to_analyze!
!buildifier_path! %EXTRA_ARGS% !files_to_analyze!
exit /b !ERRORLEVEL!
REM Utility function to collect all wanted files.
REM Exclude patterns are not supported by `dir.exe`.
:collectFiles
REM First, files. Include symlinked files when traversing runfiles.
pushd %~1
REM echo collecting in %cd%
set file_attributes=-d-l-h
if defined follow_links set file_attributes=-d-h
FOR /F %%f IN ('dir /b/a:!file_attributes! *.bzl *.sky *.bazel *.BUILD BUILD BUILD.*.oss WORKSPACE WORKSPACE.bzlmod WORKSPACE.oss WORKSPACE.*.oss 2^>nul') DO (
set FILE=%~1\%%f
REM remove .\ prefix
set FILE=!FILE:.\=!
REM echo Found %~1\%%f adding !FILE!
set files_to_analyze=!files_to_analyze! !FILE!
)
popd
REM Next, directories.
REM
REM Interestingly, turning on the exclude hidden files option (-h) excludes the
REM `.git` directory but not other directories starting with a `.` like
REM `.github` or `.bcr`. I don't know why it works this way, but hey it works.
if defined follow_links (
FOR /F %%d IN ('dir /b/a:d-h %~1 2^>nul') DO (
call :collectFiles "%~1\%%d"
)
) else (
FOR /F %%d IN ('dir /b/a:d-l-h %~1 2^>nul') DO (
call :collectFiles "%~1\%%d"
)
)