1
1
defmodule Mix.Tasks.Run do
2
2
use Mix.Task
3
3
4
- @ shortdoc "Run the given expression"
4
+ @ shortdoc "Run the given file or expression"
5
5
6
6
@ moduledoc """
7
- Run the given expression in the application context.
7
+ Runs the given file or expession in the context of the application .
8
8
9
9
Before running the code, it invokes the app.start task
10
10
which defaults to compile and load your project.
11
11
12
12
## Command line options
13
13
14
+ * `--eval`, `-e` - Evaluates the given code
14
15
* `--require`, `-r` - Requires pattern before running the command
15
16
* `--parallel-require`, `-pr` - Requires pattern in parallel
16
17
* `--no-halt` - Does not halt the system after running the command
17
- * `--no-compile` - Do not compile even if files require compilation;
18
- * `--no-start` - Do not start applications after compilation;
18
+ * `--no-compile` - Does not compile even if files require compilation
19
+ * `--no-start` - Does not start applications after compilation
19
20
20
21
## Examples
21
22
22
- mix run Hello.world
23
- mix run "Some.function with_args"
23
+ mix run -e Hello.world
24
+ mix run -e "Some.function with_args"
25
+ mix run -r some_file.exs
24
26
25
27
Command line options given to the `elixir` executable can be passed as:
26
28
27
- elixir --sname hello -S mix run "My.code"
29
+ elixir --sname hello -S mix run -e "My.code"
28
30
29
31
"""
30
32
def run ( args ) do
31
33
{ opts , head } = OptionParser . parse_head ( args ,
32
- aliases: [ r: :require , pr: :parallel_require ] ,
34
+ aliases: [ r: :require , pr: :parallel_require , e: :eval ] ,
33
35
switches: [ parallel_require: :keep , require: :keep ] )
34
36
35
37
Mix.Task . run "app.start" , args
@@ -40,12 +42,17 @@ defmodule Mix.Tasks.Run do
40
42
value |> filter_patterns |> Kernel.ParallelRequire . files
41
43
:require ->
42
44
value |> filter_patterns |> Enum . each Code . require_file ( & 1 )
45
+ :eval ->
46
+ Code . eval_string ( value )
43
47
_ ->
44
48
:ok
45
49
end
46
50
end
47
51
48
- if head != [ ] , do: Code . eval_string Enum . join ( head , " " )
52
+ if head != [ ] do
53
+ Mix . shell . error "[WARNING] mix run EXPR is deprecated, please use mix run -e EXPR instead"
54
+ Code . eval_string Enum . join ( head , " " )
55
+ end
49
56
if opts [ :no_halt ] , do: :timer . sleep ( :infinity )
50
57
end
51
58
0 commit comments