Skip to content

Commit

Permalink
Port config to root umbrella, closes #3429
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Jun 1, 2019
1 parent b10fb7d commit 4fc2642
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion installer/lib/phx_new/ecto.ex
Expand Up @@ -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
8 changes: 8 additions & 0 deletions installer/lib/phx_new/generator.ex
Expand Up @@ -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]
Expand Down
19 changes: 10 additions & 9 deletions installer/lib/phx_new/umbrella.ex
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions installer/lib/phx_new/web.ex
Expand Up @@ -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

Expand Down
12 changes: 0 additions & 12 deletions installer/templates/phx_umbrella/config/config.exs
Expand Up @@ -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"
12 changes: 12 additions & 0 deletions 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"
19 changes: 15 additions & 4 deletions installer/test/phx_new_ecto_test.exs
Expand Up @@ -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

Expand All @@ -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?"]}
Expand Down
15 changes: 12 additions & 3 deletions installer/test/phx_new_web_test.exs
Expand Up @@ -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

Expand Down

0 comments on commit 4fc2642

Please sign in to comment.