Skip to content

Commit 4f2c2ce

Browse files
author
José Valim
committed
Wrap rebar command on windows inside escript.exe
1 parent 078522d commit 4f2c2ce

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

lib/mix/lib/mix/rebar.ex

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
defmodule Mix.Rebar do
22
@moduledoc false
3-
@local_rebar_name "rebar"
43

54
# Make Mix.Rebar work like a project so we can push it into the stack.
65
@doc false
76
def project, do: []
87

98
@doc """
10-
Return the path to the local copy of rebar. Used when building deps.
9+
Returns the path supposed to host the local copy of rebar.
1110
"""
12-
def local_rebar_path, do: Path.join(Mix.Utils.mix_home, @local_rebar_name)
11+
def local_rebar_path, do: Path.join(Mix.Utils.mix_home, "rebar")
12+
13+
@doc """
14+
Returns the path to the global copy of rebar, if one exists.
15+
"""
16+
def global_rebar_cmd do
17+
wrap_cmd System.find_executable("rebar")
18+
end
19+
20+
@doc """
21+
Returns the path to the local copy of rebar, if one exists.
22+
"""
23+
def local_rebar_cmd do
24+
rebar = local_rebar_path
25+
wrap_cmd(if File.regular?(rebar), do: rebar)
26+
end
27+
28+
@doc """
29+
Returns the path to the rebar copy in the current working directory, if one exists.
30+
"""
31+
def cwd_rebar_cmd do
32+
wrap_cmd(if File.regular?("./rebar"), do: Path.join(File.cwd!, "rebar"))
33+
end
1334

1435
@doc """
1536
Loads the rebar.config and evaluates rebar.config.script if it
@@ -123,4 +144,12 @@ defmodule Mix.Rebar do
123144
:erl_eval.add_binding(k, v, binds)
124145
end)
125146
end
147+
148+
defp wrap_cmd(nil), do: nil
149+
defp wrap_cmd(rebar) do
150+
case :os.type do
151+
{ :win32, _ } -> "escript.exe #{rebar}"
152+
_ -> rebar
153+
end
154+
end
126155
end

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

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,34 @@ defmodule Mix.Tasks.Deps.Compile do
104104
end
105105
end
106106

107+
defp do_rebar(app, root_path) do
108+
do_command app, rebar_cmd(app), "compile skip_deps=true deps_dir=#{inspect root_path}"
109+
end
110+
111+
@rebar_cmds [:cwd_rebar_cmd, :local_rebar_cmd, :global_rebar_cmd]
112+
113+
defp rebar_cmd(app) do
114+
Enum.find_value(@rebar_cmds, apply(Mix.Rebar, &1, [])) || handle_rebar_not_found(app)
115+
end
116+
117+
defp handle_rebar_not_found(app) do
118+
shell = Mix.shell
119+
shell.info "Could not find rebar, which is needed to build #{app}"
120+
shell.info "I can install a local copy which is just used by mix"
121+
122+
unless shell.yes?("Shall I install this local copy?") do
123+
raise Mix.Error, message: "could not find rebar to compile " <>
124+
"dependency #{app}, please ensure rebar is available"
125+
end
126+
127+
Mix.Task.run "local.rebar", []
128+
Mix.Rebar.local_rebar_cmd || raise Mix.Error, message: "Rebar instalation failed"
129+
end
130+
107131
defp do_command(app, command, extra // "") do
108-
if System.find_executable(command) do
109-
if Mix.shell.cmd("#{command} #{extra}") != 0 do
110-
raise Mix.Error, message: "could not compile dependency #{app}, #{command} command failed. " <>
111-
"In case you want to recompile this dependency, please run: mix deps.compile #{app}"
112-
end
113-
else
114-
raise Mix.Error, message: "could not find executable #{command} to compile " <>
115-
"dependency #{app}, please ensure #{command} is available"
132+
if Mix.shell.cmd("#{command} #{extra}") != 0 do
133+
raise Mix.Error, message: "could not compile dependency #{app}, #{command} command failed. " <>
134+
"In case you want to recompile this dependency, please run: mix deps.compile #{app}"
116135
end
117136
end
118137

@@ -127,32 +146,4 @@ defmodule Mix.Tasks.Deps.Compile do
127146
"In case you want to recompile this dependency, please run: mix deps.compile #{app}"
128147
end
129148
end
130-
131-
defp do_rebar(app, root_path) do
132-
do_command app, find_rebar(app), "compile skip_deps=true deps_dir=#{inspect root_path}"
133-
end
134-
135-
defp find_rebar(app) do
136-
cond do
137-
File.regular?("./rebar") ->
138-
Path.join(File.cwd!, "rebar")
139-
140-
File.regular?(Mix.Rebar.local_rebar_path) ->
141-
Mix.Rebar.local_rebar_path
142-
143-
System.find_executable("rebar") ->
144-
"rebar"
145-
146-
true ->
147-
shell = Mix.shell
148-
shell.info "Could not find rebar, which is needed to build #{app}"
149-
shell.info "I can install a local copy which is just used by mix"
150-
unless shell.yes?("Shall I install this local copy?") do
151-
raise Mix.Error, message: "could not find rebar to compile " <>
152-
"dependency #{app}, please ensure rebar is available"
153-
end
154-
Mix.Task.run "local.rebar", []
155-
Mix.Tasks.local_rebar_path
156-
end
157-
end
158149
end

0 commit comments

Comments
 (0)