6

I've cloned a git repo and I need to start a new project based off of it.

How do I rename it, making sure everything is taken care of?

I'm using macOS.

Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
  • I usually just do a search-replace of `MyApp` with `MyNewApp`, `my_app` with `my_new_app`, rename `lib/my_app` to `lib/my_new_app`, check if it still compiles and finally manually review `git diff`. – Dogbert Apr 08 '17 at 11:23
  • @RomanPerekhrest that doesn't take care of renaming the application and internal references in the code. – Jonathan Soifer Apr 08 '17 at 11:28
  • @Dogbert would you care to explicitly show the commands you're using? – Jonathan Soifer Apr 08 '17 at 11:33
  • 1
    For search-replace in the command line, I'm a fan of Perl: `git ls-files | xargs perl -p -i -e 's/MyApp/MyNewApp/g ; s/my_app/my_new_app/g'` (make sure your changes are committed before running this!). – Dogbert Apr 08 '17 at 11:43

2 Answers2

2

Based off of nerdyworm's rename.sh, that uses ack I've adapted it to use git grep and run on macOS easily.

OTP NAME: Snake Case (snake_case) name for the Application.

NAME: Regular app name.

$ git grep -l $CURRENT_OTP | xargs sed -i '' -e "s/$CURRENT_OTP/$NEW_OTP/g"
$ git grep -l $CURRENT_NAME | xargs sed -i '' -e "s/$CURRENT_NAME/$NEW_NAME/g"

$ mv lib/$CURRENT_OTP lib/$NEW_OTP
$ mv lib/$CURRENT_OTP.ex lib/$NEW_OTP.ex
Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
1

I do not know of any way to do this automatically with perfect accuracy, but these are the steps I follow manually when I need to rename Phoenix projects.

The commands below assume your code is in a git repo and the old application is my_app / MyApp and the new one is my_new_app / MyNewApp. The process is brittle -- if your application or module name can occur in many places (e.g. it's a single letter A or something), then this will not work.

# search replace both the application name and module name in all files indexed by git
$ git ls-files -z | xargs -0 perl -p -i -e 's/my_app/my_new_app/g; s/MyApp/MyNewApp/g;'
# rename `lib/my_app`
$ mv lib/my_app lib/my_new_app

After that I run mix test and fix any compilation errors or test failures.

Finally I do a quick review of git diff to make sure everything looks right.

Dogbert
  • 212,659
  • 41
  • 396
  • 397