mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
fix: Make fix-local-includes.sh work with multiple files (#2102)
This commit is contained in:
@@ -2,26 +2,38 @@
|
||||
|
||||
# Note: This script is intended to be run from the root of the repository.
|
||||
#
|
||||
# This script checks will fix local includes in the C++ code.
|
||||
# This script will fix local includes in the C++ code for a given file.
|
||||
# Usage: ./pre-commit-hooks/fix-local-includes.sh <file1> <file2> ...
|
||||
|
||||
file_path="$1"
|
||||
|
||||
echo "+ Fixing includes in $file_path..."
|
||||
files="$@"
|
||||
echo "+ Fixing includes in $files..."
|
||||
|
||||
GNU_SED=$(sed --version 2>&1 | grep -q 'GNU' && echo true || echo false)
|
||||
|
||||
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
|
||||
# make all includes to be <...> style
|
||||
sed -i '' -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
|
||||
|
||||
# make local includes to be "..." style
|
||||
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | tr '\n' '|' | sed 's/|$//' | sed 's/|/\\|/g')
|
||||
sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
|
||||
else
|
||||
# make all includes to be <...> style
|
||||
sed -i -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
|
||||
|
||||
# make local includes to be "..." style
|
||||
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | paste -sd '|' | sed 's/|/\\|/g')
|
||||
sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
|
||||
fi
|
||||
|
||||
fix_includes() {
|
||||
file_path="$1"
|
||||
|
||||
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
|
||||
# make all includes to be <...> style
|
||||
sed -i '' -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
|
||||
|
||||
# make local includes to be "..." style
|
||||
sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
|
||||
else
|
||||
# make all includes to be <...> style
|
||||
sed -i -E 's|#include "(.*)"|#include <\1>|g' "$file_path"
|
||||
|
||||
# make local includes to be "..." style
|
||||
sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" "$file_path"
|
||||
fi
|
||||
}
|
||||
|
||||
for file in $files; do
|
||||
fix_includes "$file"
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user