11@ echo off
22setlocal enabledelayedexpansion
33
4- REM Formats Java and YAML files using Google Java Format and yamlfmt
4+ REM Formats Java, YAML, and Shell files using Google Java Format, yamlfmt, and shfmt
55REM Windows-native batch script
66
77REM Security: Ensure we're in the project directory
88cd /d " %~dp0 " || exit /b 1
99
1010set " YAMLFMT_DIR = yamllint"
11+ set " SHFMT_DIR = shfmt"
1112set " JAVA_FORMATTER = google-java-format-1.28.0-all-deps.jar"
1213set " JAVA_SOURCE_DIR = src"
1314set " EXIT_CODE = 0"
@@ -60,12 +61,30 @@ goto :main
6061 )
6162 exit /b 0
6263
64+ :detect_shfmt_binary
65+ set " ARCH = %PROCESSOR_ARCHITECTURE% "
66+
67+ if /i " %ARCH% " == " AMD64" (
68+ set " SHFMT_BINARY = shfmt_v3.12.0_windows_amd64.exe"
69+ ) else if /i " %ARCH% " == " X86" (
70+ set " SHFMT_BINARY = shfmt_v3.12.0_windows_386.exe"
71+ ) else (
72+ call :log_error " Unsupported Windows architecture for shfmt: %ARCH% "
73+ set " SHFMT_BINARY = unsupported"
74+ )
75+ exit /b 0
76+
6377:validate_environment
6478 if not exist " %YAMLFMT_DIR% " (
6579 call :log_error " yamlfmt directory not found: %YAMLFMT_DIR% "
6680 exit /b 1
6781 )
6882
83+ if not exist " %SHFMT_DIR% " (
84+ call :log_warning " shfmt directory not found: %SHFMT_DIR% "
85+ call :log_warning " Skipping shell script formatting"
86+ )
87+
6988 if not exist " %JAVA_FORMATTER% " (
7089 call :log_error " Java formatter not found: %JAVA_FORMATTER% "
7190 exit /b 1
@@ -186,6 +205,45 @@ goto :main
186205 call :log_success " YAML files formatted successfully"
187206 exit /b 0
188207
208+ :format_shell
209+ set " SHFMT_PATH = %SHFMT_DIR% \%SHFMT_BINARY% "
210+
211+ call :log_info " Formatting shell script files..."
212+
213+ if not exist " %SHFMT_PATH% " (
214+ call :log_error " shfmt binary not found: %SHFMT_PATH% "
215+ call :log_info " Available binaries in %SHFMT_DIR% :"
216+ dir /b " %SHFMT_DIR% \shfmt_*" 2 > nul || call :log_info " None found"
217+ exit /b 1
218+ )
219+
220+ REM Count shell script files
221+ set " SHELL_FILE_COUNT = 0"
222+ for /r %%f in (*.sh *.bash) do (
223+ set " FILEPATH = %%f "
224+ echo !FILEPATH! | findstr /i /c:" .git" > nul
225+ if errorlevel 1 (
226+ set /a SHELL_FILE_COUNT += 1
227+ )
228+ )
229+
230+ if %SHELL_FILE_COUNT% equ 0 (
231+ call :log_warning " No shell script files found"
232+ exit /b 0
233+ )
234+
235+ call :log_info " Found %SHELL_FILE_COUNT% shell script file(s)"
236+
237+ REM Format shell scripts using shfmt -l -w .
238+ " %SHFMT_PATH% " -l -w . 2 >& 1
239+ if errorlevel 1 (
240+ call :log_error " Shell script formatting failed"
241+ exit /b 1
242+ )
243+
244+ call :log_success " Shell script files formatted successfully"
245+ exit /b 0
246+
189247:main
190248 call :log_info " Starting code formatting for the current project"
191249
@@ -201,28 +259,40 @@ goto :main
201259 )
202260
203261 call :detect_yamlfmt_binary
262+ call :detect_shfmt_binary
204263
205264 if " %YAMLFMT_BINARY% " == " unsupported" (
206265 call :log_warning " Platform not supported for yamlfmt"
207266 call :log_warning " Architecture: %PROCESSOR_ARCHITECTURE% "
208- call :log_warning " Skipping YAML formatting, but continuing with Java formatting"
209-
210- if %VALIDATE_RESULT% neq 2 (
211- call :format_java
212- if errorlevel 1 exit /b 1
213- )
214- exit /b 0
267+ call :log_warning " Skipping YAML formatting"
268+ ) else (
269+ call :log_info " Using yamlfmt binary: %YAMLFMT_BINARY% "
215270 )
216271
217- call :log_info " Using yamlfmt binary: %YAMLFMT_BINARY% "
272+ if " %SHFMT_BINARY% " == " unsupported" (
273+ call :log_warning " Platform not supported for shfmt"
274+ call :log_warning " Architecture: %PROCESSOR_ARCHITECTURE% "
275+ call :log_warning " Skipping shell script formatting"
276+ ) else (
277+ call :log_info " Using shfmt binary: %SHFMT_BINARY% "
278+ )
218279
219280 if %VALIDATE_RESULT% neq 2 (
220281 call :format_java
221282 if errorlevel 1 exit /b 1
222283 )
223284
224- call :format_yaml
225- if errorlevel 1 exit /b 1
285+ if not " %YAMLFMT_BINARY% " == " unsupported" (
286+ call :format_yaml
287+ if errorlevel 1 exit /b 1
288+ )
289+
290+ if not " %SHFMT_BINARY% " == " unsupported" (
291+ if exist " %SHFMT_DIR% " (
292+ call :format_shell
293+ if errorlevel 1 exit /b 1
294+ )
295+ )
226296
227297 echo .
228298 call :log_success " All formatting completed successfully"
0 commit comments