Skip to content

Commit 014a2ec

Browse files
author
José Valim
committed
By default, start the application on escript
1 parent 4846d42 commit 014a2ec

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
* bug fix
88
* [IEx] Do not interpret ANSI codes in IEx results
9+
* [Kernel] Don't ignore :nil when dispatching protocols to avoid infinite loops
910
* [Mix] Fix usage of shell expressions in `Mix.Shell.cmd`
10-
* [Kernel] Don't ignore :nil when dispatching protocols to avoid infinite loops.
11+
* [Mix] Start the application by default on escripts
1112

1213
* deprecations
1314

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ defmodule Mix.Tasks.Escriptize do
1818
The following options can be specified in your mix.exs file:
1919
2020
* `escript_name` - the name of the generated escript
21-
Defaults to project name
21+
Defaults to app name
2222
2323
* `escript_path` - the path to write the escript to
24-
Defaults to project name
24+
Defaults to app name
2525
26-
* `escript_main_module` - the module containing the main/1 function.
26+
* `escript_app` - the app to start with the escript
27+
Defaults to app name
28+
29+
* `escript_main_module` - the module containing the main/1 function
2730
Defaults to `Project`
2831
29-
* `escript_embed_elixir` - if true embed elixir in the escript file.
32+
* `escript_embed_elixir` - if true embed elixir in the escript file
3033
Defaults to true
3134
3235
* `escript_embed_extra_apps` - embed additional Elixir applications
@@ -58,6 +61,7 @@ defmodule Mix.Tasks.Escriptize do
5861
filename = project[:escript_path] || atom_to_binary(script_name)
5962
embed = Keyword.get(project, :escript_embed_elixir, true)
6063
beams = all_beams()
64+
app = Keyword.get(project, :escript_app, project[:app])
6165

6266
cond do
6367
!script_name ->
@@ -67,7 +71,7 @@ defmodule Mix.Tasks.Escriptize do
6771
raise Mix.Error, message: "Could not generate escript #{filename}, " <>
6872
"no beam files available"
6973
force or Mix.Utils.stale?(beams, [filename]) ->
70-
files = gen_main(script_name, project[:escript_main_module])
74+
files = gen_main(script_name, project[:escript_main_module], app)
7175
files = files ++ all_files()
7276

7377
if embed do
@@ -149,30 +153,43 @@ defmodule Mix.Tasks.Escriptize do
149153
end
150154
end
151155

152-
defp gen_main(script_name, nil) do
156+
defp gen_main(script_name, nil, app) do
153157
camelized = Mix.Utils.camelize(atom_to_binary(script_name))
154-
gen_main(script_name, Module.concat([camelized]))
158+
gen_main(script_name, Module.concat([camelized]), app)
155159
end
156160

157-
defp gen_main(script_name, script_name) do
161+
defp gen_main(script_name, script_name, _app) do
158162
[]
159163
end
160164

161-
defp gen_main(name, module) do
165+
defp gen_main(name, module, app) do
162166
{ :module, ^name, binary, _ } =
163167
defmodule name do
164168
@module module
169+
@app app
165170

166171
def main(args) do
167172
case :application.start(:elixir) do
168173
:ok ->
174+
start_app
169175
args = Enum.map(args, :unicode.characters_to_binary(&1))
170176
Kernel.CLI.run fn -> @module.main(args) end, true
171177
_ ->
172178
IO.puts :stderr, IO.ANSI.escape("%{red, bright} Elixir is not in the code path, aborting.")
173179
System.halt(1)
174180
end
175181
end
182+
183+
defp start_app do
184+
if app = @app do
185+
case Application.Behaviour.start(app) do
186+
:ok -> :ok
187+
{ :error, reason } ->
188+
IO.puts :stderr, IO.ANSI.escape("%{red, bright} Could not start application #{app}: #{inspect reason}.")
189+
System.halt(1)
190+
end
191+
end
192+
end
176193
end
177194

178195
[{ '#{name}.beam', binary }]

0 commit comments

Comments
 (0)