Repository navigation
Expand file tree
/
Copy pathcli.ex
More file actions
251 lines (204 loc) · 6.47 KB
/
Copy pathcli.ex
File metadata and controls
251 lines (204 loc) · 6.47 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
defmodule Mix.CLI do
@moduledoc false
@doc """
Runs Mix according to the command line arguments.
"""
def main(args \\ System.argv()) do
if env_variable_activated?("MIX_DEBUG") do
IO.puts("-> Running mix CLI")
{time, res} = :timer.tc(&main/2, [args, true])
IO.puts(["<- Ran mix CLI in ", Integer.to_string(div(time, 1000)), "ms"])
res
else
main(args, false)
end
end
defp main(args, debug?) do
Mix.start()
if debug?, do: Mix.debug(true)
if env_variable_activated?("MIX_QUIET"), do: Mix.shell(Mix.Shell.Quiet)
if profile = System.get_env("MIX_PROFILE") do
Mix.State.put(:profile, String.split(profile, ","))
end
case check_for_shortcuts(args) do
:help ->
Mix.shell().info("Mix is a build tool for Elixir")
display_usage()
:version ->
display_version()
nil ->
proceed(args)
end
end
defp proceed(args) do
load_dot_config()
load_mix_exs(args)
project = Mix.Project.get()
{task, args} = get_task(args, project)
ensure_hex(task)
maybe_change_env_and_target(task, project)
run_task(task, args)
end
defp load_mix_exs(args) do
file = System.get_env("MIX_EXS") || "mix.exs"
if File.regular?(file) do
Mix.ProjectStack.post_config(state_loader: {:cli, List.first(args)})
old_undefined = Code.get_compiler_option(:no_warn_undefined)
Code.put_compiler_option(:no_warn_undefined, :all)
Code.compile_file(file)
Code.put_compiler_option(:no_warn_undefined, old_undefined)
end
end
defp get_task(["-" <> _ | _], project) do
task = "mix #{default_task(project)}"
Mix.shell().error(
"** (Mix) Mix only recognizes the options --help and --version.\n" <>
"You may have wanted to invoke a task instead, such as #{inspect(task)}"
)
display_usage()
exit({:shutdown, 1})
end
defp get_task([h | t], _project) do
{h, t}
end
defp get_task([], nil) do
Mix.shell().error(
"** (Mix) \"mix\" with no arguments must be executed in a directory with a mix.exs file"
)
display_usage()
exit({:shutdown, 1})
end
defp get_task([], project) do
{default_task(project), []}
end
defp default_task(project) do
if function_exported?(project, :cli, 0) do
project.cli()[:default_task] || "run"
else
# TODO: Deprecate default_task in v1.19
Mix.Project.config()[:default_task] || "run"
end
end
defp run_task(name, args) do
try do
ensure_no_slashes(name)
# We must go through the task instead of invoking the
# module directly because projects like Nerves alias it.
Mix.Task.run("loadconfig")
Mix.Task.run(name, args)
rescue
# We only rescue exceptions in the Mix namespace,
# all others pass through and raise as usual.
exception ->
case {Mix.debug?(), Map.get(exception, :mix, false)} do
{false, true} ->
simplified_exception(exception, 1)
{false, code} when code in 0..255 ->
simplified_exception(exception, code)
_ ->
reraise exception, __STACKTRACE__
end
end
end
defp simplified_exception(%name{} = exception, code) do
mod = name |> Module.split() |> hd()
Mix.shell().error("** (#{mod}) #{Exception.message(exception)}")
exit({:shutdown, code})
end
defp env_variable_activated?(name) do
System.get_env(name) in ~w(1 true)
end
defp ensure_hex("local.hex"), do: :ok
defp ensure_hex(_task), do: Mix.Hex.ensure_updated?()
defp ensure_no_slashes(task) do
if String.contains?(task, "/") do
raise Mix.NoTaskError, task: task
end
end
defp maybe_change_env_and_target(task, project) do
task = String.to_atom(task)
config = Mix.Project.config()
env = preferred_cli_env(project, task, config)
target = preferred_cli_target(project, task, config)
env && Mix.env(env)
target && Mix.target(target)
if env || target do
reload_project()
end
end
# TODO: Deprecate preferred_cli_env in v1.19
defp preferred_cli_env(project, task, config) do
if function_exported?(project, :cli, 0) || System.get_env("MIX_ENV") do
nil
else
config[:preferred_cli_env][task] || preferred_cli_env(task)
end
end
# TODO: Deprecate preferred_cli_target in v1.19
defp preferred_cli_target(project, task, config) do
if function_exported?(project, :cli, 0) || System.get_env("MIX_TARGET") do
nil
else
config[:preferred_cli_target][task]
end
end
@doc """
Available for backwards compatibility.
"""
def preferred_cli_env(task) when is_atom(task) or is_binary(task) do
case Mix.Task.get(task) do
nil ->
nil
module ->
case List.keyfind(module.__info__(:attributes), :preferred_cli_env, 0) do
{:preferred_cli_env, [setting]} ->
IO.warn(
"""
setting @preferred_cli_env is deprecated inside Mix tasks.
Please remove it from #{inspect(module)} and set your preferred environment in mix.exs instead:
def cli do
[
preferred_envs: [docs: "docs"]
]
end
""",
[]
)
setting
_ ->
nil
end
end
end
defp reload_project() do
if project = Mix.Project.pop() do
%{name: name, file: file} = project
Mix.Project.push(name, file)
end
end
defp load_dot_config do
path = Path.join(Mix.Utils.mix_config(), "config.exs")
if File.regular?(path) do
Mix.Tasks.Loadconfig.load_compile(path)
end
end
defp display_version do
IO.puts(:erlang.system_info(:system_version))
IO.puts("Mix " <> System.build_info()[:build])
end
defp display_usage do
Mix.shell().info("""
Usage: mix [task]
Examples:
mix - Invokes the default task (mix run) in a project
mix new PATH - Creates a new Elixir project at the given path
mix help - Lists all available tasks
mix help TASK - Prints documentation for a given task
The --help and --version options can be given instead of a task for usage and versioning information.
""")
end
# Check for --help or --version in the args
defp check_for_shortcuts([arg]) when arg in ["--help", "-h"], do: :help
defp check_for_shortcuts([arg]) when arg in ["--version", "-v"], do: :version
defp check_for_shortcuts(_), do: nil
end