-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
eval.ex
100 lines (79 loc) · 2.92 KB
/
eval.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
defmodule Mix.Tasks.Eval do
use Mix.Task
@shortdoc "Evaluates the given code"
@moduledoc """
Evaluates the given code within a configured application.
$ mix eval "IO.puts(1 + 2)"
The given code is evaluated after the current application
has been configured but without loading or starting them
(some applications may be loaded as part of booting but
that's not guaranteed). See `mix run` for running your
application and scripts within a started application.
This task is designed to mirror the `bin/my_app eval` command
in releases. It is typically used to invoke functions already
defined within your application. For example, you may have a
module such as:
defmodule MyApp.ReleaseTasks do
def migrate_database do
Application.load(:my_app)
...
end
end
Once defined, you can invoke this function either via `mix eval` or
via `bin/my_app eval` inside a release as follows:
$ mix eval MyApp.ReleaseTasks.migrate_database
$ bin/my_app eval MyApp.ReleaseTasks.migrate_database
As you can see, the current application has to be either explicitly
loaded or started in your tasks, either by calling `Application.load/1`
or `Application.ensure_all_started/1`. This gives you full control over
the application booting life-cycle. For more information, see the
`Application` module.
This task is automatically re-enabled, so it can be called multiple
times with different arguments.
## Command-line options
* `--no-archives-check` - does not check archives
* `--no-compile` - does not compile even if files require compilation
* `--no-deps-check` - does not check dependencies
* `--no-elixir-version-check` - does not check the Elixir version from mix.exs
* `--no-mix-exs` - allows the command to run even if there is no mix.exs
"""
@impl true
def run(args) do
{_opts, head} =
OptionParser.parse_head!(
args,
strict: [
mix_exs: :boolean,
compile: :boolean,
deps_check: :boolean,
archives_check: :boolean,
elixir_version_check: :boolean
]
)
case head do
[to_eval | argv] ->
cond do
Mix.Project.get() ->
Mix.Task.run("app.config", ["--no-app-loading" | args])
"--no-mix-exs" in args ->
:ok
true ->
Mix.raise(
"Cannot execute \"mix eval\" without a Mix.Project, " <>
"please ensure you are running Mix in a directory with a mix.exs file " <>
"or pass the --no-mix-exs option"
)
end
old_argv = System.argv()
try do
System.argv(argv)
Code.eval_string(to_eval)
Mix.Task.reenable("eval")
after
System.argv(old_argv)
end
_ ->
Mix.raise("\"mix eval\" expects a single string to evaluate as argument")
end
end
end