-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathformat-coding.sh
More file actions
executable file
·83 lines (68 loc) · 2.08 KB
/
format-coding.sh
File metadata and controls
executable file
·83 lines (68 loc) · 2.08 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
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2026 Intel Corporation
# Format all code: C/C++ (clang-format), Python (isort/black),
# Markdown (markdownlint), Shell (shfmt).
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
pushd "$SCRIPT_DIR" >/dev/null
command_exists() { command -v "$1" >/dev/null 2>&1; }
# ==========================
# C/C++ — clang-format
# ==========================
CLANG_FORMAT_TOOL=${CLANG_FORMAT_TOOL:-clang-format-14}
echo "=== clang-format ==="
mapfile -t CLANG_FORMAT_FILES < <(
git ls-files --cached --others --exclude-standard \
-- '*.cpp' '*.hpp' '*.cc' '*.c' '*.h' \
':!:pymtl_wrap.c' ':!:vmlinux.h'
)
if ((${#CLANG_FORMAT_FILES[@]})); then
printf '%s\0' "${CLANG_FORMAT_FILES[@]}" |
xargs -0 -n32 -P"$(nproc)" "${CLANG_FORMAT_TOOL}" -i
echo "Formatted ${#CLANG_FORMAT_FILES[@]} C/C++ files."
fi
# ==========================
# Python — isort + black
# ==========================
echo ""
echo "=== Python (isort + black) ==="
if command_exists isort && command_exists black; then
isort python/ tests/
black python/ tests/
else
echo "isort/black not found — skipping Python formatting."
echo "please use: pip install 'black==24.4.0' 'isort==5.13.2'"
fi
# ==========================
# Markdown — markdownlint
# ==========================
echo ""
echo "=== markdownlint ==="
mapfile -t MD_FILES < <(git ls-files '*.md')
if ((${#MD_FILES[@]})); then
if command_exists npx; then
npx --yes markdownlint-cli@0.43.0 \
--config "$SCRIPT_DIR/.github/linters/.markdown-lint.yml" \
--fix "${MD_FILES[@]}" || true
else
echo "npx not found — skipping. Install with: sudo apt-get install npm"
fi
fi
# ==========================
# Shell — shfmt
# ==========================
echo ""
echo "=== shfmt ==="
mapfile -t SH_FILES < <(git ls-files '*.sh')
if ((${#SH_FILES[@]})); then
if command_exists shfmt; then
shfmt -w "${SH_FILES[@]}"
echo "Formatted ${#SH_FILES[@]} shell scripts."
else
echo "shfmt not found — skipping. Install with: sudo apt-get install shfmt"
fi
fi
popd >/dev/null
echo ""
echo "Done."