-
Notifications
You must be signed in to change notification settings - Fork 5
/
repurpose_package.sh
executable file
·178 lines (124 loc) · 3.62 KB
/
repurpose_package.sh
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
if [ $# -le 1 ] || [ $1 == "--help" ]; then
echo "usage: ./repurpose_package.sh <original_package_name> <new_package_name> [--dry-run, --camel-case]
Removes the .git directory, initializes a new one,
replaces all occurences of the original package name with the new one in all files withing all subfolders,
replaces all occurences of the original package name with the new one wthinin all filenames."
exit 1
fi
ORIG_NAME=$1
NEW_NAME=$2
DRY_RUN=0
CAMEL_CASE=0
if [ "$3" == "--dry-run" ] || [ "$4" == "--dry-run" ]; then
echo "Dry run mode on - nothing will be changed."
DRY_RUN=1
fi
if [ "$3" == "--camel-case" ] || [ "$4" == "--camel-case" ]; then
echo "CamelCase occurences will also be replaced."
CAMEL_CASE=1
fi
# #{ init_new_repo ()
init_new_repo ()
{
local DRY_RUN=$1
# Remove .git and initialize a new repository
echo "** CREATING NEW GIT REPOSITORY **"
# Ask for confirmation
echo History of the old repository will be deleted.
read -p "Are you sure? (y/n) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
if [ "$DRY_RUN" -eq 0 ]; then
rm -rf .git
git init
fi
echo Created a new git repository.
else
echo Not creating a new git repository.
fi
}
# #}
# #{ replace_within_files ()
replace_within_files ()
{
local ORIG_NAME=$1
local NEW_NAME=$2
local DRY_RUN=$3
# Replace occurences within files
readarray -d '' within_files < <(grep -rl "$ORIG_NAME" --null)
echo "** REPLACING OCCURENCES WITHIN FILES **"
if [[ -z "${within_files[@]}" ]]; then
echo "No matches found for \"$ORIG_NAME\" within files!"
else
# Ask for confirmation
echo These files will be modified:
for file in "${within_files[@]}"
do
echo " - $file"
done
read -p "Are you sure? (y/n) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
for file in "${within_files[@]}"
do
echo " - Replacing \"$1\" with \"$2\" in file \"$file\"."
if [ "$DRY_RUN" -eq 0 ]; then
sed -i "s/$ORIG_NAME/$NEW_NAME/g" "$file"
fi
done
echo "Replaced all occurences within files."
else
echo "Not replacing occurences within files."
fi
fi
}
# #}
# #{ replace_within_filenames ()
replace_within_filenames ()
{
local ORIG_NAME=$1
local NEW_NAME=$2
local DRY_RUN=$3
# Replace occurences in file names
readarray -d '' files < <(find -name "*$ORIG_NAME*" -print0)
echo "** REPLACING OCCURENCES WITHIN FILE NAMES **"
if [[ -z "${files[@]}" ]]; then
echo "No matches found for \"$ORIG_NAME\" in file names!"
else
# Ask for confirmation
echo These files will be modified:
for file in "${files[@]}"
do
echo " - $file"
done
read -p "Are you sure? (y/n) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
for file in "${files[@]}"
do
new_file=${file//$ORIG_NAME/$NEW_NAME}
echo " - Renaming file \"$file\" to \"$new_file\"."
if [ "$DRY_RUN" -eq 0 ]; then
mv "$file" "$new_file"
fi
done
echo "Replaced all occurences within file name."
else
echo "Not replacing occurences within file name."
fi
fi
}
# #}
init_new_repo $DRY_RUN
replace_within_files $ORIG_NAME $NEW_NAME $DRY_RUN
replace_within_filenames $ORIG_NAME $NEW_NAME $DRY_RUN
if [ "$CAMEL_CASE" -eq 1 ]; then
ORIG_NAME_CC=`echo "$ORIG_NAME" | sed -r 's/(^|_)([a-z])/\U\2/g'`
NEW_NAME_CC=`echo "$NEW_NAME" | sed -r 's/(^|_)([a-z])/\U\2/g'`
replace_within_files $ORIG_NAME_CC $NEW_NAME_CC $DRY_RUN
replace_within_filenames $ORIG_NAME_CC $NEW_NAME_CC $DRY_RUN
fi
if [ "$DRY_RUN" -eq 1 ]; then
echo "Dry run mode on - nothing was changed."
fi