Skip to content

Commit 99145c7

Browse files
committed
Add Mix.Task.Compiler.run/2
1 parent 17d51d1 commit 99145c7

File tree

5 files changed

+53
-43
lines changed

5 files changed

+53
-43
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ defmodule IEx.Helpers do
175175

176176
defp reenable_tasks(config) do
177177
compilers = config[:compilers] || Mix.compilers()
178-
Mix.Task.Compiler.reenable(compilers: compilers)
178+
Mix.Task.Compiler.reenable(compilers)
179179
end
180180

181181
@doc """

lib/mix/lib/mix/task.compiler.ex

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,39 @@ defmodule Mix.Task.Compiler do
268268
end)
269269
end
270270

271-
# Normalize the compiler result to a diagnostic tuple.
272-
@doc false
273-
def normalize(result, name) do
271+
@doc """
272+
Runs the given list of compilers with the given arguments.
273+
274+
It returns a `{status, diagnostics}` tuple. If a compiler
275+
errors, following compilers do not run.
276+
"""
277+
@doc since: "1.19.0"
278+
def run(compilers, args) do
279+
run(compilers, args, :noop, [])
280+
end
281+
282+
defp run([], _, status, diagnostics) do
283+
{status, diagnostics}
284+
end
285+
286+
defp run([compiler | rest], args, status, diagnostics) do
287+
{new_status, new_diagnostics} = run_compiler(compiler, args)
288+
diagnostics = diagnostics ++ new_diagnostics
289+
290+
case new_status do
291+
:error -> {:error, diagnostics}
292+
:ok -> run(rest, args, :ok, diagnostics)
293+
:noop -> run(rest, args, status, diagnostics)
294+
end
295+
end
296+
297+
defp run_compiler(compiler, args) do
298+
result = normalize(Mix.Task.run("compile.#{compiler}", args), compiler)
299+
Enum.reduce(Mix.ProjectStack.pop_after_compiler(compiler), result, & &1.(&2))
300+
end
301+
302+
# Normalize the compiler result to a diagnostic tuple
303+
defp normalize(result, name) do
274304
case result do
275305
{status, diagnostics} when status in [:ok, :noop, :error] and is_list(diagnostics) ->
276306
{status, diagnostics}
@@ -328,10 +358,10 @@ defmodule Mix.Task.Compiler do
328358
re-enables `"compile"` and `"compile.all"`.
329359
"""
330360
@doc since: "1.19.0"
331-
@spec reenable(compilers: compilers) :: :ok when compilers: :all | [atom()]
332-
def reenable(opts \\ []) do
361+
@spec reenable(compilers) :: :ok when compilers: :all | [atom()]
362+
def reenable(compilers \\ :all) do
333363
compilers =
334-
case Keyword.get(opts, :compilers, :all) do
364+
case compilers do
335365
:all -> Mix.Task.Compiler.compilers()
336366
list when is_list(list) -> list
337367
end

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule Mix.Tasks.Compile.All do
6262
compile_path = to_charlist(Mix.Project.compile_path())
6363
Code.prepend_path(compile_path)
6464

65-
result =
65+
{status, diagnostics} =
6666
if "--no-compile" in args do
6767
Mix.Task.reenable("compile.all")
6868
{:noop, []}
@@ -72,9 +72,13 @@ defmodule Mix.Tasks.Compile.All do
7272

7373
config
7474
|> Mix.Task.Compiler.compilers()
75-
|> compile(args, :noop, [])
75+
|> Mix.Task.Compiler.run(args)
7676
end
7777

78+
if status == :error and "--return-errors" not in args do
79+
exit({:shutdown, 1})
80+
end
81+
7882
if app_cache do
7983
Mix.AppLoader.write_cache(app_cache, Map.new(loaded_modules))
8084
end
@@ -90,38 +94,9 @@ defmodule Mix.Tasks.Compile.All do
9094
end
9195
end
9296

93-
result
94-
end
95-
96-
defp compile([], _, status, diagnostics) do
9797
{status, diagnostics}
9898
end
9999

100-
defp compile([compiler | rest], args, status, diagnostics) do
101-
{new_status, new_diagnostics} = run_compiler(compiler, args)
102-
diagnostics = diagnostics ++ new_diagnostics
103-
104-
case new_status do
105-
:error ->
106-
if "--return-errors" not in args do
107-
exit({:shutdown, 1})
108-
end
109-
110-
{:error, diagnostics}
111-
112-
:ok ->
113-
compile(rest, args, :ok, diagnostics)
114-
115-
:noop ->
116-
compile(rest, args, status, diagnostics)
117-
end
118-
end
119-
120-
defp run_compiler(compiler, args) do
121-
result = Mix.Task.Compiler.normalize(Mix.Task.run("compile.#{compiler}", args), compiler)
122-
Enum.reduce(Mix.ProjectStack.pop_after_compiler(compiler), result, & &1.(&2))
123-
end
124-
125100
def project_apps(config) do
126101
project = Mix.Project.get!()
127102

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ defmodule Mix.Tasks.Compile do
140140
{res, diagnostics} =
141141
Mix.Task.run("compile.all", args)
142142
|> List.wrap()
143-
|> Enum.map(&Mix.Task.Compiler.normalize(&1, :all))
143+
|> Enum.map(
144+
&case &1 do
145+
:noop -> {:noop, []}
146+
{status, diagnostics} -> {status, diagnostics}
147+
end
148+
)
144149
|> Enum.reduce({:noop, []}, &merge_diagnostics/2)
145150

146151
config = Mix.Project.config()

lib/mix/test/mix/tasks/compile_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ defmodule Mix.Tasks.CompileTest do
133133
assert {:noop, []} = Mix.Task.run("compile")
134134
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
135135

136-
assert :ok = Mix.Task.Compiler.reenable(compilers: [])
136+
assert :ok = Mix.Task.Compiler.reenable([])
137137
assert {:noop, []} = Mix.Task.run("compile")
138138
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
139139

140-
assert :ok = Mix.Task.Compiler.reenable(compilers: ["erlang"])
140+
assert :ok = Mix.Task.Compiler.reenable(["erlang"])
141141
assert {:noop, []} = Mix.Task.run("compile")
142142
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
143143

@@ -151,11 +151,11 @@ defmodule Mix.Tasks.CompileTest do
151151
end
152152
""")
153153

154-
assert :ok = Mix.Task.Compiler.reenable(compilers: [:erlang])
154+
assert :ok = Mix.Task.Compiler.reenable([:erlang])
155155
assert {:noop, []} = Mix.Task.run("compile")
156156
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z2.beam")
157157

158-
assert :ok = Mix.Task.Compiler.reenable(compilers: [:elixir])
158+
assert :ok = Mix.Task.Compiler.reenable([:elixir])
159159
assert {:ok, []} = Mix.Task.run(:compile)
160160
assert File.regular?("_build/dev/lib/sample/ebin/Elixir.Z2.beam")
161161
end)

0 commit comments

Comments
 (0)