Last active
August 6, 2019 03:16
-
-
Save apmiller108/ad5bb208828b2d97f5b09d0c7a90c459 to your computer and use it in GitHub Desktop.
Rename a Phoenix application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -o pipefail | |
CURRENT_OTP=$1 | |
NEW_OTP=$2 | |
CURRENT_NAME="" | |
NEW_NAME="" | |
# New name and current name arguments are required | |
if [ $# -ne 2 ]; then | |
echo -e "Please provide the current name and new name in snake case\n"\ | |
"usage: /.rename_phoenix_app.sh current_name new_name" | |
exit 64 | |
fi | |
# Split snake cased names into array of words. | |
IFS="_" read -a current_name_words <<< "$CURRENT_OTP" | |
IFS="_" read -a new_name_words <<< "$NEW_OTP" | |
# Upercase all the words and concatenate them together to derive the CamelCase | |
# module names | |
for word in "${current_name_words[@]}"; do | |
word="$(tr '[:lower:]' '[:upper:]' <<< ${word:0:1})${word:1}" | |
CURRENT_NAME="$CURRENT_NAME$word" | |
done | |
for word in "${new_name_words[@]}"; do | |
word="$(tr '[:lower:]' '[:upper:]' <<< ${word:0:1})${word:1}" | |
NEW_NAME="$NEW_NAME$word" | |
done | |
# Confirm name change | |
echo -e -n "Please confirm this is correct:\n"\ | |
"Change $CURRENT_OTP to $NEW_OTP?\n"\ | |
"Change $CURRENT_NAME to $NEW_NAME?\n"\ | |
"Continue? [y/n]" | |
read -p "" ANSWER | |
if [ "$ANSWER" = "y" ]; then | |
grep -l $CURRENT_NAME -r . --exclude-dir=_build \ | |
| xargs sed -i '' -e "s/$CURRENT_NAME/$NEW_NAME/g" | |
grep -l $CURRENT_OTP -r . --exclude-dir=_build \ | |
| xargs sed -i '' -e "s/$CURRENT_OTP/$NEW_OTP/g" | |
mv lib/$CURRENT_OTP lib/$NEW_OTP | |
mv lib/$CURRENT_OTP.ex lib/$NEW_OTP.ex | |
echo -e "Completed renaming" | |
else | |
echo -e "exiting" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was originally inspired by this gist. Swapped out ack for grep, accept new application name as an argument and added confirmation prompt.