Skip to content

Commit 88be750

Browse files
author
José Valim
committed
import_config/1 now accepts wildcards
1 parent 032c8b8 commit 88be750

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

lib/mix/lib/mix/config.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ defmodule Mix.Config do
110110
111111
Or to import files from children in umbrella projects:
112112
113-
import_config "../apps/child/config/config.exs"
113+
import_config "../apps/*/config/config.exs"
114114
115115
"""
116116
defmacro import_config(file) do
117117
quote do
118118
var!(config, Mix.Config) =
119-
Mix.Config.merge(var!(config, Mix.Config),
120-
Mix.Config.read!(Path.expand(unquote(file), __DIR__)))
119+
Mix.Config.read_wildcard!(Path.expand(unquote(file), __DIR__), var!(config, Mix.Config))
121120
end
122121
end
123122

@@ -140,6 +139,17 @@ defmodule Mix.Config do
140139
end
141140
end
142141

142+
@doc """
143+
Reads many configuration files given by wildcard into a single config.
144+
"""
145+
def read_wildcard!(path, config) do
146+
paths = case Path.wildcard(path) do
147+
[] -> [path]
148+
o -> o
149+
end
150+
Enum.reduce(paths, config, &merge(&2, read!(&1)))
151+
end
152+
143153
@doc """
144154
Persists the given configuration by modifying
145155
the configured applications environment.

lib/mix/lib/mix/tasks/new.ex

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,22 @@ defmodule Mix.Tasks.New do
314314
"""
315315

316316
embed_template :config_umbrella, ~S"""
317+
# This file is responsible for configuring your application
318+
# and its dependencies with the aid of the Mix.Config module.
319+
use Mix.Config
320+
321+
# The configuration defined here will only affect the dependencies
322+
# in the apps directory when commands are executed from the umbrella
323+
# project. For this reason, it is preferred to configure each child
324+
# application directly and import its configuration, as done below.
325+
import_config "../apps/*/config/config.exs"
317326
318-
# Finally, note that configuration defined in children projects
319-
# inside apps/ are not automatically available to the umbrella parent.
320-
# They can, however, be easily imported:
327+
# Sample configuration (overrides the imported configuration above):
321328
#
322-
# import_config "../apps/foo/config/config.exs"
323-
# import_config "../apps/bar/config/config.exs"
329+
# config :logger, :console,
330+
# level: :info,
331+
# format: "$date $time [$level] $metadata$message\n",
332+
# metadata: [:user_id]
324333
"""
325334

326335
embed_template :lib, """

lib/mix/test/mix/config_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ defmodule Mix.ConfigTest do
3838
assert var!(config, Mix.Config) == [my_app: [key: :value]]
3939
end
4040

41+
test "import_config/1 with wildcards" do
42+
use Mix.Config
43+
import_config fixture_path("configs/good_*.exs")
44+
assert var!(config, Mix.Config) == [my_app: [key: :value]]
45+
end
46+
47+
test "import_config/1 with bad path" do
48+
use Mix.Config
49+
50+
assert_raise Mix.Config.LoadError, ~r"could not load config", fn ->
51+
import_config fixture_path("configs/unknown.exs")
52+
end
53+
end
54+
4155
test "read!/1" do
4256
assert Mix.Config.read!(fixture_path("configs/good_config.exs")) ==
4357
[my_app: [key: :value]]

0 commit comments

Comments
 (0)