Skip to content

Commit 1857267

Browse files
committed
Put escript options under :escript in the project config
1 parent 0c8c6f0 commit 1857267

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

lib/mix/lib/mix/tasks/escriptize.ex

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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])
@@ -65,28 +81,29 @@ defmodule Mix.Tasks.Escriptize do
6581
end
6682

6783
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])
7390
files = project_files()
7491

7592
cond do
7693
!script_name ->
7794
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"
7996

8097
!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"
83100

84101
force || Mix.Utils.stale?(files, [filename]) ->
85102
tuples = gen_main(script_name, main, app) ++ to_tuples(files)
86103
tuples = tuples ++ deps_tuples()
87104

88105
if embed do
89-
extra_apps = project[:escript_embed_extra_apps] || []
106+
extra_apps = escript_opts[:embed_extra_apps] || []
90107
tuples = Enum.reduce [:elixir|extra_apps], tuples, fn(app, acc) ->
91108
app_tuples(app) ++ acc
92109
end
@@ -98,9 +115,9 @@ defmodule Mix.Tasks.Escriptize do
98115

99116
case :zip.create 'mem', tuples, [:memory] do
100117
{: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"
104121

105122
script = IO.iodata_to_binary([shebang, comment, emu_args, zip])
106123

lib/mix/test/mix/tasks/escriptize_test.exs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@ defmodule Mix.Tasks.EscriptizeTest do
77
def project do
88
[ app: :escripttest,
99
version: "0.0.1",
10-
escript_main_module: Escripttest,
11-
escript_embed_elixir: true ]
10+
escript: [
11+
main_module: Escripttest,
12+
embed_elixir: true
13+
]
14+
]
1215
end
1316
end
1417

1518
defmodule EscriptWithPath do
1619
def project do
1720
[ app: :escripttestwithpath,
1821
version: "0.0.1",
19-
escript_app: nil,
20-
escript_embed_elixir: true,
21-
escript_main_module: Escripttest,
22-
escript_name: :escripttestwithpath,
23-
escript_path: Path.join("ebin", "escripttestwithpath") ]
22+
escript: [
23+
app: nil,
24+
embed_elixir: true,
25+
main_module: Escripttest,
26+
name: :escripttestwithpath,
27+
path: Path.join("ebin", "escripttestwithpath")
28+
]
29+
]
2430
end
2531
end
2632

2733
defmodule EscriptWithDeps do
2834
def project do
2935
[ app: :escripttestwithdeps,
3036
version: "0.0.1",
31-
escript_main_module: Escripttest,
32-
escript_path: Path.join("ebin", "escripttestwithdeps"),
37+
escript: [
38+
main_module: Escripttest,
39+
path: Path.join("ebin", "escripttestwithdeps"),
40+
],
3341
deps: [{:ok, path: fixture_path("deps_status/deps/ok")}] ]
3442
end
3543
end

0 commit comments

Comments
 (0)