@@ -4,10 +4,21 @@ defmodule Mix.Tasks.Run do
44 @ shortdoc "Run the given file or expression"
55
66 @ moduledoc """
7- Runs the given file or expession in the context of the application.
7+ Runs the given file or expression in the context of the application.
88
9- Before running the code, it invokes the app.start task
10- which defaults to compile and load your project.
9+ Before running the code, it invokes the `app.start` task which compiles
10+ and loads your project.
11+
12+ It is the goal of this task to provide a subset of the functionality
13+ existent in the `elixir` executable, including setting up the `System.argv`:
14+
15+ mix run -e Hello.world
16+ mix run my_script.exs arg1 arg2 arg3
17+
18+ Many command line options need to be passed to the `elixir` executable
19+ directly, which can be done as follows:
20+
21+ elixir --sname hello -S mix run -e "My.code"
1122
1223 ## Command line options
1324
@@ -18,16 +29,6 @@ defmodule Mix.Tasks.Run do
1829 * `--no-compile` - Does not compile even if files require compilation
1930 * `--no-start` - Does not start applications after compilation
2031
21- ## Examples
22-
23- mix run -e Hello.world
24- mix run -e "Some.function with_args"
25- mix run -r some_file.exs
26-
27- Command line options given to the `elixir` executable can be passed as:
28-
29- elixir --sname hello -S mix run -e "My.code"
30-
3132 """
3233 def run ( args ) do
3334 { opts , head } = OptionParser . parse_head ( args ,
@@ -36,6 +37,13 @@ defmodule Mix.Tasks.Run do
3637
3738 Mix.Task . run "app.start" , args
3839
40+ file =
41+ case head do
42+ [ "--" | t ] -> System . argv ( t ) ; nil
43+ [ h | t ] -> System . argv ( t ) ; h
44+ [ ] -> System . argv ( [ ] ) ; nil
45+ end
46+
3947 Enum . each opts , fn ( { key , value } ) ->
4048 case key do
4149 :parallel_require ->
@@ -49,10 +57,7 @@ defmodule Mix.Tasks.Run do
4957 end
5058 end
5159
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
60+ if file , do: Code . require_file ( h )
5661 if opts [ :no_halt ] , do: :timer . sleep ( :infinity )
5762 end
5863
0 commit comments