Skip to content

Commit e3e6bc0

Browse files
committed
refactor: update format.sh
1 parent 124a590 commit e3e6bc0

File tree

7 files changed

+241
-2
lines changed

7 files changed

+241
-2
lines changed

format.sh

Lines changed: 241 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,242 @@
11
#!/bin/sh
2-
find src -name "*.java" -print0 | xargs -0 -n 500 java -jar google-java-format-1.28.0-all-deps.jar --replace
3-
find . \( -name "*.yml" -o -name "*.yaml" \) -print0 | xargs -0 ./yamlfmt
2+
3+
# Formats Java and YAML files using Google Java Format and yamlfmt
4+
# Supports Linux, macOS, and Windows across multiple architectures
5+
6+
set -e
7+
8+
# Security: Ensure we're in the project directory
9+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
10+
cd "$SCRIPT_DIR" || exit 1
11+
12+
YAMLFMT_DIR="yamllint"
13+
JAVA_FORMATTER="google-java-format-1.28.0-all-deps.jar"
14+
JAVA_SOURCE_DIR="src"
15+
16+
if [ -t 1 ]; then
17+
RED='\033[0;31m'
18+
GREEN='\033[0;32m'
19+
YELLOW='\033[1;33m'
20+
BLUE='\033[0;34m'
21+
NC='\033[0m'
22+
else
23+
RED=''
24+
GREEN=''
25+
YELLOW=''
26+
BLUE=''
27+
NC=''
28+
fi
29+
30+
log_info() {
31+
printf "${BLUE}[INFO]${NC} %s\n" "$1"
32+
}
33+
34+
log_success() {
35+
printf "${GREEN}[SUCCESS]${NC} %s\n" "$1"
36+
}
37+
38+
log_warning() {
39+
printf "${YELLOW}[WARNING]${NC} %s\n" "$1"
40+
}
41+
42+
log_error() {
43+
printf "${RED}[ERROR]${NC} %s\n" "$1"
44+
}
45+
46+
detect_yamlfmt_binary() {
47+
OS=$(uname -s)
48+
ARCH=$(uname -m)
49+
50+
case "$OS" in
51+
Linux)
52+
case "$ARCH" in
53+
x86_64|amd64)
54+
echo "yamlfmt-linux-x86"
55+
;;
56+
aarch64|arm64)
57+
echo "yamlfmt-linux-arm64"
58+
;;
59+
i386|i686)
60+
log_error "32-bit Linux is not supported"
61+
echo "unsupported"
62+
;;
63+
*)
64+
log_error "Unsupported Linux architecture: $ARCH"
65+
echo "unsupported"
66+
;;
67+
esac
68+
;;
69+
Darwin)
70+
case "$ARCH" in
71+
x86_64|amd64)
72+
echo "yamlfmt-darwin-x86"
73+
;;
74+
arm64)
75+
echo "yamlfmt-darwin-arm64"
76+
;;
77+
*)
78+
log_error "Unsupported macOS architecture: $ARCH"
79+
echo "unsupported"
80+
;;
81+
esac
82+
;;
83+
MINGW*|MSYS*|CYGWIN*)
84+
case "$ARCH" in
85+
x86_64|amd64)
86+
echo "yamlfmt-x86.exe"
87+
;;
88+
aarch64|arm64)
89+
echo "yamlfmt-arm64.exe"
90+
;;
91+
*)
92+
log_error "Unsupported Windows architecture: $ARCH"
93+
echo "unsupported"
94+
;;
95+
esac
96+
;;
97+
*)
98+
log_error "Unsupported operating system: $OS"
99+
echo "unsupported"
100+
;;
101+
esac
102+
}
103+
104+
validate_environment() {
105+
if [ ! -d "$YAMLFMT_DIR" ]; then
106+
log_error "yamlfmt directory not found: $YAMLFMT_DIR"
107+
return 1
108+
fi
109+
110+
if [ -L "$YAMLFMT_DIR" ]; then
111+
log_error "Security: $YAMLFMT_DIR is a symbolic link, which is not allowed"
112+
return 1
113+
fi
114+
115+
if [ ! -f "$JAVA_FORMATTER" ]; then
116+
log_error "Java formatter not found: $JAVA_FORMATTER"
117+
return 1
118+
fi
119+
120+
if [ ! -d "$JAVA_SOURCE_DIR" ]; then
121+
log_warning "Java source directory not found: $JAVA_SOURCE_DIR"
122+
log_warning "Skipping Java formatting"
123+
return 2
124+
fi
125+
126+
return 0
127+
}
128+
129+
format_java() {
130+
log_info "Formatting Java files..."
131+
132+
java_files=$(find "$JAVA_SOURCE_DIR" -type f -name "*.java" 2>/dev/null | wc -l)
133+
134+
if [ "$java_files" -eq 0 ]; then
135+
log_warning "No Java files found in $JAVA_SOURCE_DIR"
136+
return 0
137+
fi
138+
139+
log_info "Found $java_files Java file(s)"
140+
141+
if ! find "$JAVA_SOURCE_DIR" -type f -name "*.java" -print0 | \
142+
xargs -0 -n 500 java -jar "$JAVA_FORMATTER" --replace 2>&1; then
143+
log_error "Java formatting failed"
144+
return 1
145+
fi
146+
147+
log_success "Java files formatted successfully"
148+
return 0
149+
}
150+
151+
format_yaml() {
152+
local yamlfmt_binary="$1"
153+
local yamlfmt_path="${YAMLFMT_DIR}/${yamlfmt_binary}"
154+
155+
log_info "Formatting YAML files..."
156+
157+
yamlfmt_realpath=$(cd "$(dirname "$yamlfmt_path")" && pwd)/$(basename "$yamlfmt_path")
158+
expected_dir=$(cd "$YAMLFMT_DIR" && pwd)
159+
160+
case "$yamlfmt_realpath" in
161+
"$expected_dir/"*)
162+
;;
163+
*)
164+
log_error "Security: yamlfmt binary path traversal detected"
165+
return 1
166+
;;
167+
esac
168+
169+
if [ ! -f "$yamlfmt_path" ]; then
170+
log_error "yamlfmt binary not found: $yamlfmt_path"
171+
log_info "Available binaries in $YAMLFMT_DIR:"
172+
ls -1 "$YAMLFMT_DIR"/yamlfmt-* 2>/dev/null || log_info " None found"
173+
return 1
174+
fi
175+
176+
if [ -L "$yamlfmt_path" ]; then
177+
log_error "Security: yamlfmt binary is a symbolic link, which is not allowed"
178+
return 1
179+
fi
180+
181+
chmod +x "$yamlfmt_path" 2>/dev/null || {
182+
log_warning "Could not set executable permissions on $yamlfmt_path"
183+
}
184+
185+
yaml_files=$(find . -type f \( -name "*.yml" -o -name "*.yaml" \) ! -path "./.git/*" ! -path "./.*" 2>/dev/null | wc -l)
186+
187+
if [ "$yaml_files" -eq 0 ]; then
188+
log_warning "No YAML files found"
189+
return 0
190+
fi
191+
192+
log_info "Found $yaml_files YAML file(s)"
193+
194+
if ! find . -type f \( -name "*.yml" -o -name "*.yaml" \) \
195+
! -path "./.git/*" ! -path "./.*" \
196+
-print0 | xargs -0 "$yamlfmt_path" 2>&1; then
197+
log_error "YAML formatting failed"
198+
return 1
199+
fi
200+
201+
log_success "YAML files formatted successfully"
202+
return 0
203+
}
204+
205+
main() {
206+
log_info "Starting code formatting for the current project"
207+
log_info "Platform: $(uname -s) $(uname -m)"
208+
209+
validate_result=0
210+
validate_environment || validate_result=$?
211+
212+
if [ $validate_result -eq 1 ]; then
213+
log_error "Environment validation failed"
214+
exit 1
215+
fi
216+
217+
YAMLFMT_BINARY=$(detect_yamlfmt_binary)
218+
219+
if [ "$YAMLFMT_BINARY" = "unsupported" ]; then
220+
log_warning "Platform not supported for yamlfmt"
221+
log_warning "OS: $(uname -s), Architecture: $(uname -m)"
222+
log_warning "Skipping YAML formatting, but continuing with Java formatting"
223+
224+
if [ $validate_result -ne 2 ]; then
225+
format_java || exit 1
226+
fi
227+
exit 0
228+
fi
229+
230+
log_info "Using yamlfmt binary: $YAMLFMT_BINARY"
231+
232+
if [ $validate_result -ne 2 ]; then
233+
format_java || exit 1
234+
fi
235+
236+
format_yaml "$YAMLFMT_BINARY" || exit 1
237+
238+
echo ""
239+
log_success "All formatting completed successfully"
240+
}
241+
242+
main "$@"

yamllint/yamlfmt-arm64.exe

4.04 MB
Binary file not shown.

yamllint/yamlfmt-darwin-arm64

4.07 MB
Binary file not shown.

yamllint/yamlfmt-darwin-x86

4.28 MB
Binary file not shown.

yamllint/yamlfmt-linux-arm64

4 MB
Binary file not shown.
File renamed without changes.

yamllint/yamlfmt-x86.exe

4.34 MB
Binary file not shown.

0 commit comments

Comments
 (0)