-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename_package.sh
executable file
·142 lines (110 loc) · 3.82 KB
/
rename_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
#!/bin/bash
HELP_MESSAGE="Usage: $0 NEW_PACKAGE_NAME [--dry-run]
Rename a package by replacing all text occurrences of template_component_package
with the new package name and renaming all matching files and directories.
The required positional argument is the old package name to replace.
Use --dry-run to prevent any filesystem changes while testing the usage.
This script replaces all text occurrences of template_component_package with
NEW_PACKAGE_NAME in all files in the following search paths:
- ./.devcontainer.json
- ./.github/workflows/build-test.yml
- ./aica-package.toml
- ./source/**
It also replaces all hyphenated occurrences of the package names in the same search paths
(i.e. template-component-package would be replaced with NEW-PACKAGE-NAME).
Finally, it renames all files and directories that contain template_component_package in
their names to the equivalent name using NEW_PACKAGE_NAME in the following search paths:
- .source/**
Options:
-n, --dry-run Echo the new version but do not
write changes to any files.
-h, --help Show this help message.
"
DRY_RUN=false
POSITIONAL_ARGS=()
while [ "$#" -gt 0 ]; do
case "$1" in
-n | --dry-run) DRY_RUN=true; shift 1;;
-h | --help) echo "${HELP_MESSAGE}"; exit 0;;
-*) echo "Unknown option: $1" >&2; echo "${HELP_MESSAGE}"; exit 1;;
*) POSITIONAL_ARGS+=("$1"); shift 1;;
esac
done
if [[ "${#POSITIONAL_ARGS[@]}" -ne 1 ]]; then
echo "${HELP_MESSAGE}"
exit 1
fi
OLD_NAME=template_component_package
NEW_NAME="${POSITIONAL_ARGS[0]}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
function rename_file() {
FILEPATH="${1}"
BASENAME="$(basename ${FILEPATH})"
NEW_BASENAME="${BASENAME//$OLD_NAME/$NEW_NAME}"
NEW_FILEPATH="$(dirname ${FILEPATH})/${NEW_BASENAME}"
echo " from: ${FILEPATH}"
echo " to: ${NEW_FILEPATH}"
if [ ${DRY_RUN} == false ]; then
mv "${FILEPATH}" "${NEW_FILEPATH}"
fi
}
function replace_text_in_file() {
FILEPATH="${1}"
echo "Replacing text in file: $FILEPATH"
if [ ${DRY_RUN} != false ]; then
return
fi
SED_STR="s/${OLD_NAME}/${NEW_NAME}/g"
SED_STR_HYPHENATED="${SED_STR//_/-}"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "${SED_STR}" "${FILEPATH}"
sed -i '' "${SED_STR_HYPHENATED}" "${FILEPATH}"
else
sed -i "${SED_STR}" "${FILEPATH}"
sed -i "${SED_STR_HYPHENATED}" "${FILEPATH}"
fi
}
if [ ${DRY_RUN} != false ]; then
echo "=== THIS IS A DRY RUN! NO FILE OR FILESYSTEM CHANGES WILL BE MADE ==="
echo
fi
echo "All text occurences of:"
echo " - ${OLD_NAME}"
echo " - ${OLD_NAME//_/-}"
echo "will be replaced with:"
echo " - ${NEW_NAME}"
echo " - ${NEW_NAME//_/-}"
echo "in the following search paths:"
echo " - ${SCRIPT_DIR}/.devcontainer.json"
echo " - ${SCRIPT_DIR}/.github/workflows/build-test.yml"
echo " - ${SCRIPT_DIR}/aica-package.toml"
echo " - ${SCRIPT_DIR}/source/**"
echo
replace_text_in_file "${SCRIPT_DIR}/.devcontainer.json"
replace_text_in_file "${SCRIPT_DIR}/.github/workflows/build-test.yml"
replace_text_in_file "${SCRIPT_DIR}/aica-package.toml"
RENAME_DIRECTORIES=()
for FIND_PATH in $(find "${SCRIPT_DIR}/source"); do
BASENAME=$(basename $FIND_PATH)
if [ -d $FIND_PATH ]; then
if [[ $BASENAME == *"$OLD_NAME"* ]]; then
RENAME_DIRECTORIES+=($FIND_PATH)
fi
else
replace_text_in_file $FIND_PATH
if [[ $BASENAME == *"$OLD_NAME"* ]]; then
echo "Renaming file:"
rename_file $FIND_PATH
fi
fi
done
# rename directories in reverse order (from deep to shallow nesting)
for ((i=${#RENAME_DIRECTORIES[@]}-1; i>=0; i--)); do
echo "Renaming directory:"
RENAME_DIRECTORY="${RENAME_DIRECTORIES[$i]}"
rename_file $RENAME_DIRECTORY
done
if [ ${DRY_RUN} == true ]; then
echo
echo "=== THIS WAS A DRY RUN! NO FILE OR FILESYSTEM CHANGES WERE MADE ==="
fi