How to change a Phoenix Project name smoothly?

I use mix phoenix.new paper to create a new project paper.

So all the stuff is called Paper or :paper.

However, I have to define a model called paper. So I have something like defmodule Paper.Paper do.

When I have multiple aliaes, I need to move Paper.paper to the last.

I know I made a “stupid” codename that may interfere with the model name.

So how can I modify all the Paper. stuff and :paper stuff eaisly? Is there a Phoenix mix command to do that?

Thanks!

9 Likes

There is not a command for renaming a project. I have however had to do so a couple times in the past, and in general global find and replace works really rather well. Just also move any directories named paper and you’re done.

4 Likes

Thanks a lot! I just want to know if others may have the same problem. In my following project I should be more careful about the namings.

2 Likes

A global find+replace works a lot better in Elixir than in most OOP-languages, because you always refer to a certain piece of code the same way: ModuleName.function_name. And structs use the ModuleName as well.

In OOP-languages, you can only hope that some object isn’t referred to under a different name.

4 Likes

I’ve been using this script for the two or three times I had to rename a phoenix project: https://gist.github.com/nerdyworm/3d623b13bf0d6d664373e2f501f16423

requires ack to be installed, but you could certainly swap that out for grep.

Cheers,
Ben

10 Likes

• Works on MacOS, not tested elsewhere.
• Needs: git, xargs and sed.

From the root folder of the project:

  1. git grep -l 'old_name' | xargs sed -i '' -e 's/old_name/new_name/g'

  2. git grep -l 'OldName' | xargs sed -i '' -e 's/OldName/NewName/g'

  3. mv ./lib/old_name ./lib/new_name

  4. mv ./lib/old_name.ex ./lib/new_name.ex

    If you have a similar folder structure in the tests folder, you probably also need:

  5. mv ./test/old_name ./test/new_name

10 Likes

@jaysoifer: you forgot about mv command for tests :smile:
Also you need to be careful using grep (no preview of changes), because some simple names could be also in other files like assets. Better way is to exclude some folders - just to be sure.
I suggest to use kfilereplace if you are using KDE/Plasma 5, because you can preview what files will be modified.

2 Likes

Editing!

I’m running 1.3-rc1, I don’t see anything else missing, can you help me?

@jaysoifer: you could have same directory structure in test folder like in lib folder.

Updated!

I am working on phoenix 1.3.0 , is possible to use the above command to change project name?

1 Like

Will this work for Phoenix 1.3?

1 Like
mv ./lib/old_name_web ./lib/new_name_web
mv ./lib/old_name_web.ex ./lib/new_name_web.ex

…done

Since this is the first hit on google for this problem, I library that I’ve used a couple times for this problem is

1 Like

Great library! Worked out great, the only was with it also change the DB name on dev.exs/text.exs etc which I had to fix manually but besides that - works great!

1 Like