Skip to content

Commit f65ef1d

Browse files
author
José Valim
committed
Remove duplication in deps.compile
1 parent 356b74d commit f65ef1d

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

lib/mix/lib/mix/deps.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ defmodule Mix.Deps do
175175
changing the current working directory and loading the given
176176
project onto the project stack.
177177
178+
This function only works for mix dependencies.
178179
It is expected a fetched dependency as argument.
179180
"""
180181
def in_dependency(dep, post_config // [], fun)

lib/mix/lib/mix/deps/retriever.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ defmodule Mix.Deps.Retriever do
144144
end)
145145
end
146146

147-
defp rebar_dep(Mix.Dep[opts: opts] = dep, config) do
147+
defp rebar_dep(Mix.Dep[opts: opts] = dep, _config) do
148148
File.cd!(opts[:dest], fn ->
149149
config = Mix.Rebar.load_config(".")
150150
extra = Dict.take(config, [:sub_dirs])

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ defmodule Mix.Tasks.Deps.Compile do
6767

6868
compiled = cond do
6969
not nil?(opts[:compile]) ->
70-
File.cd! deps_path, fn -> do_compile app, opts[:compile] end
70+
do_compile app, deps_path, opts[:compile]
7171
mix?(dep) ->
72-
File.cd! deps_path, fn -> do_mix dep, config end
72+
do_mix dep, config
7373
rebar?(dep) ->
74-
File.cd! deps_path, fn -> do_rebar app, root_path end
74+
do_rebar app, deps_path, root_path
7575
make?(dep) ->
76-
File.cd! deps_path, fn -> do_command app, "make" end
76+
do_command app, deps_path, "make"
7777
true ->
7878
shell.error "Could not compile #{app}, no mix.exs, rebar.config or Makefile " <>
7979
"(pass :compile as an option to customize compilation, set it to false to do nothing)"
@@ -95,29 +95,24 @@ defmodule Mix.Tasks.Deps.Compile do
9595
:ok
9696
end
9797

98-
defp do_mix(Mix.Dep[app: app, opts: opts], config) do
99-
env = opts[:env] || :prod
100-
old_env = Mix.env
101-
102-
try do
103-
Mix.env(env)
104-
res = Mix.Project.in_project(app, ".", config, fn _ ->
105-
Mix.Task.run "compile", ["--no-deps"]
106-
end)
107-
:ok in List.wrap(res)
108-
catch
109-
kind, reason ->
110-
Mix.shell.error "could not compile dependency #{app}, mix compile failed. " <>
111-
"You can recompile this dependency with `mix deps.compile #{app}` or " <>
112-
"update it with `mix deps.update #{app}`"
113-
:erlang.raise(kind, reason, System.stacktrace)
114-
after
115-
Mix.env(old_env)
98+
defp do_mix(dep, config) do
99+
Mix.Deps.in_dependency dep, config, fn _ ->
100+
try do
101+
res = Mix.Task.run("compile", ["--no-deps"])
102+
:ok in List.wrap(res)
103+
catch
104+
kind, reason ->
105+
app = dep.app
106+
Mix.shell.error "could not compile dependency #{app}, mix compile failed. " <>
107+
"You can recompile this dependency with `mix deps.compile #{app}` or " <>
108+
"update it with `mix deps.update #{app}`"
109+
:erlang.raise(kind, reason, System.stacktrace)
110+
end
116111
end
117112
end
118113

119-
defp do_rebar(app, root_path) do
120-
do_command app, rebar_cmd(app), "compile skip_deps=true deps_dir=#{inspect root_path}"
114+
defp do_rebar(app, deps_path, root_path) do
115+
do_command app, deps_path, rebar_cmd(app), "compile skip_deps=true deps_dir=#{inspect root_path}"
121116
end
122117

123118
defp rebar_cmd(app) do
@@ -138,23 +133,21 @@ defmodule Mix.Tasks.Deps.Compile do
138133
Mix.Rebar.local_rebar_cmd || raise Mix.Error, message: "rebar instalation failed"
139134
end
140135

141-
defp do_command(app, command, extra // "") do
142-
if Mix.shell.cmd("#{command} #{extra}") != 0 do
143-
raise Mix.Error, message: "Could not compile dependency #{app}, #{command} command failed. " <>
144-
"If you want to recompile this dependency, please run: mix deps.compile #{app}"
145-
end
146-
true
147-
end
148-
149-
defp do_compile(_, false) do
136+
defp do_compile(_, _deps_path, false) do
150137
false
151138
end
152139

153-
defp do_compile(app, command) when is_binary(command) do
140+
defp do_compile(app, deps_path, command) when is_binary(command) do
154141
Mix.shell.info("#{app}: #{command}")
155-
if Mix.shell.cmd(command) != 0 do
156-
raise Mix.Error, message: "Could not compile dependency #{app}, custom #{command} command failed. " <>
157-
"If you want to recompile this dependency, please run: mix deps.compile #{app}"
142+
do_command(app, deps_path, command)
143+
end
144+
145+
defp do_command(app, deps_path, command, extra // "") do
146+
File.cd! deps_path, fn ->
147+
if Mix.shell.cmd("#{command} #{extra}") != 0 do
148+
raise Mix.Error, message: "Could not compile dependency #{app}, #{command} command failed. " <>
149+
"If you want to recompile this dependency, please run: mix deps.compile #{app}"
150+
end
158151
end
159152
true
160153
end

0 commit comments

Comments
 (0)