Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some usability improvements to formatting script #6153

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions extras/formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source_dir="$(dirname "$script_dir")"
since_rev=""

check_only=0
no_version_check=0
Expand All @@ -30,6 +31,7 @@ Options:
--md Format only markdown files
--sh Format only shell script files
--cmake Format only CMake files
--since <rev> Only format files since Git revision <rev>
EOF
}

Expand Down Expand Up @@ -65,6 +67,10 @@ while [[ "$#" -gt 0 ]]; do
source_dir="$2"
shift
;;
--since)
since_rev="$2"
shift
;;
*)
echo "unrecognized argument: $1"
show_help
Expand Down Expand Up @@ -108,23 +114,31 @@ require_bin() {
}

require_bin "git" "1.8"
require_bin "gersemi" "0.17"
require_bin "xargs" "3"
((run_all || run_cmake)) && require_bin "gersemi" "0.17"
((run_all || run_cpp)) && require_bin "xargs" "3"
require_bin "diff" "2"
require_bin "clang-format" "17" "18"
require_bin "prettier" "3"
require_bin "shfmt" "3"
((run_all || run_cpp)) && require_bin "clang-format" "17" "18"
((run_all || run_yaml || run_markdown)) && require_bin "prettier" "3"
((run_all || run_sh)) && require_bin "shfmt" "3"

if [ "$missing_bin" ]; then
exit 1
fi

exit_code=0

function list_files() {
if [ "$since_rev" ]; then
git diff --name-only "$since_rev" HEAD $@
else
git ls-files $@
fi
}

cmake_formatting() {
echo "Formatting CMake files..." >&2

readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
readarray -t files < <(list_files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')

common_args=(
# turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
Expand All @@ -150,7 +164,7 @@ track_progress() {
local total=$1
local current=0

while IFS= read -r _; do
((total)) && while IFS= read -r _; do
((current++)) || :
percent=$((current * 100 / total))
printf '\rProgress: [%-50s] %d%%' "$(printf '#%.0s' $(seq 1 $((percent / 2))))" "$percent" >&2
Expand All @@ -161,7 +175,7 @@ track_progress() {
cpp_formatting() {
echo "Formatting cpp files..." >&2

readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
readarray -t files < <(list_files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')

# The progress reporting is a bit sneaky, we use `--verbose` with xargs which
# prints a line to stderr for each command, and we simply count these...
Expand Down Expand Up @@ -211,23 +225,23 @@ prettier_formatting() {
yaml_json_formatting() {
echo "Formatting yaml and json files..." >&2

readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')
readarray -t files < <(list_files "*.yaml" "*.yml" "*.json" ':!external/**')

prettier_formatting
}

markdown_formatting() {
echo "Formatting markdown files..." >&2

readarray -t files < <(git ls-files "*.md" ':!external/**')
readarray -t files < <(list_files "*.md" ':!external/**')

prettier_formatting
}

sh_formatting() {
echo "Formatting sh files..." >&2

readarray -t files < <(git ls-files "*.sh")
readarray -t files < <(list_files "*.sh")

common_args=(
# default 8 is way too wide
Expand Down
Loading