Skip to content

Commit d57658a

Browse files
committed
Add Mix.Rebar.available?/1 for explicit availability checks
1 parent cb5e812 commit d57658a

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

lib/mix/lib/mix/dep/loader.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ defmodule Mix.Dep.Loader do
332332
defp rebar_dep(dep, children, manager, locked?) do
333333
%Mix.Dep{app: app, opts: opts, extra: overrides} = dep
334334

335-
if locked? and is_nil(Mix.Rebar.rebar_path(manager)) do
335+
if locked? and not Mix.Rebar.available?(manager) do
336336
Mix.Tasks.Local.Rebar.run(["--force"])
337337
end
338338

lib/mix/lib/mix/rebar.ex

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,36 @@ defmodule Mix.Rebar do
22
@moduledoc false
33

44
# TODO: Remove on Elixir v1.20 because phx_new and other installers rely on it.
5-
@deprecated "Use global_rebar_path/1 instead"
5+
@deprecated "Use env_rebar_path/1 instead"
66
def global_rebar_cmd(manager) do
7-
global_rebar_path(manager)
7+
env_rebar_path(manager)
88
end
99

1010
@deprecated "Use local_rebar_path/1 instead"
1111
def local_rebar_cmd(manager) do
1212
local_rebar_path(manager)
1313
end
1414

15-
@deprecated "Use rebar_path/1 instead"
15+
@deprecated "Use rebar_args/2 or available?/1 instead"
1616
def rebar_cmd(manager) do
1717
global_rebar_cmd(manager) || local_rebar_cmd(manager)
1818
end
1919

20+
@doc """
21+
Returns if Rebar is available or not.
22+
"""
23+
def available?(manager) do
24+
env_rebar_path(manager) != nil or File.regular?(local_rebar_path(manager))
25+
end
26+
2027
@doc """
2128
Receives a Rebar executable and returns how it must be invoked.
29+
30+
It returns a result even if Rebar is not available.
2231
"""
23-
def rebar_args(rebar, args) do
32+
def rebar_args(:rebar3, args) do
33+
rebar = env_rebar_path(:rebar3) || local_rebar_path(:rebar3)
34+
2435
if match?({:win32, _}, :os.type()) and not String.ends_with?(rebar, ".cmd") do
2536
{"escript.exe", [rebar | args]}
2637
else
@@ -29,16 +40,9 @@ defmodule Mix.Rebar do
2940
end
3041

3142
@doc """
32-
Returns either the global or local rebar path.
33-
"""
34-
def rebar_path(:rebar3) do
35-
global_rebar_path(:rebar3) || local_rebar_path(:rebar3)
36-
end
37-
38-
@doc """
39-
Finds the global rebar path.
43+
Returns the global rebar path.
4044
"""
41-
def global_rebar_path(:rebar3) do
45+
def env_rebar_path(:rebar3) do
4246
System.get_env("MIX_REBAR3")
4347
end
4448

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ defmodule Mix.Tasks.Deps.Compile do
180180
end
181181

182182
defp do_rebar3(%Mix.Dep{opts: opts} = dep, config) do
183+
unless Mix.Rebar.available?(:rebar3) do
184+
handle_rebar_not_found(dep)
185+
end
186+
183187
dep_path = opts[:dest]
184188
build_path = opts[:build]
185189
File.mkdir_p!(build_path)
@@ -207,8 +211,7 @@ defmodule Mix.Tasks.Deps.Compile do
207211
{"TERM", "dumb"}
208212
]
209213

210-
rebar = Mix.Rebar.local_rebar_path(:rebar3) || handle_rebar_not_found(dep)
211-
{exec, args} = Mix.Rebar.rebar_args(rebar, ["bare", "compile", "--paths", lib_path])
214+
{exec, args} = Mix.Rebar.rebar_args(:rebar3, ["bare", "compile", "--paths", lib_path])
212215

213216
if Mix.shell().cmd({exec, args}, opts_for_cmd(dep, config, env)) != 0 do
214217
Mix.raise(
@@ -253,7 +256,10 @@ defmodule Mix.Tasks.Deps.Compile do
253256
end
254257

255258
Mix.Tasks.Local.Rebar.run(["--force"])
256-
Mix.Rebar.local_rebar_path(manager) || Mix.raise("\"#{manager}\" installation failed")
259+
260+
unless Mix.Rebar.available?(manager) do
261+
Mix.raise("\"#{manager}\" installation failed")
262+
end
257263
end
258264

259265
defp do_make(dep, config) do

lib/mix/test/mix/rebar_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ defmodule Mix.RebarTest do
240240
in_tmp("rebar3 env with spaces", fn ->
241241
File.cp!(Mix.Rebar.local_rebar_path(:rebar3), "rebar3")
242242
System.put_env("MIX_REBAR3", Path.absname("rebar3"))
243-
assert Mix.Rebar.rebar_path(:rebar3) =~ " "
243+
assert Mix.Rebar.rebar_args(:rebar3, []) |> elem(0) =~ " "
244244

245245
Mix.Project.push(RebarAsDep)
246246
Mix.Tasks.Deps.Get.run([])

0 commit comments

Comments
 (0)