Skip to content

Commit eee921b

Browse files
pmaJosé Valim
authored andcommitted
Support mix help --search PATTERN
Signed-off-by: José Valim <[email protected]>
1 parent 73c6a0b commit eee921b

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

lib/mix/lib/mix/tasks/help.ex

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ defmodule Mix.Tasks.Help do
88
99
## Arguments
1010
11-
mix help - prints all tasks and their shortdoc
12-
mix help TASK - prints full docs for the given task
13-
mix help --names - prints all task names and aliases
14-
(useful for autocompleting)
11+
mix help - prints all tasks and their shortdoc
12+
mix help TASK - prints full docs for the given task
13+
mix help --search PATTERN - prints all tasks that contain PATTERN in the name
14+
mix help --names - prints all task names and aliases
15+
(useful for autocompleting)
1516
1617
## Colors
1718
@@ -40,24 +41,12 @@ defmodule Mix.Tasks.Help do
4041
def run([]) do
4142
loadpaths!
4243

43-
shell = Mix.shell
4444
modules = Mix.Task.load_all()
4545

46-
docs = for module <- modules,
47-
doc = Mix.Task.shortdoc(module) do
48-
{"mix " <> Mix.Task.task_name(module), doc}
49-
end
50-
51-
max = Enum.reduce docs, 0, fn({task, _}, acc) ->
52-
max(byte_size(task), acc)
53-
end
46+
{docs, max} = build_task_doc_list(modules)
5447

5548
display_default_task_doc(max)
56-
57-
Enum.each Enum.sort(docs), fn({task, doc}) ->
58-
shell.info format_task(task, max, doc)
59-
end
60-
49+
display_task_doc_list(docs, max)
6150
display_iex_task_doc(max)
6251
end
6352

@@ -78,6 +67,22 @@ defmodule Mix.Tasks.Help do
7867
end
7968
end
8069

70+
def run(["--search", pattern]) do
71+
loadpaths!
72+
73+
modules =
74+
Mix.Task.load_all()
75+
|> Enum.filter(&(String.contains?(Mix.Task.task_name(&1), pattern)))
76+
77+
{docs, max} = build_task_doc_list(modules)
78+
79+
display_task_doc_list(docs, max)
80+
end
81+
82+
def run(["--search"]) do
83+
Mix.raise "Unexpected arguments, expected `mix help --search PATTERN`"
84+
end
85+
8186
def run([task]) do
8287
loadpaths!
8388

@@ -147,4 +152,22 @@ defmodule Mix.Tasks.Help do
147152
Mix.shell.info format_task("iex -S mix", max,
148153
"Start IEx and run the default task")
149154
end
155+
156+
defp display_task_doc_list(docs, max) do
157+
Enum.each Enum.sort(docs), fn({task, doc}) ->
158+
Mix.shell.info format_task(task, max, doc)
159+
end
160+
end
161+
162+
defp build_task_doc_list(modules) do
163+
Enum.reduce modules, {[], 0}, fn module, {docs, max} ->
164+
doc = Mix.Task.shortdoc(module)
165+
if doc do
166+
task = "mix " <> Mix.Task.task_name(module)
167+
docs = [{task, doc} | docs]
168+
max = max(byte_size(task), max)
169+
end
170+
{docs, max}
171+
end
172+
end
150173
end

lib/mix/test/mix/tasks/help_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ defmodule Mix.Tasks.HelpTest do
5656
end
5757
end
5858

59+
test "help --search PATTERN" do
60+
in_fixture "no_mixfile", fn ->
61+
Mix.Tasks.Help.run ["--search", "deps"]
62+
assert_received {:mix_shell, :info, ["mix deps" <> _]}
63+
assert_received {:mix_shell, :info, ["mix deps.clean" <> _]}
64+
end
65+
end
66+
67+
test "help --search without pattern" do
68+
assert_raise Mix.Error, "Unexpected arguments, expected `mix help --search PATTERN`", fn ->
69+
Mix.Tasks.Help.run ["--search"]
70+
end
71+
end
72+
73+
test "help --search without results" do
74+
in_fixture "no_mixfile", fn ->
75+
output =
76+
capture_io fn ->
77+
Mix.Tasks.Help.run ["--search", "foo"]
78+
end
79+
80+
assert output == ""
81+
end
82+
end
83+
5984
test "bad arguments" do
6085
assert_raise Mix.Error, "Unexpected arguments, expected `mix help` or `mix help TASK`", fn ->
6186
Mix.Tasks.Help.run ["foo", "bar"]

0 commit comments

Comments
 (0)