Skip to content

Commit cf7f205

Browse files
stoshfabriciusjosevalim
authored andcommitted
Run formatter on deps.compile (#6894)
1 parent 9faba94 commit cf7f205

File tree

1 file changed

+109
-66
lines changed

1 file changed

+109
-66
lines changed

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

+109-66
Original file line numberDiff line numberDiff line change
@@ -30,59 +30,68 @@ defmodule Mix.Tasks.Deps.Compile do
3030
`b` is included in the compilation step, pass `--include-children`.
3131
"""
3232

33-
import Mix.Dep, only: [loaded: 1, available?: 1, loaded_by_name: 2,
34-
make?: 1, mix?: 1]
33+
import Mix.Dep, only: [loaded: 1, available?: 1, loaded_by_name: 2, make?: 1, mix?: 1]
3534

3635
@switches [include_children: :boolean, force: :boolean]
3736

38-
@spec run(OptionParser.argv) :: :ok
37+
@spec run(OptionParser.argv()) :: :ok
3938
def run(args) do
4039
unless "--no-archives-check" in args do
41-
Mix.Task.run "archive.check", args
40+
Mix.Task.run("archive.check", args)
4241
end
4342

44-
Mix.Project.get!
43+
Mix.Project.get!()
4544

4645
case OptionParser.parse(args, switches: @switches) do
4746
{opts, [], _} ->
4847
# Because this command may be invoked explicitly with
4948
# deps.compile, we simply try to compile any available
5049
# dependency.
51-
compile(Enum.filter(loaded(env: Mix.env), &available?/1), opts)
50+
compile(Enum.filter(loaded(env: Mix.env()), &available?/1), opts)
51+
5252
{opts, tail, _} ->
53-
compile(loaded_by_name(tail, [env: Mix.env] ++ opts), opts)
53+
compile(loaded_by_name(tail, [env: Mix.env()] ++ opts), opts)
5454
end
5555
end
5656

5757
@doc false
5858
def compile(deps, options \\ []) do
59-
shell = Mix.shell
60-
config = Mix.Project.deps_config
59+
shell = Mix.shell()
60+
config = Mix.Project.deps_config()
6161

62-
Mix.Task.run "deps.precompile"
62+
Mix.Task.run("deps.precompile")
6363

6464
compiled =
6565
Enum.map(deps, fn %Mix.Dep{app: app, status: status, opts: opts, scm: scm} = dep ->
6666
check_unavailable!(app, status)
6767

6868
maybe_clean(app, options)
6969

70-
compiled? = cond do
71-
not is_nil(opts[:compile]) ->
72-
do_compile dep, config
73-
mix?(dep) ->
74-
do_mix dep, config
75-
make?(dep) ->
76-
do_make dep, config
77-
dep.manager == :rebar ->
78-
do_rebar dep, config
79-
dep.manager == :rebar3 ->
80-
do_rebar3 dep, config
81-
true ->
82-
shell.error "Could not compile #{inspect app}, no \"mix.exs\", \"rebar.config\" or \"Makefile\" " <>
83-
"(pass :compile as an option to customize compilation, set it to \"false\" to do nothing)"
84-
false
85-
end
70+
compiled? =
71+
cond do
72+
not is_nil(opts[:compile]) ->
73+
do_compile(dep, config)
74+
75+
mix?(dep) ->
76+
do_mix(dep, config)
77+
78+
make?(dep) ->
79+
do_make(dep, config)
80+
81+
dep.manager == :rebar ->
82+
do_rebar(dep, config)
83+
84+
dep.manager == :rebar3 ->
85+
do_rebar3(dep, config)
86+
87+
true ->
88+
shell.error(
89+
"Could not compile #{inspect(app)}, no \"mix.exs\", \"rebar.config\" or \"Makefile\" " <>
90+
"(pass :compile as an option to customize compilation, set it to \"false\" to do nothing)"
91+
)
92+
93+
false
94+
end
8695

8796
unless mix?(dep), do: build_structure(dep, config)
8897
# We should touch fetchable dependencies even if they
@@ -92,12 +101,12 @@ defmodule Mix.Tasks.Deps.Compile do
92101
compiled? and fetchable?
93102
end)
94103

95-
if true in compiled, do: Mix.Dep.Lock.touch_manifest, else: :ok
104+
if true in compiled, do: Mix.Dep.Lock.touch_manifest(), else: :ok
96105
end
97106

98107
defp maybe_clean(app, opts) do
99108
if Keyword.get(opts, :force, false) do
100-
File.rm_rf! Path.join [Mix.Project.build_path, "lib", Atom.to_string(app)]
109+
File.rm_rf!(Path.join([Mix.Project.build_path(), "lib", Atom.to_string(app)]))
101110
end
102111
end
103112

@@ -112,83 +121,109 @@ defmodule Mix.Tasks.Deps.Compile do
112121
end
113122

114123
defp check_unavailable!(app, {:unavailable, _}) do
115-
Mix.raise "Cannot compile dependency #{inspect app} because " <>
116-
"it isn't available, run \"mix deps.get\" first"
124+
Mix.raise(
125+
"Cannot compile dependency #{inspect(app)} because " <>
126+
"it isn't available, run \"mix deps.get\" first"
127+
)
117128
end
118129

119130
defp check_unavailable!(_, _) do
120131
:ok
121132
end
122133

123134
defp do_mix(dep, _config) do
124-
Mix.Dep.in_dependency dep, fn _ ->
125-
if req = old_elixir_req(Mix.Project.config) do
126-
Mix.shell.error "warning: the dependency #{inspect dep.app} requires Elixir #{inspect req} " <>
127-
"but you are running on v#{System.version}"
135+
Mix.Dep.in_dependency(dep, fn _ ->
136+
if req = old_elixir_req(Mix.Project.config()) do
137+
Mix.shell().error(
138+
"warning: the dependency #{inspect(dep.app)} requires Elixir #{inspect(req)} " <>
139+
"but you are running on v#{System.version()}"
140+
)
128141
end
129142

130143
# Force recompilation on compile status
131144
if dep.status == :compile do
132-
Mix.Dep.Lock.touch_manifest
145+
Mix.Dep.Lock.touch_manifest()
133146
end
134147

135148
try do
136-
res = Mix.Task.run("compile", ["--no-deps", "--no-archives-check",
137-
"--no-elixir-version-check", "--no-warnings-as-errors"])
149+
options = [
150+
"--no-deps",
151+
"--no-archives-check",
152+
"--no-elixir-version-check",
153+
"--no-warnings-as-errors"
154+
]
155+
156+
res = Mix.Task.run("compile", options)
157+
138158
match?({:ok, _}, res)
139159
catch
140160
kind, reason ->
141-
stacktrace = System.stacktrace
161+
stacktrace = System.stacktrace()
142162
app = dep.app
143-
Mix.shell.error "could not compile dependency #{inspect app}, \"mix compile\" failed. " <>
144-
"You can recompile this dependency with \"mix deps.compile #{app}\", update it " <>
145-
"with \"mix deps.update #{app}\" or clean it with \"mix deps.clean #{app}\""
163+
164+
Mix.shell().error(
165+
"could not compile dependency #{inspect(app)}, \"mix compile\" failed. " <>
166+
"You can recompile this dependency with \"mix deps.compile #{app}\", update it " <>
167+
"with \"mix deps.update #{app}\" or clean it with \"mix deps.clean #{app}\""
168+
)
169+
146170
:erlang.raise(kind, reason, stacktrace)
147171
end
148-
end
172+
end)
149173
end
150174

151175
defp do_rebar(dep, config) do
152176
lib_path = Path.join(config[:env_path], "lib")
153-
cmd = "#{rebar_cmd(dep)} compile skip_deps=true deps_dir=#{inspect lib_path}"
154-
do_command dep, config, cmd, false
177+
cmd = "#{rebar_cmd(dep)} compile skip_deps=true deps_dir=#{inspect(lib_path)}"
178+
do_command(dep, config, cmd, false)
155179
end
156180

157181
defp do_rebar3(%Mix.Dep{opts: opts} = dep, config) do
158-
dep_path = opts[:build]
182+
dep_path = opts[:build]
159183
config_path = Path.join(dep_path, "mix.rebar.config")
160-
lib_path = Path.join(config[:env_path], "lib/*/ebin")
184+
lib_path = Path.join(config[:env_path], "lib/*/ebin")
161185

162186
env = [{"REBAR_CONFIG", config_path}, {"TERM", "dumb"}]
163-
cmd = "#{rebar_cmd(dep)} bare compile --paths #{inspect lib_path}"
187+
cmd = "#{rebar_cmd(dep)} bare compile --paths #{inspect(lib_path)}"
164188

165189
File.mkdir_p!(dep_path)
166190
File.write!(config_path, rebar_config(dep))
167-
do_command dep, config, cmd, false, env
191+
do_command(dep, config, cmd, false, env)
168192
end
169193

170194
defp rebar_config(dep) do
171195
dep.extra
172-
|> Mix.Rebar.dependency_config
173-
|> Mix.Rebar.serialize_config
196+
|> Mix.Rebar.dependency_config()
197+
|> Mix.Rebar.serialize_config()
174198
end
175199

176200
defp rebar_cmd(%Mix.Dep{manager: manager} = dep) do
177201
Mix.Rebar.rebar_cmd(manager) || handle_rebar_not_found(dep)
178202
end
179203

180204
defp handle_rebar_not_found(%Mix.Dep{app: app, manager: manager}) do
181-
shell = Mix.shell
182-
shell.info "Could not find \"#{manager}\", which is needed to build dependency #{inspect app}"
183-
shell.info "I can install a local copy which is just used by Mix"
205+
shell = Mix.shell()
206+
207+
shell.info(
208+
"Could not find \"#{manager}\", which is needed to build dependency #{inspect(app)}"
209+
)
184210

185-
unless shell.yes?("Shall I install #{manager}? (if running non-interactively, use \"mix local.rebar --force\")") do
186-
Mix.raise "Could not find \"#{manager}\" to compile " <>
187-
"dependency #{inspect app}, please ensure \"#{manager}\" is available"
211+
shell.info("I can install a local copy which is just used by Mix")
212+
213+
install_question =
214+
"Shall I install #{manager}? (if running non-interactively, " <>
215+
"use \"mix local.rebar --force\")"
216+
217+
unless shell.yes?(install_question) do
218+
error_message =
219+
"Could not find \"#{manager}\" to compile " <>
220+
"dependency #{inspect(app)}, please ensure \"#{manager}\" is available"
221+
222+
Mix.raise(error_message)
188223
end
189224

190225
(Mix.Tasks.Local.Rebar.run([]) && Mix.Rebar.local_rebar_cmd(manager)) ||
191-
Mix.raise "\"#{manager}\" installation failed"
226+
Mix.raise("\"#{manager}\" installation failed")
192227
end
193228

194229
defp do_make(dep, config) do
@@ -200,11 +235,13 @@ defmodule Mix.Tasks.Deps.Compile do
200235
makefile_win? = makefile_win?(dep)
201236

202237
command =
203-
case :os.type do
238+
case :os.type() do
204239
{:win32, _} when makefile_win? ->
205240
"nmake /F Makefile.win"
241+
206242
{:unix, type} when type in [:freebsd, :openbsd] ->
207243
"gmake"
244+
208245
_ ->
209246
"make"
210247
end
@@ -227,34 +264,40 @@ defmodule Mix.Tasks.Deps.Compile do
227264
defp do_command(%Mix.Dep{app: app, opts: opts}, config, command, print_app?, env \\ []) do
228265
File.cd!(opts[:dest], fn ->
229266
env = [{"ERL_LIBS", Path.join(config[:env_path], "lib")}] ++ env
267+
230268
if Mix.Shell.cmd(command, env: env, into: %Mix.Shell{print_app?: print_app?}) != 0 do
231-
Mix.raise "Could not compile dependency #{inspect app}, \"#{command}\" command failed. " <>
232-
"You can recompile this dependency with \"mix deps.compile #{app}\", update it " <>
233-
"with \"mix deps.update #{app}\" or clean it with \"mix deps.clean #{app}\""
269+
Mix.raise(
270+
"Could not compile dependency #{inspect(app)}, \"#{command}\" command failed. " <>
271+
"You can recompile this dependency with \"mix deps.compile #{app}\", update it " <>
272+
"with \"mix deps.update #{app}\" or clean it with \"mix deps.clean #{app}\""
273+
)
234274
end
235275
end)
276+
236277
true
237278
end
238279

239280
defp build_structure(%Mix.Dep{opts: opts} = dep, config) do
240281
build_path = Path.dirname(opts[:build])
241-
Enum.each Mix.Dep.source_paths(dep), fn {source, base} ->
282+
283+
Enum.each(Mix.Dep.source_paths(dep), fn {source, base} ->
242284
app = Path.join(build_path, base)
243285
build_structure(source, app, config)
244286
Code.prepend_path(Path.join(app, "ebin"))
245-
end
287+
end)
246288
end
247289

248290
defp build_structure(dest, build, config) do
249-
File.cd! dest, fn ->
291+
File.cd!(dest, fn ->
250292
config = Keyword.put(config, :app_path, build)
251293
Mix.Project.build_structure(config, symlink_ebin: true)
252-
end
294+
end)
253295
end
254296

255297
defp old_elixir_req(config) do
256298
req = config[:elixir]
257-
if req && not Version.match?(System.version, req) do
299+
300+
if req && not Version.match?(System.version(), req) do
258301
req
259302
end
260303
end

0 commit comments

Comments
 (0)