forked from exercism/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-stub-files.sh
More file actions
executable file
·35 lines (33 loc) · 1.57 KB
/
Copy pathmake-stub-files.sh
File metadata and controls
executable file
·35 lines (33 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash
#
# Helper script to fix up incorrect namespaces and add stub files.
exercises_dir=$(cd "$(dirname "$0")/../exercises" && pwd)
for exercise_dir in "${exercises_dir}"/*; do
cd "$exercise_dir" || exit 1
exercise_name=$(basename "$(pwd)" | sed 's/-/_/g')
# Rename incorrect namespaces
if ! grep -q "namespace $exercise_name" ./*.cpp ./*.h; then
echo "Cleaning up $exercise_name"
old=$(grep '^namespace [[:lower:]_]*' *.h --no-filename --max-count 1 --only-matching | head -1 | cut -d ' ' -f2)
find . -name "*.h" -exec sed -i "s/namespace $old/namespace $exercise_name/g" {} \;
find . -name "*.cpp" -exec sed -i "s/namespace $old/namespace $exercise_name/g" {} \;
find . -name "*.h" -exec sed -i "s/$old::/$exercise_name::/g" {} \;
find . -name "*.cpp" -exec sed -i "s/$old::/$exercise_name::/g" {} \;
git commit * -m "$exercise_name: Fix-up namespace"
fi
# Add stubs if they don't exist
header="$exercise_name.h"
source="$exercise_name.cpp"
if ! test -f "$header"; then
printf "#if !defined(${exercise_name^^}_H)\n" >> $header
printf "#define ${exercise_name^^}_H\n" >> $header
printf "\nnamespace $exercise_name {\n\n} // namespace $exercise_name\n" >> $header
printf "\n#endif // ${exercise_name^^}_H" >> $header
git add $header
fi
if ! test -f "$source"; then
printf "#include \"$header\"\n" >> $source
printf "\nnamespace $exercise_name {\n\n} // namespace $exercise_name\n" >> $source
git add $source
fi
done