Skip to content

Commit dad0f3f

Browse files
author
José Valim
committed
Add Mix.Config.persist/1
1 parent 7ca57af commit dad0f3f

File tree

7 files changed

+36
-21
lines changed

7 files changed

+36
-21
lines changed

lib/mix/lib/mix/cli.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ defmodule Mix.CLI do
104104
if File.regular?(path) do
105105
path
106106
|> Mix.Config.read()
107-
|> Mix.Tasks.Loadconfig.set()
107+
|> Mix.Config.persist()
108108
end
109109
end
110110

lib/mix/lib/mix/config.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ defmodule Mix.Config do
1515
config
1616
end
1717

18+
@doc """
19+
Persists the given configuration by modifying
20+
the configured applications environment.
21+
"""
22+
def persist(config) do
23+
for {app, kw} <- config, {k, v} <- kw do
24+
:application.set_env(app, k, v, persist: true)
25+
end
26+
:ok
27+
end
28+
1829
@doc """
1930
Validates a configuration.
2031
"""

lib/mix/lib/mix/tasks/compile.elixir.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ defmodule Mix.Tasks.Compile.Elixir do
271271
@doc """
272272
Compiles stale Elixir files.
273273
274-
It expects a manifest file, all stale files, all source files
275-
available (including the ones that are not stale) and a path
276-
where compiled files will be written to. All paths are required
277-
to be relative to the current working directory.
274+
It expects a manifest file, a flag if compilation should be forced
275+
or not, all source files available (including the ones that are not
276+
stale) and a path where compiled files will be written to. All paths
277+
are required to be relative to the current working directory.
278278
279279
The manifest is written down with information including dependencies
280280
in between modules, which helps it recompile only the modules that

lib/mix/lib/mix/tasks/compile.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ defmodule Mix.Tasks.Compile do
5050
shell.info "\nEnabled compilers: #{Enum.join get_compilers, ", "}"
5151
end
5252

53-
@doc """
54-
Runs this compile task by recursively calling all registered compilers.
55-
"""
5653
def run(args) do
5754
# --no-deps is used only internally. It has not purpose
5855
# from Mix.CLI because the CLI itself already loads

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ defmodule Mix.Tasks.Loadconfig do
22
use Mix.Task
33

44
@moduledoc """
5-
Loads the application configuration.
5+
Loads and persists the project configuration.
66
77
In case the application is an umbrella application, the
88
configuration for all children app will be merged together
99
and, in case there are any conflicts, they need to be resolved
1010
in the umbrella application.
1111
"""
1212

13+
@doc """
14+
Runs this task.
15+
"""
1316
def run(_) do
14-
set load()
17+
Mix.Config.persist load()
1518
end
1619

20+
@doc """
21+
Loads the configuration for the current project.
22+
"""
1723
def load() do
1824
if Mix.Project.get do
1925
project = Mix.Project.config
@@ -25,13 +31,6 @@ defmodule Mix.Tasks.Loadconfig do
2531
end
2632
end
2733

28-
def set(config) do
29-
_ =
30-
for {app, kw} <- config, {k, v} <- kw do
31-
:application.set_env(app, k, v, persist: true)
32-
end
33-
end
34-
3534
defp eval(nil) do
3635
if File.regular?("config/config.exs") do
3736
eval("config/config.exs")

lib/mix/lib/mix/tasks/local.rebar.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ defmodule Mix.Tasks.Local.Rebar do
88

99
@moduledoc """
1010
Fetch a copy of rebar from the given path or url. It defaults to a
11-
rebar copy that ships with Elixir source if available or fetches it from
12-
#{@rebar_url}.
13-
The local copy is stored in your MIX_HOME (defaults to ~/.mix).
11+
rebar copy that ships with Elixir source if available or fetches it
12+
from #{@rebar_url}.
1413
15-
This version of rebar will be used as required by mix deps.compile
14+
The local copy is stored in your MIX_HOME (defaults to ~/.mix).
15+
This version of rebar will be used as required by `mix deps.compile`.
1616
"""
1717

1818
def run(argv) do

lib/mix/test/mix/config_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ defmodule Mix.ConfigTest do
1919
Mix.Config.read fixture_path("configs/bad_root.exs")
2020
end
2121
end
22+
23+
test "persist/1" do
24+
assert Application.get_env(:my_app, :key) == nil
25+
Mix.Config.persist [my_app: [key: :value]]
26+
assert Application.get_env(:my_app, :key) == :value
27+
after
28+
Application.delete_env(:my_app, :key)
29+
end
2230
end

0 commit comments

Comments
 (0)