Skip to content

Commit 6dc0d6f

Browse files
authored
Add Mix.Tasks.Compile.reenable (#13771)
1 parent d9c536c commit 6dc0d6f

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ defmodule IEx.Helpers do
169169
end
170170

171171
defp reenable_tasks(config) do
172-
Mix.Task.reenable("compile")
173-
Mix.Task.reenable("compile.all")
174172
compilers = config[:compilers] || Mix.compilers()
175-
Enum.each(compilers, &Mix.Task.reenable("compile.#{&1}"))
173+
Mix.Task.Compiler.reenable(compilers: compilers)
176174
end
177175

178176
@doc """

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,28 @@ defmodule Mix.Task.Compiler do
312312

313313
Mix.Sync.PubSub.broadcast(build_path, lazy_message)
314314
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 `:all` which effectively means all the compilers returned by
323+
`Mix.Task.Compiler.compilers()` are to be reenabled. This task always
324+
re-enables `"compile"` and `"compile.all"`.
325+
"""
326+
@doc since: "1.19.0"
327+
@spec reenable(compilers: compilers) :: :ok when compilers: :all | [atom()]
328+
def reenable(opts \\ []) do
329+
compilers =
330+
case Keyword.get(opts, :compilers, :all) do
331+
:all -> Mix.Task.Compiler.compilers()
332+
list when is_list(list) -> list
333+
end
334+
335+
Mix.Task.reenable("compile")
336+
Mix.Task.reenable("compile.all")
337+
Enum.each(compilers, &Mix.Task.reenable("compile.#{&1}"))
338+
end
315339
end

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,51 @@ defmodule Mix.Tasks.CompileTest do
112112
end)
113113
end
114114

115+
test "reenables compilers" do
116+
in_fixture("no_mixfile", fn ->
117+
assert Mix.Tasks.Compile.run(["--verbose"]) == {:ok, []}
118+
Mix.shell().flush()
119+
120+
File.mkdir_p!("lib/foo")
121+
122+
File.write!("lib/foo/z1.ex", """
123+
defmodule Z1 do
124+
def ok, do: :ok
125+
end
126+
""")
127+
128+
assert :ok = Mix.Task.reenable("compile")
129+
assert {:noop, []} = Mix.Task.run("compile")
130+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
131+
132+
assert :ok = Mix.Task.Compiler.reenable(compilers: [])
133+
assert {:noop, []} = Mix.Task.run("compile")
134+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
135+
136+
assert :ok = Mix.Task.Compiler.reenable(compilers: ["erlang"])
137+
assert {:noop, []} = Mix.Task.run("compile")
138+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
139+
140+
assert :ok = Mix.Task.Compiler.reenable()
141+
assert {:ok, []} = Mix.Task.run("compile")
142+
assert File.regular?("_build/dev/lib/sample/ebin/Elixir.Z1.beam")
143+
144+
File.write!("lib/foo/z2.ex", """
145+
defmodule Z2 do
146+
def ko, do: :ko
147+
end
148+
""")
149+
150+
assert :ok = Mix.Task.Compiler.reenable(compilers: [:erlang])
151+
assert {:noop, []} = Mix.Task.run("compile")
152+
refute File.regular?("_build/dev/lib/sample/ebin/Elixir.Z2.beam")
153+
154+
assert :ok = Mix.Task.Compiler.reenable(compilers: [:elixir])
155+
assert {:ok, []} = Mix.Task.run(:compile)
156+
assert File.regular?("_build/dev/lib/sample/ebin/Elixir.Z2.beam")
157+
end)
158+
end
159+
115160
test "recompiles app cache if manifest changes" do
116161
in_fixture("no_mixfile", fn ->
117162
Mix.Tasks.Compile.run(["--force"])

0 commit comments

Comments
 (0)