2021-01-11 04:51:08 +01:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
|
|
|
# Check that regex-rename is installed
|
|
|
|
if ! command -v regex-rename &> /dev/null
|
|
|
|
then
|
|
|
|
echo "regex-rename python module not found. Please run 'python -m pip install regex-rename'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-26 18:48:24 +01:00
|
|
|
# GNU sed and BSD(Mac) sed handle -i differently :(
|
|
|
|
function is_gnu_sed(){
|
|
|
|
sed --version >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2021-01-11 04:51:08 +01:00
|
|
|
# Allow specifying either:
|
|
|
|
# * One argument, which is the new project name, assuming the old project name is "my project name"
|
|
|
|
# * Or two arguments, where one can specify 1. the old project name and 2. the new project name
|
|
|
|
if [ $# -eq 1 ]; then
|
|
|
|
PLACEHOLDER="my project name"
|
|
|
|
REPLACEMENT=$1
|
|
|
|
elif [ $# -eq 2 ]; then
|
|
|
|
PLACEHOLDER=$1
|
|
|
|
REPLACEMENT=$2
|
|
|
|
else
|
|
|
|
echo "Usage:"
|
|
|
|
echo "./"$(basename "$0") "\"new name\""
|
|
|
|
echo "./"$(basename "$0") "\"old name\" \"new name\""
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-01-11 05:43:31 +01:00
|
|
|
PLACEHOLDER_DASHES="${PLACEHOLDER// /-}"
|
|
|
|
PLACEHOLDER_UNDERSCORES="${PLACEHOLDER// /_}"
|
2021-01-11 04:51:08 +01:00
|
|
|
|
2021-01-11 05:43:31 +01:00
|
|
|
REPLACEMENT_DASHES="${REPLACEMENT// /-}"
|
|
|
|
REPLACEMENT_UNDERSCORES="${REPLACEMENT// /_}"
|
2021-01-11 04:51:08 +01:00
|
|
|
|
|
|
|
echo "Updating file and folder names..."
|
|
|
|
|
2021-01-11 05:43:31 +01:00
|
|
|
# Iterate over all directories (besides venv's and .git) and rename files/folders
|
|
|
|
# Yes this looks like some crazy voodoo, but it's necessary as regex-rename does
|
|
|
|
# not provide any sort of recursive functionality...
|
|
|
|
find . -type d -not -path "./env*" -not -path "./.git" -not -path "./.git*" \
|
|
|
|
-exec sh -c "cd {} && \
|
|
|
|
regex-rename --rename \"(.*)$PLACEHOLDER_DASHES(.*)\" \"\1$REPLACEMENT_DASHES\2\" && \
|
|
|
|
regex-rename --rename \"(.*)$PLACEHOLDER_UNDERSCORES(.*)\" \"\1$REPLACEMENT_UNDERSCORES\2\"" \; > /dev/null
|
2021-01-11 04:51:08 +01:00
|
|
|
|
|
|
|
echo "Updating references within files..."
|
|
|
|
|
|
|
|
# Iterate through each file and replace strings within files
|
2021-02-26 18:48:24 +01:00
|
|
|
for file in $(grep --exclude-dir=env --exclude-dir=venv --exclude-dir=.git --exclude *.pyc -lEw "$PLACEHOLDER_DASHES|$PLACEHOLDER_UNDERSCORES" -R * .[^.]*); do
|
2021-01-11 05:43:31 +01:00
|
|
|
echo "Checking $file"
|
2021-01-11 04:51:08 +01:00
|
|
|
if [[ $file != $(basename "$0") ]]; then
|
2021-02-26 18:48:24 +01:00
|
|
|
if is_gnu_sed; then
|
|
|
|
sed -i "s/$PLACEHOLDER_DASHES/$REPLACEMENT_DASHES/g" $file
|
|
|
|
sed -i "s/$PLACEHOLDER_UNDERSCORES/$REPLACEMENT_UNDERSCORES/g" $file
|
|
|
|
else
|
|
|
|
sed -i "" "s/$PLACEHOLDER_DASHES/$REPLACEMENT_DASHES/g" $file
|
|
|
|
sed -i "" "s/$PLACEHOLDER_UNDERSCORES/$REPLACEMENT_UNDERSCORES/g" $file
|
|
|
|
fi
|
2021-01-11 04:51:08 +01:00
|
|
|
echo " - $file"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "Done!"
|