Skip to content

Commit 37433e8

Browse files
committed
Add Compiler.reenable/1 to reenable compilers
1 parent 13a07cc commit 37433e8

File tree

5 files changed

+44
-52
lines changed

5 files changed

+44
-52
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,7 @@ defmodule Mix.Task.Compiler do
175175
"""
176176
@callback clean() :: any
177177

178-
@doc """
179-
A compiler task (as well as a regular task) may return a list of
180-
dependent tasks if they are to be re-enabled when this task gets
181-
re-enabled.
182-
183-
For instance, if task `"foo"` executes `"bar"` with
184-
`Mix.Task.run("bar")` from `run/1`, this method should be
185-
implemented to return `["bar"]`.
186-
"""
187-
@callback depends_on :: [binary]
188-
189-
@optional_callbacks clean: 0, manifests: 0, depends_on: 0
178+
@optional_callbacks clean: 0, manifests: 0
190179

191180
@doc """
192181
Adds a callback that runs after a given compiler.
@@ -323,4 +312,33 @@ defmodule Mix.Task.Compiler do
323312

324313
Mix.Sync.PubSub.broadcast(build_path, lazy_message)
325314
end
315+
316+
@doc """
317+
Reenables given compilers so they can be executed again down the stack.
318+
319+
If an umbrella project reenables compilers, they are re-enabled for all
320+
child projects.
321+
322+
Default is `[]`, for none compilers to be reenabled.
323+
This task always re-enables `"compiler.all"`.
324+
"""
325+
@spec reenable([{:compilers, compilers}]) :: :ok when compilers: :all | [Mix.Task.task_name()]
326+
def reenable(opts \\ []) do
327+
if not Keyword.keyword?(opts) do
328+
IO.warn(
329+
"Mix.Task.Compiler.reenable/1 expects a keyword list " <>
330+
"as an argument with compilers: [compiler()], " <>
331+
"reenabling no compiler"
332+
)
333+
else
334+
compilers =
335+
case Keyword.get(opts, :compilers, []) do
336+
:all -> Mix.Tasks.Compile.compilers()
337+
list when is_list(list) -> list
338+
end
339+
340+
Enum.each(["loadpaths", "compile", "compile.all"], &Mix.Task.reenable(&1))
341+
Enum.each(compilers, &Mix.Task.reenable("compile.#{&1}"))
342+
end
343+
end
326344
end

lib/mix/lib/mix/task.ex

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,6 @@ defmodule Mix.Task do
131131
"""
132132
@callback run(command_line_args :: [binary]) :: any
133133

134-
@doc """
135-
A task may return a list of dependent tasks
136-
if they are to be re-enabled when this task gets re-enabled.
137-
138-
For instance, if task `"foo"` executes `"bar"` with
139-
`Mix.Task.run("bar")` from `run/1`, this method should be implemented
140-
to return `["bar"]`.
141-
"""
142-
@callback depends_on :: [binary]
143-
144-
@optional_callbacks depends_on: 0
145-
146134
@doc false
147135
defmacro __using__(_opts) do
148136
quote do
@@ -630,20 +618,8 @@ defmodule Mix.Task do
630618
end
631619

632620
def reenable(task) when is_binary(task) do
633-
module = get(task)
634-
635-
# it’s safe against `no_return` because otherwise `run/1`
636-
# would be `no_return` as well
637-
if module && function_exported?(module, :depends_on, 0) do
638-
Enum.each(module.depends_on(), &reenable/1)
639-
end
640-
641-
do_reenable(task, module)
642-
end
643-
644-
defp do_reenable(task, module) do
645621
proj = Mix.Project.get()
646-
recursive = module && recursive(module)
622+
recursive = (module = get(task)) && recursive(module)
647623

648624
Mix.TasksServer.delete_many([{:task, task, proj}, {:alias, task, proj}])
649625

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ defmodule Mix.Tasks.Compile.All do
8989
result
9090
end
9191

92-
@impl true
93-
@doc false
94-
def depends_on do
95-
Mix.Project.config()
96-
|> Mix.Tasks.Compile.compilers()
97-
|> Enum.map(&"compile.#{&1}")
98-
end
99-
10092
defp compile([], _, status, diagnostics) do
10193
{status, diagnostics}
10294
end

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ defmodule Mix.Tasks.Compile do
174174
{res, diagnostics}
175175
end
176176

177-
@impl true
178-
def depends_on do
179-
["loadpaths", "compile.all"]
180-
end
181-
182177
defp format(expression, args) do
183178
:io_lib.format(expression, args) |> IO.iodata_to_binary()
184179
end

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,20 @@ defmodule Mix.Tasks.CompileTest do
123123
end
124124
""")
125125

126-
Mix.Task.reenable("compile")
127-
assert {:ok, []} = Mix.Task.run("compile")
126+
assert :ok = Mix.Task.reenable("compile")
127+
assert {:noop, []} = Mix.Task.run("compile")
128+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z.beam")
129+
130+
assert :ok = Mix.Task.Compiler.reenable()
131+
assert {:noop, []} = Mix.Task.run("compile")
132+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z.beam")
128133

134+
assert :ok = Mix.Task.Compiler.reenable(compilers: ["erlang"])
135+
assert {:noop, []} = Mix.Task.run("compile")
136+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z.beam")
137+
138+
assert :ok = Mix.Task.Compiler.reenable(compilers: ["elixir", "erlang"])
139+
assert {:ok, []} = Mix.Task.run("compile")
129140
assert File.regular?("_build/dev/lib/sample/ebin/Elixir.Z.beam")
130141
end)
131142
end

0 commit comments

Comments
 (0)