-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp_refactor
executable file
·81 lines (67 loc) · 3.26 KB
/
cpp_refactor
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# Author: Seth A. Yoder 6/15/2013, the day I got tired of manually renaming things.
# Description: Refactoring tool for c++ projects. Stick this script in the top level of your project directory. I'm
# currently assuming your makefile is there, and assuming it has "akefile" in its name, and that there is
# only one file named as such.
# I haven't checked against every edge case. Back up your files before you use this. However, if anything goes wrong,
# nothing should happen besides compiler errors.
#make clean
echo "1) Change a class name and all references"
echo -e "2) Change a file name and all corresponding \043include statements"
read -p "Choose an option, or enter anything else to quit: " ANSWER
if [ $ANSWER == "1" ]; then
echo "Please enter the original class/function name: "
read -e CLASSNAME
echo "Please enter your new desired class/function name: "
read -e NEWNAME
ANYMODIFIED=false
for FILE in $(find . -type f| grep --color "\(\.\(cpp\|h\)\|Makefile\)"); do
if $(grep -Eq " $CLASSNAME([^a-zA-Z]|$)" $FILE) || $(grep -Eq "[^a-zA-Z|^]$CLASSNAME[^a-zA-Z]" $FILE) ; then
sed -i -re "s/ $CLASSNAME([^a-zA-Z]|$)/ $NEWNAME\1/g" $FILE
sed -i -re "s/([^a-zA-Z]|^)$CLASSNAME([^a-zA-Z])/\1$NEWNAME\2/g" $FILE
echo "$FILE modified."
ANYMODIFIED=true
fi
done
if ! $ANYMODIFIED; then
echo "No classes with that name were found. If you believe this is in error, report the issue!"
fi
fi
if [ $ANSWER == "2" ]; then
echo "Please enter the original file name, without an extension. If you use an extension, I will ignore it anyway."
read -p "All files that match the given name, even if they have different extensions, will be renamed: " FILENAME
FILENAME=$(echo "$FILENAME" | sed -re "s/([a-zA-Z0-9_-]+)([\.]?[a-zA-Z0-9_-]+)?/\1/g" )
read -p "Enter your new file name: " NEWNAME
NEWNAME=$(echo "$NEWNAME" | sed -re "s/([a-zA-Z0-9_-]+)([\.]?[a-zA-Z0-9_-]+)?/\1/g" )
echo ""
MAKEFILE=$(ls . | grep akefile) #lol
if [ -r $MAKEFILE ]; then
if grep -qE "(\/| )$FILENAME\." $MAKEFILE; then
echo "$MAKEFILE modified."
sed -i -re "s/(\/| )$FILENAME\./\1$NEWNAME\./g" $MAKEFILE
fi
fi
INCLUDESMODIFIED=false
for i in $(find . -type f | grep -v "\/\.git"); do
if grep -qE "\#include ([\"<])$FILENAME\.h([\">])" $i ; then
INCLUDESMODIFIED=true
sed -i -re "s/\#include ([\"<])$FILENAME\.h([\">])/\#include \1$NEWNAME\.h\2/g" $i
echo "$i modified."
fi
done
NAMESMODIFIED=false
FILES=$(find . -type f | grep "$FILENAME\.")
for i in $FILES; do
NAMESMODIFIED=true
EXTENSION=$(echo $i | sed -re "s/.*\/[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)/\1/g")
FILEPATH=$(echo $i | sed -re "s/(.*\/)[a-zA-Z0-9_-]+[\.]?[a-zA-Z0-9_-]+/\1/g")
echo "$FILEPATH$FILENAME$EXTENSION -----> $FILEPATH$NEWNAME$EXTENSION"
mv $i "$FILEPATH$NEWNAME$EXTENSION"
done
if ! $INCLUDESMODIFIED; then
echo -e "No \043includes were found for the file you renamed."
fi
if ! $NAMESMODIFIED; then
echo "No files with that name were found. If you believe this is in error, report the issue!"
fi
fi