Commit b4a37435 authored by Ben Boeckel's avatar Ben Boeckel

format.all: add a tool to run all formatters

parent 0128bd1e
Pipeline #47534 passed with stage
......@@ -48,3 +48,36 @@ endfunction ()
add_subdirectory(c)
add_subdirectory(python)
add_subdirectory(rust)
get_property(formatters GLOBAL
PROPERTY formatters)
if (formatters)
set(formatters_build)
set(formatters_install)
foreach (formatter IN LISTS formatters)
if (NOT ENABLE_${formatter})
continue ()
endif ()
get_property("format_${formatter}_script" GLOBAL
PROPERTY "formatter_${formatter}_script")
string(APPEND formatters_build "format_all '${formatter}' '${format_${formatter}_script}'\n")
string(APPEND formatters_install "format_all '${formatter}' 'format.${formatter}'\n")
endforeach ()
set(formatter_calls "${formatters_build}")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/format.all.in"
"${CMAKE_BINARY_DIR}/bin/format.all"
@ONLY)
set(formatter_calls "${formatters_install}")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/format.all.in"
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/format.all"
@ONLY)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/format.all"
DESTINATION "bin"
COMPONENT "formatters")
endif ()
#!/bin/sh
# This program formats a repository according to the formatting scripts
# available in the current build.
#
# This is really meant to be copied as an example and tweaked to be more
# specific to a project and then used to make a clean branch for the project
# after which, code may use the formatters to check new code for unformatted
# code.
set -e
if ! which "parallel" > /dev/null; then
echo >&2 'error: could not find the `parallel` binary'
exit 3
fi
if ! git rev-parse --is-inside-work-tree; then
echo >&2 'error: this must be run from inside of a Git work tree'
exit 1
fi
# Move to the top of the work tree.
cd "$( git rev-parse --show-toplevel )"
format_all () {
local kind="$1"
readonly kind
shift
local formatter="$1"
readonly formatter
shift
git ls-files | \
git check-attr --stdin "format.$kind" | \
sed -n -e '/: set$/ s/:[^:]*:[^:]*$//p' | \
parallel "$formatter" '{}'
}
@formatter_calls@
:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment