diff --git a/installer/lib/phx_new/ecto.ex b/installer/lib/phx_new/ecto.ex index 5d56a47769..2eff11f174 100644 --- a/installer/lib/phx_new/ecto.ex +++ b/installer/lib/phx_new/ecto.ex @@ -27,9 +27,9 @@ defmodule Phx.New.Ecto do end def generate(%Project{} = project) do + inject_umbrella_config_defaults(project) copy_from project, __MODULE__, :new if Project.ecto?(project), do: Phx.New.Single.gen_ecto(project) project end - end diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex index e0ee1a9085..b1dab54fa7 100644 --- a/installer/lib/phx_new/generator.ex +++ b/installer/lib/phx_new/generator.ex @@ -83,6 +83,14 @@ defmodule Phx.New.Generator do end end + def inject_umbrella_config_defaults(project) do + unless File.exists?(Project.join_path(project, :project, "config/dev.exs")) do + path = Project.join_path(project, :project, "config/config.exs") + extra = Phx.New.Umbrella.render(:new, "phx_umbrella/config/extra_config.exs") + File.write(path, [File.read!(path), extra]) + end + end + defp split_with_self(contents, text) do case :binary.split(contents, text) do [left, right] -> [left, text, right] diff --git a/installer/lib/phx_new/umbrella.ex b/installer/lib/phx_new/umbrella.ex index 1c46d37dcb..b268ad11cc 100644 --- a/installer/lib/phx_new/umbrella.ex +++ b/installer/lib/phx_new/umbrella.ex @@ -4,15 +4,16 @@ defmodule Phx.New.Umbrella do alias Phx.New.{Ecto, Web, Project} template :new, [ - {:eex, "phx_umbrella/gitignore", :project, ".gitignore"}, - {:eex, "phx_umbrella/config/config.exs", :project, "config/config.exs"}, - {:eex, "phx_umbrella/config/dev.exs", :project, "config/dev.exs"}, - {:eex, "phx_umbrella/config/test.exs", :project, "config/test.exs"}, - {:eex, "phx_umbrella/config/prod.exs", :project, "config/prod.exs"}, - {:eex, "phx_umbrella/config/prod.secret.exs", :project, "config/prod.secret.exs"}, - {:eex, "phx_umbrella/mix.exs", :project, "mix.exs"}, - {:eex, "phx_umbrella/README.md", :project, "README.md"}, - {:eex, "phx_umbrella/formatter.exs", :project, ".formatter.exs"}, + {:eex, "phx_umbrella/gitignore", :project, ".gitignore"}, + {:eex, "phx_umbrella/config/config.exs", :project, "config/config.exs"}, + {:config, "phx_umbrella/config/extra_config.exs", :project, "config/config.exs"}, + {:eex, "phx_umbrella/config/dev.exs", :project, "config/dev.exs"}, + {:eex, "phx_umbrella/config/test.exs", :project, "config/test.exs"}, + {:eex, "phx_umbrella/config/prod.exs", :project, "config/prod.exs"}, + {:eex, "phx_umbrella/config/prod.secret.exs", :project, "config/prod.secret.exs"}, + {:eex, "phx_umbrella/mix.exs", :project, "mix.exs"}, + {:eex, "phx_umbrella/README.md", :project, "README.md"}, + {:eex, "phx_umbrella/formatter.exs", :project, ".formatter.exs"}, ] def prepare_project(%Project{app: app} = project) when not is_nil(app) do diff --git a/installer/lib/phx_new/web.ex b/installer/lib/phx_new/web.ex index 21f32f9b4b..9e58c1e636 100644 --- a/installer/lib/phx_new/web.ex +++ b/installer/lib/phx_new/web.ex @@ -62,6 +62,7 @@ defmodule Phx.New.Web do end def generate(%Project{} = project) do + inject_umbrella_config_defaults(project) copy_from project, __MODULE__, :new copy_from project, __MODULE__, :gettext diff --git a/installer/templates/phx_umbrella/config/config.exs b/installer/templates/phx_umbrella/config/config.exs index 0284598b03..8b9ddc0b5d 100644 --- a/installer/templates/phx_umbrella/config/config.exs +++ b/installer/templates/phx_umbrella/config/config.exs @@ -8,15 +8,3 @@ # configurations or dependencies per app, it is best to # move said applications out of the umbrella. use Mix.Config - -# Configures Elixir's Logger -config :logger, :console, - format: "$time $metadata[$level] $message\n", - metadata: [:request_id] - -# Use Jason for JSON parsing in Phoenix -config :phoenix, :json_library, Jason - -# Import environment specific config. This must remain at the bottom -# of this file so it overrides the configuration defined above. -import_config "#{Mix.env()}.exs" diff --git a/installer/templates/phx_umbrella/config/extra_config.exs b/installer/templates/phx_umbrella/config/extra_config.exs new file mode 100644 index 0000000000..b1734d2f10 --- /dev/null +++ b/installer/templates/phx_umbrella/config/extra_config.exs @@ -0,0 +1,12 @@ + +# Configures Elixir's Logger +config :logger, :console, + format: "$time $metadata[$level] $message\n", + metadata: [:request_id] + +# Use Jason for JSON parsing in Phoenix +config :phoenix, :json_library, Jason + +# Import environment specific config. This must remain at the bottom +# of this file so it overrides the configuration defined above. +import_config "#{Mix.env()}.exs" diff --git a/installer/test/phx_new_ecto_test.exs b/installer/test/phx_new_ecto_test.exs index 4011af6296..6ca3411777 100644 --- a/installer/test/phx_new_ecto_test.exs +++ b/installer/test/phx_new_ecto_test.exs @@ -12,10 +12,21 @@ defmodule Mix.Tasks.Phx.New.EctoTest do :ok end + @app_name "phx_ecto" + test "new without args" do - in_tmp_umbrella_project "new without args", fn -> - assert capture_io(fn -> Mix.Tasks.Phx.New.Ecto.run([]) end) =~ - "Creates a new Ecto project within an umbrella project." + assert capture_io(fn -> Mix.Tasks.Phx.New.Ecto.run([]) end) =~ + "Creates a new Ecto project within an umbrella project." + end + + test "new with barebones umbrella" do + in_tmp_umbrella_project "new with barebones umbrella", fn -> + files = ~w[../config/dev.exs ../config/test.exs ../config/prod.exs ../config/prod.secret.exs] + Enum.each(files, &File.rm/1) + + assert_file "../config/config.exs", &refute(&1 =~ ~S[import_config "#{Mix.env()}.exs"]) + Mix.Tasks.Phx.New.Ecto.run([@app_name]) + assert_file "../config/config.exs", &assert(&1 =~ ~S[import_config "#{Mix.env()}.exs"]) end end @@ -29,7 +40,7 @@ defmodule Mix.Tasks.Phx.New.EctoTest do test "new with defaults", config do in_tmp_umbrella_project config.test, fn -> - Mix.Tasks.Phx.New.Ecto.run(["phx_ecto"]) + Mix.Tasks.Phx.New.Ecto.run([@app_name]) # Install dependencies? assert_received {:mix_shell, :yes?, ["\nFetch and install dependencies?"]} diff --git a/installer/test/phx_new_web_test.exs b/installer/test/phx_new_web_test.exs index 93ab23f5e8..4b828f89a2 100644 --- a/installer/test/phx_new_web_test.exs +++ b/installer/test/phx_new_web_test.exs @@ -15,9 +15,18 @@ defmodule Mix.Tasks.Phx.New.WebTest do end test "new without args" do - in_tmp_umbrella_project "new without args", fn -> - assert capture_io(fn -> Mix.Tasks.Phx.New.Web.run([]) end) =~ - "Creates a new Phoenix web project within an umbrella project." + assert capture_io(fn -> Mix.Tasks.Phx.New.Web.run([]) end) =~ + "Creates a new Phoenix web project within an umbrella project." + end + + test "new with barebones umbrella" do + in_tmp_umbrella_project "new with barebones umbrella", fn -> + files = ~w[../config/dev.exs ../config/test.exs ../config/prod.exs ../config/prod.secret.exs] + Enum.each(files, &File.rm/1) + + assert_file "../config/config.exs", &refute(&1 =~ ~S[import_config "#{Mix.env()}.exs"]) + Mix.Tasks.Phx.New.Web.run([@app_name]) + assert_file "../config/config.exs", &assert(&1 =~ ~S[import_config "#{Mix.env()}.exs"]) end end