Skip to content

Commit

Permalink
Merge pull request #208 from rest-for-physics/205-format-rml-files-in…
Browse files Browse the repository at this point in the history
…-a-similar-way-as-clang-format

Format rml files in a similar way as clang format
  • Loading branch information
lobis authored May 13, 2022
2 parents c4dd136 + 43df942 commit e20fc0e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
16 changes: 16 additions & 0 deletions cmake/GitHooks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ endif ()
# We cannot write the file from here because we need exec permissions
configure_file(${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit)

find_package(Git)

if(Git_FOUND)
message(STATUS "${GIT_EXECUTABLE} submodule foreach git rev-parse --git-path hooks")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule --quiet foreach "git rev-parse --git-path hooks" OUTPUT_VARIABLE SUBMODULE_OUTPUT)
string(REPLACE "\n" ";" SUBMODULE_OUTPUT ${SUBMODULE_OUTPUT})
set(SUBMODULE_HOOK ${SUBMODULE_OUTPUT})
foreach(HOOK_DIR ${SUBMODULE_HOOK})
if (NOT EXISTS ${HOOK_DIR}/pre-commit)
configure_file(${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in ${HOOK_DIR}/pre-commit)
message(STATUS "Installing ${CMAKE_SOURCE_DIR}/cmake/utils/git_pre-commit.in in ${HOOK_DIR}/pre-commit")
endif()
endforeach()
else ()
message (WARNING "Git not found")
endif ()
16 changes: 8 additions & 8 deletions cmake/utils/git_pre-commit.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash

STYLE=$PWD/.clang-format
echo "$STYLE"
if [ -n "$STYLE" ]; then
STYLEARG="-style=file"
else
STYLEARG=""
gitCommand=$(git rev-parse --show-superproject-working-tree)
if [ -z $gitCommand ]; then
gitCommand=$(git rev-parse --show-toplevel)
fi

gitPath=$(readlink -f $gitCommand)
reformatCommand="bash $gitPath/scripts/reformat-files.sh"

format_file() {
file="$1"
if [ -f $file ]; then
clang-format -i $STYLEARG $1
$reformatCommand $1
git add $1
fi
}
Expand All @@ -21,7 +21,7 @@ case "${1}" in
echo "Runs clang-format on source files"
;;
*)
for file in $(git diff-index --cached --name-only HEAD | grep -iE '\.(cxx|cc|h|C)$'); do
for file in $(git diff-index --cached --name-only HEAD | grep -iE '\.(cxx|cc|h|C|rml)$'); do
format_file "$file"
done
;;
Expand Down
35 changes: 26 additions & 9 deletions scripts/reformat-clang.sh → scripts/reformat-files.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,56 @@
# Change this if your clang-format executable is somewhere else

CLANG_FORMAT="clang-format"
XML_LINT="xmllint"

if [ $# -eq 0 ]; then
echo ' '
echo 'This script formats the source directory /path/to/source/'
echo 'given as the only argument. Only *.h, *.cxx, *.cc and *.C files will be '
echo 'reformatted.'
echo 'given as the only argument. Only *.h, *.cxx, *.cc, *.C and *.rml files will be reformatted.'
echo ' '
echo 'The formatting will consider the .clang-format file closer to'
echo 'the source directory given.'
echo 'The formatting will consider the .clang-format file closer to the source directory given.'
echo ' '
echo 'Usage: ./reformat-clang.sh /path/to/source/'
echo 'Usage: ./reformat-files.sh /path/to/source/'
echo ' '
echo 'Use carefully! :)'

exit 1
fi

echo "Begin formatting!"

if ! type -P $CLANG_FORMAT; then
echo "WARNING: '$CLANG_FORMAT' was not found in your system! we cannot format code files"
CLANG_FORMAT_DISABLED=True
fi

if ! type -P $XML_LINT; then
echo "WARNING: '$XML_LINT' was not found in your system! we cannot format *.rml files"
XML_LINT_DISABLED=True
fi

pathToFormat=$(readlink -f $1)

if [ -d "$pathToFormat" ]; then
for DIRECTORY in $pathToFormat; do
echo "Formatting code under $DIRECTORY/"
find "$DIRECTORY" \( -name '*.h' -or -name '*.cxx' -or -name '*.cc' -or -name '*.C' \) -print0 | xargs -0 "$CLANG_FORMAT" -i
echo "DONE!"
if [[ -z "$CLANG_FORMAT_DISABLED" ]]; then
find "$DIRECTORY" \( -name '*.h' -or -name '*.cxx' -or -name '*.cc' -or -name '*.C' \) -print0 | xargs -0 "$CLANG_FORMAT" -i
fi
if [[ -z "$XML_LINT_DISABLED" ]]; then
find "$DIRECTORY" -name "*.rml" -type f -exec $XML_LINT --output '{}' --format '{}' \;
fi
echo "Done formatting all files in '$DIRECTORY'"
done
elif [ -f "$pathToFormat" ]; then
echo "Formatting file \"$pathToFormat\""
ext="${pathToFormat##*.}"
if [[ "$ext" == "h" || "$ext" == "cxx" || "$ext" == "cc" || "$ext" == "C" ]]; then
echo "$CLANG_FORMAT -i $pathToFormat"
eval "$CLANG_FORMAT -i $pathToFormat"
elif [[ "$ext" == "rml" ]]; then
$XML_LINT --output "$pathToFormat" --format "$pathToFormat"
else
echo "Not valid extension $ext, valid extensions are *.h, *.cxx, *.cc and *.C"
echo "Not valid extension $ext, valid extensions are *.h, *.cxx, *.cc, *.C or *.rml"
fi
else
echo "$pathToFormat is not valid file or directory"
Expand Down

0 comments on commit e20fc0e

Please sign in to comment.