Skip to content

Commit 35e04d0

Browse files
author
José Valim
committed
Allow mix loadconfig to be called multiple times
1 parent 1038018 commit 35e04d0

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

lib/mix/lib/mix/cli.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ defmodule Mix.CLI do
113113
defp load_dot_config do
114114
path = Path.expand("~/.mix/config.exs")
115115
if File.regular?(path) do
116-
path
117-
|> Mix.Config.read!()
118-
|> Mix.Config.persist()
116+
Mix.Task.run "loadconfig", [path]
119117
end
120118
end
121119

lib/mix/lib/mix/tasks/loadconfig.ex

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
defmodule Mix.Tasks.Loadconfig do
22
use Mix.Task
33

4+
@shortdoc "Loads and persists the given configuration"
5+
46
@moduledoc """
5-
Loads and persists the project configuration.
6-
"""
7+
Loads and persists the given configuration.
78
8-
def run(_) do
9-
if File.regular?("config/config.exs") do
10-
Mix.Config.persist Mix.Config.read! "config/config.exs"
9+
In case no configuration file is given, it
10+
loads the project oe at "config/config.exs".
11+
12+
This task is automatically reenabled, so it
13+
can be called multiple times to load different
14+
configs.
15+
"""
16+
def run(args) do
17+
cond do
18+
file = Enum.at(args, 0) ->
19+
load file
20+
File.regular?("config/config.exs") ->
21+
load "config/config.exs"
22+
true ->
23+
:ok
1124
end
25+
26+
Mix.Task.reenable "loadconfig"
27+
end
28+
29+
defp load(file) do
30+
Mix.Config.persist Mix.Config.read! file
1231
end
1332
end

lib/mix/test/mix/tasks/loadconfig_test.exs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@ defmodule Mix.Tasks.LoadconfigTest do
1616
:ok
1717
end
1818

19-
test "reads and persists application configuration" do
19+
test "reads and persists project configuration" do
2020
Mix.Project.push MixTest.Case.Sample
2121

2222
in_fixture "no_mixfile", fn ->
2323
write_config """
24-
[my_app: [key: :value]]
24+
[my_app: [key: :project]]
2525
"""
2626

2727
assert Application.fetch_env(:my_app, :key) == :error
28-
Mix.Tasks.Loadconfig.run []
29-
assert Application.fetch_env(:my_app, :key) == {:ok, :value}
28+
Mix.Task.run "loadconfig", []
29+
assert Application.fetch_env(:my_app, :key) == {:ok, :project}
30+
31+
# App configuration should have lower precedence
3032
:ok = :application.load({:application, :my_app, [vsn: '1.0.0', env: [key: :app]]})
33+
assert Application.fetch_env(:my_app, :key) == {:ok, :project}
34+
35+
# laodconfig can be called multiple times
36+
# Later values should have higher precedence
37+
Mix.Task.run "loadconfig", [fixture_path("configs/good_config.exs")]
3138
assert Application.fetch_env(:my_app, :key) == {:ok, :value}
3239
end
3340
end

0 commit comments

Comments
 (0)