Skip to content

Commit

Permalink
Consolidate protocols on demand on reloader, closes #1253
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Nov 30, 2015
1 parent 4c2206b commit c26dfb3
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/phoenix/code_reloader/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,53 @@ defmodule Phoenix.CodeReloader.Server do
end

defp mix_compile(paths, compilers) do
reloadable_paths = Enum.flat_map(paths, &["--elixirc-paths", &1])
opts = Enum.flat_map(paths, &["--elixirc-paths", &1])
Enum.each compilers, &Mix.Task.reenable("compile.#{&1}")

{res, out} =
proxy_io(fn ->
try do
# We call build_structure mostly for Windows
# so any new assets in priv is copied to the
# build directory.
# We call build_structure mostly for Windows so any
# new assets in priv is copied to the build directory.
Mix.Project.build_structure
Enum.each compilers, &Mix.Task.run("compile.#{&1}", reloadable_paths)
res = Enum.flat_map(compilers, &mix_compile_each(&1, opts))

if :ok in res && consolidate_protocols? do
Mix.Task.reenable("compile.protocols")
Mix.Task.run("compile.protocols")
else
res
end
catch
_, _ -> :error
end
end)

cond do
:error in res -> {:error, out}
:ok in res -> :ok
true -> :noop
case res do
:error -> {:error, out}
:noop -> :noop
_ -> :ok
end
end

defp mix_compile_each(compiler, opts) do
# We always wrap in a list because Mix.Task.run
# will return a list in case of umbrella applications.
List.wrap(Mix.Task.run("compile.#{compiler}", opts))
end

defp consolidate_protocols? do
Mix.Project.config[:consolidate_protocols]
end

defp proxy_io(fun) do
original_gl = Process.group_leader
{:ok, proxy_gl} = Proxy.start()
Process.group_leader(self(), proxy_gl)

try do
res = fun.()
{List.wrap(res), Proxy.stop(proxy_gl)}
{res, Proxy.stop(proxy_gl)}
after
Process.group_leader(self(), original_gl)
Process.exit(proxy_gl, :kill)
Expand Down

0 comments on commit c26dfb3

Please sign in to comment.