@@ -16,40 +16,56 @@ defmodule Mix.Tasks.Escriptize do
16
16
17
17
## Configuration
18
18
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:
20
21
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.
22
23
The module must contain a function named `main/1` that will receive the
23
24
command line arguments as binaries;
24
25
25
26
The remaining options can be specified to further customize the escript:
26
27
27
- * `:escript_name ` - the name of the generated escript.
28
+ * `:name ` - the name of the generated escript.
28
29
Defaults to app name;
29
30
30
- * `:escript_path ` - the path to write the escript to.
31
+ * `:path ` - the path to write the escript to.
31
32
Defaults to app name;
32
33
33
- * `:escript_app ` - the app to start with the escript.
34
+ * `:app ` - the app to start with the escript.
34
35
Defaults to app name. Set it to `nil` if no application should
35
36
be started.
36
37
37
- * `:escript_embed_elixir ` - if `true` embed elixir in the escript file.
38
+ * `:embed_elixir ` - if `true` embed elixir in the escript file.
38
39
Defaults to `true`.
39
40
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`.
42
43
Defaults to `[]`.
43
44
44
- * `:escript_shebang ` - shebang interpreter directive used to execute the escript.
45
+ * `:shebang ` - shebang interpreter directive used to execute the escript.
45
46
Defaults to "#! /usr/bin/env escript\n".
46
47
47
- * `:escript_comment ` - comment line to follow shebang directive in the escript.
48
+ * `:comment ` - comment line to follow shebang directive in the escript.
48
49
Defaults to "%%\n"
49
50
50
- * `:escript_emu_args ` - emulator arguments to embed in the escript file.
51
+ * `:emu_args ` - emulator arguments to embed in the escript file.
51
52
Defaults to "%%!\n".
52
53
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
+
53
69
"""
54
70
def run ( args ) do
55
71
{ opts , _ , _ } = OptionParser . parse ( args , switches: [ force: :boolean , no_compile: :boolean ] )
@@ -64,29 +80,53 @@ defmodule Mix.Tasks.Escriptize do
64
80
escriptize ( Mix.Project . config , opts [ :force ] )
65
81
end
66
82
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
+
67
105
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 ] )
73
113
files = project_files ( )
74
114
75
115
cond do
76
116
! script_name ->
77
117
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"
79
119
80
120
! 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"
83
123
84
124
force || Mix.Utils . stale? ( files , [ filename ] ) ->
85
125
tuples = gen_main ( script_name , main , app ) ++ to_tuples ( files )
86
126
tuples = tuples ++ deps_tuples ( )
87
127
88
128
if embed do
89
- extra_apps = project [ :escript_embed_extra_apps ] || [ ]
129
+ extra_apps = escript_opts [ :embed_extra_apps ] || [ ]
90
130
tuples = Enum . reduce [ :elixir | extra_apps ] , tuples , fn ( app , acc ) ->
91
131
app_tuples ( app ) ++ acc
92
132
end
@@ -98,9 +138,9 @@ defmodule Mix.Tasks.Escriptize do
98
138
99
139
case :zip . create 'mem' , tuples , [ :memory ] do
100
140
{ :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 "
104
144
105
145
script = IO . iodata_to_binary ( [ shebang , comment , emu_args , zip ] )
106
146
0 commit comments