Getting the right mix phx.new mixture of --app and --module

I am playing with different ways of structuring Phoenix applications and have come upon a frustrating scenario in which I can’t seem to get the right incantation of mix phx.new. Here’s what I’m trying to accomplish.

If you were to run mix phx.new cocktail --umbrella, it would create a structure similar to this (truncated):

cocktail/
  apps/
    cocktail/
      lib/
        cocktail.ex
        cocktail/
      mix.exs
    cocktail_web/
      lib/
        cocktail_web.ex
        cocktail_web/
      mix.exs

For cocktail, the app name is cocktail and for cocktail_web, the app name is cocktail_web — as you’d expect. cocktail_web's module name is CocktailWeb and the generated controller name is CocktailWeb.PageController which is perfect.

Now the issue I’m having is that I have created a straight Elixir app named cocktail that contains my business logic. I’d like to create a Phoenix application named cocktail_web that is a presentation layer for that data, but I’m having the hardest time getting the right mix of --app and --module to get it to generate files similar to how mix phx.new cocktail --umbrella would generate the web side.

I would like the app to be named cocktail_web but the generator always creates a file structure like this

cocktail_web/
  lib/
    cocktail_web/
    cocktail_web.ex
    cocktail_web_web/
      controllers, etc.
    cocktail_web_web.ex

How can I keep it from putting all the web-centric stuff in cocktail_web_web? Can I use the generator to create a structure similar to mix phx.new cocktail --umbrella?

Hopefully all that made sense.

1 Like

It sounds like the phx.new.web, task is what you’re after. It’s a task specifically for creating an web umbrella app inside an existing umbrella which you would wire up to cocktail. So you should be able to cd into your apps dir, then runmix phx.new.web cocktail_web.

13 Likes

I wish I could like this more than once

I did previously discover mix phx.new.web and saw that it does what I’m hoping for inside an umbrella app, but in this case I’m specifically trying to avoid creating an umbrella app. Based on your response, I’m guessing that what I’m trying to achieve is just not possible with the generator.

1 Like

One workaround is that you can run mix phx.new.web in a folder named apps where the parent directory has a mix.exs file (which can be an empty file). Then you can remove/modify the lines that are umbrella specific.

I believe that for the current generators these are the main lines that need modifcation:

$ ag '\.\.\/\.\.'
assets/package.json
9:    "phoenix": "file:../../../deps/phoenix",
10:    "phoenix_html": "file:../../../deps/phoenix_html"

mix.exs
8:      build_path: "../../_build",
9:      config_path: "../../config/config.exs",
10:      deps_path: "../../deps",
11:      lockfile: "../../mix.lock",

I’ve been doing some local testing everything seems to be wokring fine so far. Although I haven’t tried to build a production deploy yet.

2 Likes