11defmodule Mix.Tasks.Run do
22 use Mix.Task
33
4- @ shortdoc "Run the given expression"
4+ @ shortdoc "Run the given file or expression"
55
66 @ moduledoc """
7- Run the given expression in the application context.
7+ Runs the given file or expession in the context of the application .
88
99 Before running the code, it invokes the app.start task
1010 which defaults to compile and load your project.
1111
1212 ## Command line options
1313
14+ * `--eval`, `-e` - Evaluates the given code
1415 * `--require`, `-r` - Requires pattern before running the command
1516 * `--parallel-require`, `-pr` - Requires pattern in parallel
1617 * `--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
1920
2021 ## Examples
2122
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
2426
2527 Command line options given to the `elixir` executable can be passed as:
2628
27- elixir --sname hello -S mix run "My.code"
29+ elixir --sname hello -S mix run -e "My.code"
2830
2931 """
3032 def run ( args ) do
3133 { opts , head } = OptionParser . parse_head ( args ,
32- aliases: [ r: :require , pr: :parallel_require ] ,
34+ aliases: [ r: :require , pr: :parallel_require , e: :eval ] ,
3335 switches: [ parallel_require: :keep , require: :keep ] )
3436
3537 Mix.Task . run "app.start" , args
@@ -40,12 +42,17 @@ defmodule Mix.Tasks.Run do
4042 value |> filter_patterns |> Kernel.ParallelRequire . files
4143 :require ->
4244 value |> filter_patterns |> Enum . each Code . require_file ( & 1 )
45+ :eval ->
46+ Code . eval_string ( value )
4347 _ ->
4448 :ok
4549 end
4650 end
4751
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
4956 if opts [ :no_halt ] , do: :timer . sleep ( :infinity )
5057 end
5158
0 commit comments