Skip to content

Commit c3d55f5

Browse files
authored
fix: make the formatter format in parallel using rust-parallel if available (#295)
1 parent c000528 commit c3d55f5

File tree

7 files changed

+215
-58
lines changed

7 files changed

+215
-58
lines changed

scripts/fmt

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,27 @@ TOP_DIR=$(git rev-parse --show-toplevel)
3030
# Add license header to all files that don't already have it.
3131
"${TOP_DIR}/scripts/add_license"
3232

33-
# Format all TOML files.
34-
"${TOP_DIR}/scripts/format_toml_files"
35-
if [[ $? -ne 0 ]]; then
36-
exit 1
37-
fi
38-
39-
# Format all Python code and organize imports.
40-
uv run --directory "${TOP_DIR}/python" ruff check --select I --fix --preview --unsafe-fixes .
41-
uv run --directory "${TOP_DIR}/python" ruff format --preview .
42-
if [[ $? -ne 0 ]]; then
43-
exit 1
44-
fi
45-
46-
# Modernize and format all Go code.
47-
go mod -C "${TOP_DIR}/go" tidy -go="${GO_VERSION}"
48-
go fmt -C "${TOP_DIR}/go" ./...
49-
if [[ $? -ne 0 ]]; then
50-
exit 1
51-
fi
52-
53-
# Format all TypeScript code.
54-
pushd "${TOP_DIR}"
55-
pnpm run format
56-
if [[ $? -ne 0 ]]; then
57-
exit 1
58-
fi
59-
popd
60-
61-
# Format handlebarrz
62-
pushd "${TOP_DIR}/python/handlebarrz"
63-
cargo fmt --all
64-
if [[ $? -ne 0 ]]; then
65-
exit 1
66-
fi
67-
popd
68-
69-
# Format all rust code
70-
pushd "${TOP_DIR}"
71-
cargo fmt --all
72-
if [[ $? -ne 0 ]]; then
73-
exit 1
74-
fi
75-
popd
76-
77-
# Format all Java code.
78-
pushd "${TOP_DIR}"
79-
"${TOP_DIR}/scripts/format_java_files"
80-
if [[ $? -ne 0 ]]; then
81-
exit 1
82-
fi
83-
popd
84-
85-
# Format all build files.
86-
if ! command -v "buildifier" >/dev/null 2>&1; then
87-
echo "Buildifier not found. Installing via go install..."
88-
go install github.com/bazelbuild/buildtools/buildifier@latest
89-
fi
90-
buildifier -r "${TOP_DIR}"
33+
if command -v rust-parallel >/dev/null 2>&1; then
34+
echo "Using rust-parallel to format files in parallel"
35+
printf "%s\n" \
36+
"${TOP_DIR}/scripts/format_go_files" \
37+
"${TOP_DIR}/scripts/format_java_files" \
38+
"${TOP_DIR}/scripts/format_rust_files" \
39+
"${TOP_DIR}/scripts/format_python_files" \
40+
"${TOP_DIR}/scripts/format_handlebarrz_files" \
41+
"${TOP_DIR}/scripts/format_toml_files" \
42+
"${TOP_DIR}/scripts/format_ts_files" \
43+
| rust-parallel -s --exit-on-error || exit 1
44+
else
45+
echo "rust-parallel was not detected; not performing formatting in parallel"
46+
"${TOP_DIR}/scripts/format_go_files" || exit 1
47+
"${TOP_DIR}/scripts/format_java_files" || exit 1
48+
"${TOP_DIR}/scripts/format_python_files" || exit 1
49+
"${TOP_DIR}/scripts/format_handlebarrz_files" || exit 1
50+
"${TOP_DIR}/scripts/format_rust_files" || exit 1
51+
"${TOP_DIR}/scripts/format_toml_files" || exit 1
52+
"${TOP_DIR}/scripts/format_ts_files" || exit 1
53+
fi
54+
55+
# Do this after all of the above have completed.
56+
"${TOP_DIR}/scripts/format_build_files" || exit 1

scripts/format_build_files

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
TOP_DIR=$(git rev-parse --show-toplevel)
27+
28+
if ! command -v "buildifier" >/dev/null 2>&1; then
29+
echo "Buildifier not found. Installing via go install..."
30+
go install github.com/bazelbuild/buildtools/buildifier@latest
31+
fi
32+
33+
buildifier -r "${TOP_DIR}"

scripts/format_go_files

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
GO_VERSION="1.23.8"
27+
28+
TOP_DIR=$(git rev-parse --show-toplevel)
29+
GO_DIR="$TOP_DIR/go"
30+
31+
# Modernize and format all Go code.
32+
go mod -C "${GO_DIR}" tidy -go="${GO_VERSION}" || exit 1
33+
go fmt -C "${GO_DIR}" ./... || exit 1

scripts/format_handlebarrz_files

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
TOP_DIR=$(git rev-parse --show-toplevel)
27+
28+
# Format handlebarrz
29+
pushd "${TOP_DIR}/python/handlebarrz"
30+
cargo fmt --all || exit 1
31+
popd

scripts/format_python_files

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
TOP_DIR=$(git rev-parse --show-toplevel)
27+
PY_DIR="${TOP_DIR}/python"
28+
29+
# Format all Python code and organize imports.
30+
uv run --directory "${PY_DIR}" ruff check --select I --fix --preview --unsafe-fixes . || exit 1
31+
uv run --directory "${PY_DIR}" ruff format --preview . || exit 1

scripts/format_rust_files

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
TOP_DIR=$(git rev-parse --show-toplevel)
27+
28+
# Format all Python code and organize imports.
29+
30+
pushd "${TOP_DIR}"
31+
cargo fmt --all || exit 1
32+
popd

scripts/format_ts_files

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
18+
#set -x # Uncomment to enable tracing.
19+
set -euo pipefail
20+
21+
if ((EUID == 0)); then
22+
echo "Please do not run as root"
23+
exit
24+
fi
25+
26+
TOP_DIR=$(git rev-parse --show-toplevel)
27+
28+
# Format all TypeScript code.
29+
pushd "${TOP_DIR}"
30+
pnpm run format || exit 1
31+
popd

0 commit comments

Comments
 (0)