How to rename an Elixir Phoenix app

There are times when you want to rename your Phoenix project. It requires renaming the module as well the OTP app. You can use following sequence of steps:

Steps

  1. Check in all your files into a git repo (git init ., git add -A, git commit -m 'initial commit')
  2. Create a new file rename_phoenix_project.sh and copy the code.
  3. Replace the variables NEW_NAME and NEW_OTP with the appropriate new names.
  4. Replace the variables CURRENT_NAME and CURRENT_OTP with the old names.
  5. Save the file, and execute the script in the terminal: sh rename_phoenix_project.sh
#!/bin/bash
export LC_CTYPE=C
export LANG=C

NEW_NAME="NewName"
NEW_OTP="new_name"

CURRENT_NAME="OldName"
CURRENT_OTP="old_name"

set -e

if ! command -v ack &> /dev/null
then
    echo "\`ack\` could not be found. Please install it before continuing (Mac: brew install ack)."
    exit 1
fi

ack -l $CURRENT_NAME --ignore-file=is:rename_phoenix_project.sh | xargs sed -i '' -e "s/$CURRENT_NAME/$NEW_NAME/g"
ack -l $CURRENT_OTP --ignore-file=is:rename_phoenix_project.sh | xargs sed -i '' -e "s/$CURRENT_OTP/$NEW_OTP/g"

git mv lib/$CURRENT_OTP lib/$NEW_OTP
git mv lib/$CURRENT_OTP.ex lib/$NEW_OTP.ex
git mv lib/${CURRENT_OTP}_web lib/${NEW_OTP}_web
git mv lib/${CURRENT_OTP}_web.ex lib/${NEW_OTP}_web.ex


git mv test/${CURRENT_OTP}_web test/${NEW_OTP}_web

Reference

Rename Phoenix Project script from Petal Boilerplate

© 2023 Abhinav Saxena   •   Powered by Soopr   •   Theme  Moonwalk