@@ -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 ] )
@@ -65,28 +81,29 @@ defmodule Mix.Tasks.Escriptize do
65
81
end
66
82
67
83
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 ] )
84
+ escript_opts = project [ :escript ]
85
+ script_name = escript_opts [ :name ] || project [ :app ]
86
+ filename = escript_opts [ :path ] || Atom . to_string ( script_name )
87
+ main = escript_opts [ :main_module ]
88
+ embed = Keyword . get ( escript_opts , :embed_elixir , true )
89
+ app = Keyword . get ( escript_opts , :app , project [ :app ] )
73
90
files = project_files ( )
74
91
75
92
cond do
76
93
! script_name ->
77
94
Mix . raise "Could not generate escript, no name given, " <>
78
- "set :escript_name or :app in the project settings"
95
+ "set :name escript option or :app in the project settings"
79
96
80
97
! 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"
98
+ Mix . raise "Could not generate escript, please set :main_module " <>
99
+ "in your project configuration (under `:escript` option) to a module that implements main/1"
83
100
84
101
force || Mix.Utils . stale? ( files , [ filename ] ) ->
85
102
tuples = gen_main ( script_name , main , app ) ++ to_tuples ( files )
86
103
tuples = tuples ++ deps_tuples ( )
87
104
88
105
if embed do
89
- extra_apps = project [ :escript_embed_extra_apps ] || [ ]
106
+ extra_apps = escript_opts [ :embed_extra_apps ] || [ ]
90
107
tuples = Enum . reduce [ :elixir | extra_apps ] , tuples , fn ( app , acc ) ->
91
108
app_tuples ( app ) ++ acc
92
109
end
@@ -98,9 +115,9 @@ defmodule Mix.Tasks.Escriptize do
98
115
99
116
case :zip . create 'mem' , tuples , [ :memory ] do
100
117
{ :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 "
118
+ shebang = escript_opts [ :shebang ] || "#! /usr/bin/env escript\n "
119
+ comment = escript_opts [ :comment ] || "%%\n "
120
+ emu_args = escript_opts [ :emu_args ] || "%%!\n "
104
121
105
122
script = IO . iodata_to_binary ( [ shebang , comment , emu_args , zip ] )
106
123
0 commit comments