Skip to content

Commit bcadd96

Browse files
Daniel PerezJosé Valim
Daniel Perez
authored and
José Valim
committed
Revert mix do to enforce space after comma (#4893)
Signed-off-by: José Valim <[email protected]>
1 parent 99a0bfa commit bcadd96

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

lib/mix/lib/mix/tasks/do.ex

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule Mix.Tasks.Do do
66
@moduledoc """
77
Executes the tasks separated by comma.
88
9+
The comma should be followed by a space.
10+
911
## Examples
1012
1113
The example below prints the available compilers and
@@ -24,27 +26,21 @@ defmodule Mix.Tasks.Do do
2426

2527
@doc false
2628
def gather_commands(args) do
27-
gather_commands(args, [], [])
29+
gather_commands args, [], []
2830
end
2931

30-
defp gather_commands([], current, commands) do
31-
[current | commands]
32-
|> Enum.reject(&(&1 == []))
33-
|> Enum.map(&Enum.reverse(&1))
34-
|> Enum.reverse
32+
def gather_commands([head | rest], current, acc)
33+
when binary_part(head, byte_size(head), -1) == "," do
34+
part = binary_part(head, 0, byte_size(head) - 1)
35+
current = Enum.reverse([part | current])
36+
gather_commands rest, [], [current | acc]
3537
end
3638

37-
defp gather_commands([arg | rest], current, commands) do
38-
case String.split(arg, ",", parts: 2) do
39-
[arg] ->
40-
gather_commands(rest, [arg | current], commands)
41-
[left, right] ->
42-
rest = append_unless_empty(right, rest)
43-
current = append_unless_empty(left, current)
44-
gather_commands(rest, [], [current | commands])
45-
end
39+
def gather_commands([head | rest], current, acc) do
40+
gather_commands rest, [head | current], acc
4641
end
4742

48-
defp append_unless_empty("", list), do: list
49-
defp append_unless_empty(h, list), do: [h | list]
43+
def gather_commands([], current, acc) do
44+
Enum.reverse [Enum.reverse(current) | acc]
45+
end
5046
end

lib/mix/test/mix/tasks/do_test.exs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ defmodule Mix.Tasks.DoTest do
1111
end
1212
end
1313

14-
test "gather_command ignore spaces and trailing commas" do
14+
test "gather_command returns a list of commands" do
1515
import Mix.Tasks.Do, only: [gather_commands: 1]
1616
assert gather_commands(["compile", "--list,", "help"]) == [["compile", "--list"], ["help"]]
17-
assert gather_commands(["compile", "--list,help"]) == [["compile", "--list"], ["help"]]
18-
assert gather_commands(["help", ",compile", "--list"]) == [["help"], ["compile", "--list"]]
19-
assert gather_commands(["compile", "--list", ",", "help"]) == [["compile", "--list"], ["help"]]
17+
assert gather_commands(["help,", "compile", "--list"]) == [["help"], ["compile", "--list"]]
2018
assert gather_commands(["compile,", "run", "-e", "IO.puts :hello"]) == [["compile"], ["run", "-e", "IO.puts :hello"]]
21-
assert gather_commands(
22-
[",", "compile,", "run", "-e", "IO.puts :hello",",foo", "--bar", "--baz", ",", "baz,qux,abc", ","]) ==
23-
[["compile"], ["run", "-e", "IO.puts :hello"], ["foo", "--bar", "--baz"], ["baz"], ["qux"], ["abc"]]
19+
assert gather_commands(["compile,", "run", "-e", "[1, 2]"]) == [["compile"], ["run", "-e", "[1, 2]"]]
2420
end
2521
end

0 commit comments

Comments
 (0)