@@ -16,40 +16,56 @@ defmodule Mix.Tasks.Escriptize do
1616
1717 ## Configuration
1818
19- The following option must be specified in your `mix.exs`:
19+ The following option must be specified in your `mix.exs` under `:escript`
20+ key:
2021
21- * `:escript_main_module ` - the module to be invoked once the escript starts.
22+ * `:main_module ` - the module to be invoked once the escript starts.
2223 The module must contain a function named `main/1` that will receive the
2324 command line arguments as binaries;
2425
2526 The remaining options can be specified to further customize the escript:
2627
27- * `:escript_name ` - the name of the generated escript.
28+ * `:name ` - the name of the generated escript.
2829 Defaults to app name;
2930
30- * `:escript_path ` - the path to write the escript to.
31+ * `:path ` - the path to write the escript to.
3132 Defaults to app name;
3233
33- * `:escript_app ` - the app to start with the escript.
34+ * `:app ` - the app to start with the escript.
3435 Defaults to app name. Set it to `nil` if no application should
3536 be started.
3637
37- * `:escript_embed_elixir ` - if `true` embed elixir in the escript file.
38+ * `:embed_elixir ` - if `true` embed elixir in the escript file.
3839 Defaults to `true`.
3940
40- * `:escript_embed_extra_apps ` - embed additional Elixir applications.
41- if `:escript_embed_elixir ` is `true`.
41+ * `:embed_extra_apps ` - embed additional Elixir applications.
42+ if `:embed_elixir ` is `true`.
4243 Defaults to `[]`.
4344
44- * `:escript_shebang ` - shebang interpreter directive used to execute the escript.
45+ * `:shebang ` - shebang interpreter directive used to execute the escript.
4546 Defaults to "#! /usr/bin/env escript\n".
4647
47- * `:escript_comment ` - comment line to follow shebang directive in the escript.
48+ * `:comment ` - comment line to follow shebang directive in the escript.
4849 Defaults to "%%\n"
4950
50- * `:escript_emu_args ` - emulator arguments to embed in the escript file.
51+ * `:emu_args ` - emulator arguments to embed in the escript file.
5152 Defaults to "%%!\n".
5253
54+ ## Example
55+
56+ defmodule MyApp.Mixfile do
57+ def project do
58+ [ app: :myapp,
59+ version: "0.0.1",
60+ escript: escript ]
61+ end
62+
63+ def escript do
64+ [ main_module: MyApp.CLI,
65+ embed_extra_apps: [:mix] ]
66+ end
67+ end
68+
5369 """
5470 def run ( args ) do
5571 { opts , _ , _ } = OptionParser . parse ( args , switches: [ force: :boolean , no_compile: :boolean ] )
@@ -64,29 +80,53 @@ defmodule Mix.Tasks.Escriptize do
6480 escriptize ( Mix.Project . config , opts [ :force ] )
6581 end
6682
83+
84+ @ deprecated_opts [
85+ :escript_main_module , :escript_name , :escript_path , :escript_app ,
86+ :escript_embed_elixir , :escript_embed_extra_apps , :escript_shebang ,
87+ :escript_comment , :escript_mu_args , ]
88+
89+ @ prefix_len String . length ( "escript_" )
90+
91+ defp collect_deprecated_opts ( project ) do
92+ Enum . reduce ( @ deprecated_opts , [ ] , fn name , acc ->
93+ if Keyword . has_key? ( project , name ) do
94+ IO . puts :stderr , "Option #{ inspect name } is deprecated. " <>
95+ "Use the new `:escript` option that takes a keyword list instead."
96+ new_name =
97+ Atom . to_string ( name ) |> String . slice ( @ prefix_len , 100 ) |> String . to_atom ( )
98+ [ { new_name , project [ name ] } | acc ]
99+ else
100+ acc
101+ end
102+ end )
103+ end
104+
67105 defp escriptize ( project , force ) do
68- script_name = project [ :escript_name ] || project [ :app ]
69- filename = project [ :escript_path ] || Atom . to_string ( script_name )
70- main = project [ :escript_main_module ]
71- embed = Keyword . get ( project , :escript_embed_elixir , true )
72- app = Keyword . get ( project , :escript_app , project [ :app ] )
106+ escript_opts = project [ :escript ] || collect_deprecated_opts ( project )
107+
108+ script_name = escript_opts [ :name ] || project [ :app ]
109+ filename = escript_opts [ :path ] || Atom . to_string ( script_name )
110+ main = escript_opts [ :main_module ]
111+ embed = Keyword . get ( escript_opts , :embed_elixir , true )
112+ app = Keyword . get ( escript_opts , :app , project [ :app ] )
73113 files = project_files ( )
74114
75115 cond do
76116 ! script_name ->
77117 Mix . raise "Could not generate escript, no name given, " <>
78- "set :escript_name or :app in the project settings"
118+ "set :name escript option or :app in the project settings"
79119
80120 ! main or ! Code . ensure_loaded? ( main ) ->
81- Mix . raise "Could not generate escript, please set :escript_main_module " <>
82- "in your project configuration to a module that implements main/1"
121+ Mix . raise "Could not generate escript, please set :main_module " <>
122+ "in your project configuration (under `:escript` option) to a module that implements main/1"
83123
84124 force || Mix.Utils . stale? ( files , [ filename ] ) ->
85125 tuples = gen_main ( script_name , main , app ) ++ to_tuples ( files )
86126 tuples = tuples ++ deps_tuples ( )
87127
88128 if embed do
89- extra_apps = project [ :escript_embed_extra_apps ] || [ ]
129+ extra_apps = escript_opts [ :embed_extra_apps ] || [ ]
90130 tuples = Enum . reduce [ :elixir | extra_apps ] , tuples , fn ( app , acc ) ->
91131 app_tuples ( app ) ++ acc
92132 end
@@ -98,9 +138,9 @@ defmodule Mix.Tasks.Escriptize do
98138
99139 case :zip . create 'mem' , tuples , [ :memory ] do
100140 { :ok , { 'mem' , zip } } ->
101- shebang = project [ :escript_shebang ] || "#! /usr/bin/env escript\n "
102- comment = project [ :escript_comment ] || "%%\n "
103- emu_args = project [ :escript_emu_args ] || "%%!\n "
141+ shebang = escript_opts [ :shebang ] || "#! /usr/bin/env escript\n "
142+ comment = escript_opts [ :comment ] || "%%\n "
143+ emu_args = escript_opts [ :emu_args ] || "%%!\n "
104144
105145 script = IO . iodata_to_binary ( [ shebang , comment , emu_args , zip ] )
106146
0 commit comments