Skip to content

Instantly share code, notes, and snippets.

@oliverswitzer
Created April 19, 2023 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliverswitzer/8a6825a8c7ab24d62d9b4f424d23009e to your computer and use it in GitHub Desktop.
Save oliverswitzer/8a6825a8c7ab24d62d9b4f424d23009e to your computer and use it in GitHub Desktop.
A little shell script that helps you rename a templated Phoenix project (note: might be out of date with current version of Phoenix)
#!/bin/bash
rename_cwd() {
cd . || return
new_dir=${PWD%/*}/$1
mv -- "$PWD" "$new_dir" &&
cd -- "$new_dir"
}
if grep -R -l rename lib > /dev/null; then
echo ""
else
read -p "You've already renamed this app. Are you sure you want to rename it
again? Y/n " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Nn]$ ]]; then
exit 1
fi
fi
read -p "Need to install tool 'rename' in order to globally rename files in this project. Is that ok? Y/n " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
brew install rename
else
echo "Aborting"
exit 1
fi
cat << EOF
What module name would you like to give this app?
Example: MyApp would create MyApp and MyAppWeb module prefixes, with a file
structure like so:
* lib/my_app/...
* lib/my_app_web/..
* test/my_app/...
* test/my_app_web/...
Note: it will also be used as the default name for your Heroku app when automatically deploying to Heroku later, ie my-app.herokuapp.com (you can skip this step if you'd like)
EOF
read module_name
export module_name
export snake_name=$(echo $module_name | sed 's/[[:upper:]]/_&/g;s/^_//' | tr '[:upper:]' '[:lower:]')
echo "Renaming all instances of ReplaceMe with $module_name and replace_me with
$snake_name in project..."
LC_ALL=C find lib config test assets priv -type f -name '*' -exec sed -i '' s/RenameMe/$module_name/g {} +
LC_ALL=C find lib config test assets priv -type f -name '*' -exec sed -i '' s/rename_me/$snake_name/g {} +
sed -I '' "s/RenameMe/$module_name/g" mix.exs
sed -I '' "s/rename_me/$snake_name/g" mix.exs
# Rename all folders/files using rename
find . -execdir rename -f 's/rename_me/\Q$ENV{snake_name}\E/' '{}' \+ &> /dev/null
echo "Heads up! Also renaming the current working directory to $snake_name..."
rename_cwd $snake_name
echo "✅ Done! your app is renamed to $snake_name / $module_name."
echo
cat << EOF
______ __ __ ______ ______ ______ ______ ______
/\ ___\ /\ \/\ \ /\ ___\ /\ ___\ /\ ___\ /\ ___\ /\ ___\
\ \___ \ \ \ \_\ \ \ \ \____ \ \ \____ \ \ __\ \ \___ \ \ \___ \
\/\_____\ \ \_____\ \ \_____\ \ \_____\ \ \_____\ \/\_____\ \/\_____\
\/_____/ \/_____/ \/_____/ \/_____/ \/_____/ \/_____/ \/_____/
EOF
read -p "Do you want to run the app locally? (ensure that you have postgres running first) Y/n " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
mix ecto.create && mix ecto.migrate && mix phx.server
else
echo "Skipping: compile & run the app"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment