-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrepurpose_package.sh
More file actions
executable file
·178 lines (139 loc) · 4.36 KB
/
Copy pathrepurpose_package.sh
File metadata and controls
executable file
·178 lines (139 loc) · 4.36 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
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
178
#!/usr/bin/env bash
if [ $# -le 1 ] || [ "$1" = "--help" ]; then
echo "usage: ./repurpose_package.sh <original_package_name> <new_package_name> [--dry-run, --no-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 within the package,
replaces all occurences of the original package name with the new one within all filenames."
exit 1
fi
ORIG_NAME="$1"
NEW_NAME="$2"
DRY_RUN=0
CAMEL_CASE=1
if [ "$ORIG_NAME" = "$NEW_NAME" ]; then
echo "Package names must be different."
exit 1
fi
if [[ " ${*:3} " == *" --dry-run "* ]]; then
echo "Dry run mode on - nothing will be changed."
DRY_RUN=1
fi
if [[ " ${*:3} " == *" --no-camel-case "* ]]; then
echo "CamelCase occurences will not be replaced."
CAMEL_CASE=0
fi
# Try to find and then change into the given directory
dir=$(find . -type d -name "$ORIG_NAME" -print -quit)
if [ -n "$dir" ]; then
cd "$dir" || exit 1
else
echo "Package $ORIG_NAME not found."
exit 1
fi
# Prepare variants of the package names without example_ and _plugin
ORIG_VARIANT=("$ORIG_NAME")
NEW_VARIANT=("$NEW_NAME")
for pattern in "#example_" "%_plugin"; do
eval "ORIG_TEMP=\${ORIG_NAME$pattern}"
eval "NEW_TEMP=\${NEW_NAME$pattern}"
if [ "$ORIG_NAME" != "$ORIG_TEMP" ]; then
ORIG_VARIANT=("$ORIG_TEMP" "${ORIG_VARIANT[@]}")
NEW_VARIANT=("$NEW_TEMP" "${NEW_VARIANT[@]}")
fi
done
# #{ 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"
for i in "${!ORIG_VARIANT[@]}"; do
replace_within_files "${ORIG_VARIANT[i]}" "${NEW_VARIANT[i]}" "$DRY_RUN"
replace_within_filenames "${ORIG_VARIANT[i]}" "${NEW_VARIANT[i]}" "$DRY_RUN"
if [ "$CAMEL_CASE" -eq 1 ]; then
ORIG_CC=$(echo "${ORIG_VARIANT[i]}" | sed -r 's/(^|_)([a-z])/\U\2/g')
NEW_CC=$(echo "${NEW_VARIANT[i]}" | sed -r 's/(^|_)([a-z])/\U\2/g')
replace_within_files "$ORIG_CC" "$NEW_CC" "$DRY_RUN"
replace_within_filenames "$ORIG_CC" "$NEW_CC" "$DRY_RUN"
fi
done
if [ "$DRY_RUN" -eq 1 ]; then
echo "Dry run mode on - nothing was changed."
fi