Customizing `mix phoenix.gen.*` tasks

Because I am who I am, I've deviated from some default Phoenix file layouts. For example, rather than models having a single changeset function with two parameters you combine in different ways, I've tentatively decided I'd rather have variants like new_action_changeset, create_action_changeset, update_action_changeset, etc. That means a model should start out looking like this:

Similar changes are needed in the controller - and in model tests - and in controller tests. It would be tedious to run the default mix.phoenix.html task and then edit those files to support my quirkiness, so I needed to customize the "gen" process.

That's easy enough, but a checklist can't hurt for newbies like me.

[Update: José Valim points out the instructions below assume you want to keep the old mix tasks working. If you just want to replace them, skip past this first task list to the second.]

  1. You'll need two directories for copies of the Phoenix versions: lib/mix/tasks/ and priv/templates/. Those correspond to deps/phoenix/lib/mix/tasks/ and deps/phoenix/priv/templates/. (You could edit the Phoenix versions in place, but then later Phoenix releases would clobber your changes.)

     mkdir -p lib/mix/tasks/ priv/templates/
    
  2. Make copies of the templates you care about. Each task has a directory full of templates. If (as I do) you want to change the phoenix.gen.html task, you'll need two directories (because that task depends on the phoenix.gen.model task).

     cp -r deps/phoenix/priv/templates/phoenix.gen.html priv/templates
     cp -r deps/phoenix/priv/templates/phoenix.gen.model priv/templates
    
  3. You'll need the corresponding tasks. I gave mine the same name, but with a 2 on the end. (So that they'd show up next to the originals in mix help.)

    cp deps/phoenix/lib/mix/tasks/phoenix.gen.model.ex lib/mix/tasks/phoenix.gen.model2.ex
    cp deps/phoenix/lib/mix/tasks/phoenix.gen.html.ex lib/mix/tasks/phoenix.gen.html2.ex
    
  4. Because of the name change, elixir files need to be edited. And it doesn't hurt to describe the change:

  5. Edit your new template files.

  6. Compile.

    mix compile
    

That's all there is to it.


Update: if you know you won't ever want to use the default tasks, you can simplify the above like this:

  1. You'll need this directory for copies of the Phoenix versions: priv/templates/. It corresponds to deps/phoenix/priv/templates/. (You could edit the Phoenix versions in place, but then later Phoenix releases would clobber your changes.)

     mkdir -p priv/templates/
    
  2. Make copies of the templates you care about. Each task has a directory full of templates. If (as I do) you want to change the phoenix.gen.html task, you'll need two directories (because that task depends on the phoenix.gen.model task).

     cp -r deps/phoenix/priv/templates/phoenix.gen.html priv/templates
     cp -r deps/phoenix/priv/templates/phoenix.gen.model priv/templates
    
  3. Edit your new template files.

  4. Compile.

    mix compile
    

That's all there is to it.