Skip to content

Commit 68270a4

Browse files
committed
feat(tools/shellfmt): accept positional file args for scoped format
When called without arguments, lib/tools/shellfmt.sh keeps its current behaviour — aggregate every .sh/.conf/.inc/.csc/.tvb under compile.sh, config/boards, config/sources, lib, and extensions and shfmt -w the lot (CI-style mass-format). When called with positional file arguments, only those files are formatted, filtered against the same extension allowlist so a stray .py / .md / .yml doesn't sneak through. .editorconfig still drives the actual rules — shfmt picks it up regardless. This lets editor-driven workflows (post-edit hooks, agent helpers) scope the format to the file the user just touched without dragging the whole tree into the next commit. Previously the wrapper silently ignored file arguments and reformatted everything. Verified: - bash lib/tools/shellfmt.sh → full-tree run as before - bash lib/tools/shellfmt.sh extensions/apa.sh → only apa.sh formatted - bash lib/tools/shellfmt.sh foo.py extensions/apa.sh → .py skipped (unsupported extension), apa.sh formatted.
1 parent e8b4906 commit 68270a4

1 file changed

Lines changed: 51 additions & 7 deletions

File tree

lib/tools/shellfmt.sh

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,64 @@ fi
5858
ACTUAL_VERSION="$("${SHELLFMT_BIN}" -version)"
5959
echo "Running shellfmt ${ACTUAL_VERSION}"
6060

61+
# Resolve positional args to absolute paths BEFORE chdir, so callers
62+
# from sub-directories can pass relative paths and have them keep
63+
# meaning. Avoid `realpath -m` — its `-m` flag is GNU-only and breaks
64+
# on the macOS realpath(1) the script's darwin branch otherwise
65+
# supports. Plain `$PWD/path` prefix is enough; shfmt doesn't care
66+
# about symlink canonicalisation or `..` normalisation, and the
67+
# missing-file branch below still triggers on dangling paths.
68+
declare -a SCOPED_ARGS=()
69+
for _f in "$@"; do
70+
if [[ "${_f}" == /* ]]; then
71+
SCOPED_ARGS+=("${_f}")
72+
else
73+
SCOPED_ARGS+=("${PWD}/${_f}")
74+
fi
75+
done
76+
6177
cd "${SRC}"
6278
#"${SHELLFMT_BIN}" --help
6379
#"${SHELLFMT_BIN}" -f "${SRC}"
6480
#"${SHELLFMT_BIN}" -d "${SRC}"
6581

6682
# Should match the .editorconfig
6783

68-
# Aggregate all files that should be formatted
69-
board_config_files=$(find config/boards -type f \( -name "*.conf" -o -name "*.csc" -o -name "*.tvb" \)) # All board config files
70-
family_config_files=$(find config/sources -type f \( -name "*.conf" -o -name "*.inc" -o -name "*.sh" \)) # All family config files
71-
lib_files=$(find lib -type f -name "*.sh") # All build framework shell files
72-
extensions_files=$(find extensions -type f -name "*.sh") # All extension shell files
73-
74-
declare -a ALL_BASH_FILES=(compile.sh ${board_config_files} ${family_config_files} ${lib_files} ${extensions_files})
84+
# Allowed file extensions for both full-tree and scoped modes; keep
85+
# in sync with the find globs below.
86+
ALLOWED_EXT_RE='\.(sh|conf|inc|csc|tvb)$'
87+
88+
declare -a ALL_BASH_FILES=()
89+
90+
if ((${#SCOPED_ARGS[@]} > 0)); then
91+
# Scoped mode: only format the files passed on the command line.
92+
# Skip anything that doesn't match the allowed extensions so a
93+
# stray Python/Markdown/YAML argument doesn't sneak through.
94+
echo -e "\nScoped mode: formatting only the files passed on argv"
95+
for f in "${SCOPED_ARGS[@]}"; do
96+
if [[ ! -e "${f}" ]]; then
97+
echo " skip (missing): ${f}"
98+
continue
99+
fi
100+
if [[ ! "${f}" =~ ${ALLOWED_EXT_RE} ]]; then
101+
echo " skip (unsupported extension): ${f}"
102+
continue
103+
fi
104+
ALL_BASH_FILES+=("${f}")
105+
done
106+
if ((${#ALL_BASH_FILES[@]} == 0)); then
107+
echo "No eligible files after filtering — nothing to do."
108+
exit 0
109+
fi
110+
else
111+
# Full-tree mode: aggregate every file the framework cares about.
112+
board_config_files=$(find config/boards -type f \( -name "*.conf" -o -name "*.csc" -o -name "*.tvb" \)) # All board config files
113+
family_config_files=$(find config/sources -type f \( -name "*.conf" -o -name "*.inc" -o -name "*.sh" \)) # All family config files
114+
lib_files=$(find lib -type f -name "*.sh") # All build framework shell files
115+
extensions_files=$(find extensions -type f -name "*.sh") # All extension shell files
116+
117+
ALL_BASH_FILES=(compile.sh ${board_config_files} ${family_config_files} ${lib_files} ${extensions_files})
118+
fi
75119

76120
echo -e "\nAll files:" "${ALL_BASH_FILES[@]}"
77121

0 commit comments

Comments
 (0)