/
help.ex
236 lines (184 loc) · 6.44 KB
/
help.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
defmodule Mix.Tasks.Help do
use Mix.Task
@shortdoc "Prints help information for tasks"
@moduledoc """
Lists all tasks and aliases or prints the documentation for a given task or alias.
## Arguments
mix help - prints all aliases, tasks and their short descriptions
mix help ALIAS - prints the definition for the given alias
mix help TASK - prints full docs for the given task
mix help --search PATTERN - prints all tasks and aliases that contain PATTERN in the name
mix help --names - prints all task names and aliases
(useful for autocompleting)
## Colors
When possible, `mix help` is going to use coloring for formatting
guides. The formatting can be customized by configuring the Mix
application either inside your project (in `config/config.exs`) or
by using the local config (in `~/.mix/config.exs`).
For example, to disable color, one may use the configuration:
[mix: [colors: [enabled: false]]]
The available color options are:
* `:enabled` - shows ANSI formatting (defaults to `IO.ANSI.enabled?/0`)
* `:doc_code` - the attributes for code blocks (cyan, bright)
* `:doc_inline_code` - inline code (cyan)
* `:doc_headings` - h1 and h2 (yellow, bright)
* `:doc_title` - the overall heading for the output (reverse, yellow, bright)
* `:doc_bold` - (bright)
* `:doc_underline` - (underline)
"""
@impl true
def run(argv)
def run([]) do
loadpaths!()
modules = load_tasks()
aliases = load_aliases()
{docs, max} = build_doc_list(modules, aliases)
display_default_task_doc(max)
display_doc_list(docs, max)
display_iex_task_doc(max)
end
def run(["--names"]) do
loadpaths!()
tasks = Enum.map(load_tasks(), &Mix.Task.task_name/1)
aliases =
Enum.map(Mix.Project.config()[:aliases], fn {alias_name, _} ->
Atom.to_string(alias_name)
end)
for info <- Enum.sort(aliases ++ tasks) do
Mix.shell().info(info)
end
end
def run(["--search", pattern]) do
loadpaths!()
modules = Enum.filter(load_tasks(), &String.contains?(Mix.Task.task_name(&1), pattern))
aliases = Enum.filter(load_aliases(), fn {name, _} -> String.contains?(name, pattern) end)
{docs, max} = build_doc_list(modules, aliases)
display_doc_list(docs, max)
end
def run(["--search"]) do
Mix.raise("Unexpected arguments, expected \"mix help --search PATTERN\"")
end
def run([task]) do
loadpaths!()
opts = Application.get_env(:mix, :colors)
opts =
if ansi_docs?(opts) do
[width: width()] ++ opts
else
opts
end
for doc <- verbose_doc(task) do
print_doc(task, doc, opts)
end
end
def run(_) do
Mix.raise("Unexpected arguments, expected \"mix help\" or \"mix help TASK\"")
end
defp print_doc(task, {doc, location, note}, opts) do
if ansi_docs?(opts) do
opts = [width: width()] ++ opts
IO.ANSI.Docs.print_heading("mix #{task}", opts)
IO.ANSI.Docs.print(doc, opts)
IO.puts("Location: #{location}")
note && IO.puts("") && IO.ANSI.Docs.print(note, opts)
else
IO.puts("# mix #{task}\n")
IO.puts(doc)
IO.puts("\nLocation: #{location}")
note && IO.puts([?\n, note, ?\n, ?\n])
end
end
# Loadpaths without checks because tasks may be defined in deps.
defp loadpaths! do
args = ["--no-elixir-version-check", "--no-deps-check", "--no-archives-check"]
Mix.Task.run("loadpaths", args)
Mix.Task.reenable("loadpaths")
Mix.Task.reenable("deps.loadpaths")
end
defp load_tasks() do
Enum.filter(Mix.Task.load_all(), &(Mix.Task.moduledoc(&1) != false))
end
defp load_aliases() do
aliases = Mix.Project.config()[:aliases]
Map.new(aliases, fn {alias_name, alias_tasks} -> {Atom.to_string(alias_name), alias_tasks} end)
end
defp ansi_docs?(opts) do
Keyword.get(opts, :enabled, IO.ANSI.enabled?())
end
defp width() do
case :io.columns() do
{:ok, width} -> min(width, 80)
{:error, _} -> 80
end
end
defp format_task(task, max, doc) do
String.pad_trailing(task, max) <> " # " <> doc
end
defp where_is_file(module) do
case :code.where_is_file(Atom.to_charlist(module) ++ '.beam') do
:non_existing ->
"not available"
location ->
location
|> Path.dirname()
|> Path.expand()
|> Path.relative_to_cwd()
end
end
defp display_default_task_doc(max) do
message = "Runs the default task (current: \"mix #{Mix.Project.config()[:default_task]}\")"
Mix.shell().info(format_task("mix", max, message))
end
defp display_iex_task_doc(max) do
Mix.shell().info(format_task("iex -S mix", max, "Starts IEx and runs the default task"))
end
defp display_doc_list(docs, max) do
Enum.each(Enum.sort(docs), fn {task, doc} ->
Mix.shell().info(format_task(task, max, doc))
end)
end
defp build_doc_list(modules, aliases) do
{task_docs, task_max} = build_task_doc_list(modules)
{alias_docs, alias_max} = build_alias_doc_list(aliases)
{task_docs ++ alias_docs, max(task_max, alias_max)}
end
defp build_task_doc_list(modules) do
Enum.reduce(modules, {[], 0}, fn module, {docs, max} ->
if doc = Mix.Task.shortdoc(module) do
task = "mix " <> Mix.Task.task_name(module)
{[{task, doc} | docs], max(byte_size(task), max)}
else
{docs, max}
end
end)
end
defp build_alias_doc_list(aliases) do
Enum.reduce(aliases, {[], 0}, fn {alias_name, _task_name}, {docs, max} ->
doc = "Alias defined in mix.exs"
task = "mix " <> alias_name
{[{task, doc} | docs], max(byte_size(task), max)}
end)
end
defp verbose_doc(task) do
aliases = load_aliases()
has_alias? = Map.has_key?(aliases, task)
has_task? = Mix.Task.get(task)
cond do
has_alias? and has_task? ->
note = "There is also a task named \"#{task}\". The documentation is shown next."
[alias_doc(aliases[task], note), task_doc(task)]
has_alias? ->
[alias_doc(aliases[task], nil)]
true ->
[task_doc(task)]
end
end
defp alias_doc(task_name, note) do
{"Alias for " <> inspect(task_name), "mix.exs", note}
end
defp task_doc(task) do
module = Mix.Task.get!(task)
doc = Mix.Task.moduledoc(module) || "There is no documentation for this task"
{doc, where_is_file(module), nil}
end
end