Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/mix/tasks/ecto.gen.repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do

case File.read(config_path) do
{:ok, contents} ->
check = String.contains?(contents, "import Config")
config_first_line = get_first_config_line(check) <> "\n"
new_contents = config_first_line <> "\n" <> config_template(opts)
Mix.shell.info [:green, "* updating ", :reset, "config/config.exs"]
File.write! "config/config.exs",
String.replace(contents, "use Mix.Config\n", config_template(opts))
File.write! "config/config.exs", String.replace(contents, config_first_line, new_contents)
{:error, _} ->
create_file "config/config.exs", config_template(opts)
config_first_line = Config |> Code.ensure_loaded?() |> get_first_config_line()
create_file "config/config.exs", config_first_line <> "\n\n" <> config_template(opts)
end

open?("config/config.exs")
Expand All @@ -74,6 +77,9 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do
"""
end

defp get_first_config_line(true), do: "import Config"
defp get_first_config_line(false), do: "use Mix.Config"

embed_template :repo, """
defmodule <%= inspect @mod %> do
use Ecto.Repo,
Expand All @@ -83,8 +89,6 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do
"""

embed_template :config, """
use Mix.Config

config <%= inspect @app %>, <%= inspect @mod %>,
database: "<%= @app %>_<%= @base %>",
username: "user",
Expand Down
4 changes: 3 additions & 1 deletion test/mix/tasks/ecto.gen.repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ defmodule Mix.Tasks.Ecto.Gen.RepoTest do
end
"""

first_line = if Code.ensure_loaded?(Config), do: "import Config", else: "use Mix.Config"

assert_file "config/config.exs", """
use Mix.Config
#{first_line}

config :ecto, Repo,
database: "ecto_repo",
Expand Down