Skip to content

Commit 1612865

Browse files
author
José Valim
committed
Make Mix.Project.load_project private in favor of in_project
1 parent b97343f commit 1612865

File tree

3 files changed

+64
-72
lines changed

3 files changed

+64
-72
lines changed

lib/mix/lib/mix/deps/converger.ex

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,17 @@ defmodule Mix.Deps.Converger do
117117

118118
# The dependency contains a Mixfile, so let's
119119
# load it and retrieve its nested dependencies.
120-
defp nested_deps(Mix.Dep[app: app, opts: opts], config) do
121-
File.cd! opts[:dest], fn ->
122-
env = opts[:env] || :prod
123-
old_env = Mix.env
120+
defp nested_deps(Mix.Dep[app: app, opts: opts], post_config) do
121+
env = opts[:env] || :prod
122+
old_env = Mix.env
124123

125-
try do
126-
Mix.env(env)
127-
project = Mix.Project.load_project(app, config)
128-
129-
try do
130-
{ project, Enum.reverse Mix.Deps.Project.all }
131-
after
132-
Mix.Project.pop
133-
end
134-
after
135-
Mix.env(old_env)
136-
end
124+
try do
125+
Mix.env(env)
126+
Mix.Project.in_project(app, opts[:dest], post_config, fn config ->
127+
{ config, Enum.reverse Mix.Deps.Project.all }
128+
end)
129+
after
130+
Mix.env(old_env)
137131
end
138132
end
139133
end

lib/mix/lib/mix/project.ex

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ defmodule Mix.Project do
128128
end
129129

130130
@doc """
131-
Returns if project is an umbrella project.
131+
Returns true if project is an umbrella project.
132132
"""
133133
def umbrella? do
134134
config[:apps_path] != nil
@@ -141,49 +141,22 @@ defmodule Mix.Project do
141141
Path.expand(config[:apps_path])
142142
end
143143

144-
@doc """
145-
Loads mix.exs in the current directory or loads the project from the
146-
mixfile cache and pushes the project to the project stack. Optionally
147-
takes a post_config.
148-
"""
149-
def load_project(app, post_config // []) do
150-
if cached = Mix.Server.call({ :mixfile_cache, app }) do
151-
Mix.Project.post_config(post_config)
152-
Mix.Project.push(cached)
153-
cached
154-
else
155-
old_proj = Mix.Project.get
156-
157-
if File.regular?("mix.exs") do
158-
Mix.Project.post_config(post_config)
159-
Code.load_file "mix.exs"
160-
end
161-
162-
new_proj = Mix.Project.get
163-
164-
if old_proj == new_proj do
165-
new_proj = nil
166-
Mix.Project.push new_proj
167-
end
168-
169-
Mix.Server.cast({ :mixfile_cache, app, new_proj })
170-
new_proj
171-
end
172-
end
173-
174144
@doc """
175145
Run fun for every application in the umbrella project. Changes current
176146
project and working directory.
177147
"""
178148
def recursive(fun) do
179-
paths = Path.wildcard(Path.join(Mix.project[:apps_path], "*"))
149+
apps_path = config[:apps_path]
150+
paths = Path.wildcard(Path.join(apps_path, "*"))
151+
180152
projects = Enum.map paths, fn path ->
181153
dir = Path.basename(path)
182154
app = dir |> String.downcase |> binary_to_atom
183155
{ app, path }
184156
end
185157

186-
projects = topsort_projects(projects)
158+
projects = topsort_projects(projects, Path.expand(apps_path))
159+
187160
results = Enum.map projects, fn { app, app_path } ->
188161
in_project(app, app_path, fun)
189162
end
@@ -192,16 +165,15 @@ defmodule Mix.Project do
192165
end
193166

194167
@doc """
195-
Run fun in the context of project app and working directory app_path.
196-
Optionally takes a post_config.
168+
Runs the given `fun` inside the given project by changing
169+
the current working directory and loading the given project
170+
into the project stack.
197171
"""
198172
def in_project(app, app_path, post_config // [], fun) do
199-
umbrella_path = apps_path
200-
201173
File.cd! app_path, fn ->
202-
load_project(app, post_config)
174+
cached = load_project(app, post_config)
203175
result = try do
204-
fun.(umbrella_path)
176+
fun.(cached)
205177
after
206178
Mix.Project.pop
207179
end
@@ -232,16 +204,43 @@ defmodule Mix.Project do
232204
paths ++ compile_paths
233205
end
234206

207+
# Loads mix.exs in the current directory or loads the project from the
208+
# mixfile cache and pushes the project to the project stack.
209+
defp load_project(app, post_config) do
210+
if cached = Mix.Server.call({ :mixfile_cache, app }) do
211+
Mix.Project.post_config(post_config)
212+
Mix.Project.push(cached)
213+
cached
214+
else
215+
old_proj = Mix.Project.get
216+
217+
if File.regular?("mix.exs") do
218+
Mix.Project.post_config(post_config)
219+
Code.load_file "mix.exs"
220+
end
221+
222+
new_proj = Mix.Project.get
223+
224+
if old_proj == new_proj do
225+
new_proj = nil
226+
Mix.Project.push new_proj
227+
end
228+
229+
Mix.Server.cast({ :mixfile_cache, app, new_proj })
230+
new_proj
231+
end
232+
end
233+
235234
# Sort projects in dependency order
236-
defp topsort_projects(projects) do
235+
defp topsort_projects(projects, apps_path) do
237236
graph = :digraph.new
238237

239238
Enum.each projects, fn { app, app_path } ->
240239
:digraph.add_vertex(graph, app, app_path)
241240
end
242241

243242
Enum.each projects, fn { app, app_path } ->
244-
in_project app, app_path, fn apps_path ->
243+
in_project app, app_path, fn _ ->
245244
Enum.each Mix.Deps.children, fn dep ->
246245
if Mix.Deps.available?(dep) and Mix.Deps.in_umbrella?(dep, apps_path) do
247246
:digraph.add_edge(graph, dep.app, app)

lib/mix/test/mix/umbrella_test.exs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,27 @@ defmodule Mix.UmbrellaTest do
55

66
test "compile umbrella" do
77
in_fixture "umbrella_dep/deps/umbrella", fn ->
8-
Mix.Project.load_project(:umbrella)
9-
Mix.Task.run "compile"
10-
11-
assert_received { :mix_shell, :info, ["==> bar"] }
12-
assert_received { :mix_shell, :info, ["Compiled lib/bar.ex"] }
13-
assert_received { :mix_shell, :info, ["Generated bar.app"] }
14-
assert_received { :mix_shell, :info, ["==> foo"] }
15-
assert_received { :mix_shell, :info, ["Compiled lib/foo.ex"] }
16-
assert_received { :mix_shell, :info, ["Generated foo.app"] }
8+
Mix.Project.in_project(:umbrella, ".", fn _ ->
9+
Mix.Task.run "compile"
10+
assert_received { :mix_shell, :info, ["==> bar"] }
11+
assert_received { :mix_shell, :info, ["Compiled lib/bar.ex"] }
12+
assert_received { :mix_shell, :info, ["Generated bar.app"] }
13+
assert_received { :mix_shell, :info, ["==> foo"] }
14+
assert_received { :mix_shell, :info, ["Compiled lib/foo.ex"] }
15+
assert_received { :mix_shell, :info, ["Generated foo.app"] }
16+
end)
1717
end
1818
after
19-
Mix.Project.pop
2019
purge [Umbrella.Mixfile, Foo, Foo.Mix, Bar, Bar.Mix]
2120
end
2221

2322
test "umbrella as dependency" do
2423
in_fixture "umbrella_dep", fn ->
25-
Mix.Project.load_project(:umbrella_dep)
26-
Mix.Tasks.Deps.Compile.run []
27-
Mix.Tasks.Deps.Loadpaths.run []
28-
29-
assert "hello world" == Bar.bar
24+
Mix.Project.in_project(:umbrella_dep, ".", fn _ ->
25+
Mix.Tasks.Deps.Compile.run []
26+
Mix.Tasks.Deps.Loadpaths.run []
27+
assert "hello world" == Bar.bar
28+
end)
3029
end
3130
after
3231
Mix.Project.pop

0 commit comments

Comments
 (0)