@@ -212,63 +212,8 @@ defmodule Mix.Tasks.Escript.Build do
212
212
@ config unquote ( config )
213
213
@ app unquote ( app )
214
214
215
- # We need to use Erlang modules at this point
216
- # because we are not sure Elixir is available.
217
- unquote ( module_body_for ( language ) )
218
- end
219
-
220
- { :module , ^ name , binary , _ } = Module . create ( name , module_body , Macro.Env . location ( __ENV__ ) )
221
- [ { '#{ name } .beam' , binary } ]
222
- end
223
-
224
- defp module_body_for ( :elixir ) do
225
- quote do
226
- def main ( args ) do
227
- case :application . ensure_all_started ( :elixir ) do
228
- { :ok , _ } ->
229
- load_config ( @ config )
230
- start_app ( @ app )
231
- args = Enum . map ( args , & List . to_string ( & 1 ) )
232
- Kernel.CLI . run fn _ -> @ module . main ( args ) end , true
233
- _ ->
234
- :io . put_chars :standard_error , "Elixir is not available, aborting.\n "
235
- :erlang . halt ( 1 )
236
- end
237
- end
238
-
239
- defp load_config ( config ) do
240
- for { app , kw } <- config , { k , v } <- kw do
241
- :application . set_env ( app , k , v , persistent: true )
242
- end
243
- :ok
244
- end
245
-
246
- defp start_app ( nil ) do
247
- :ok
248
- end
249
-
250
- defp start_app ( app ) do
251
- case :application . ensure_all_started ( app ) do
252
- { :ok , _ } -> :ok
253
- { :error , { app , reason } } ->
254
- io_error "Could not start application #{ app } : " <>
255
- Application . format_error ( reason )
256
- System . halt ( 1 )
257
- end
258
- end
259
-
260
- defp io_error ( message ) do
261
- IO . puts :stderr , IO.ANSI . format ( [ :red , :bright , message ] )
262
- end
263
- end
264
- end
265
-
266
- defp module_body_for ( :erlang ) do
267
- quote do
268
215
def main ( args ) do
269
- load_config ( @ config )
270
- start_app ( @ app )
271
- @ module . main ( args )
216
+ unquote ( main_body_for ( language ) )
272
217
end
273
218
274
219
defp load_config ( config ) do
@@ -288,8 +233,13 @@ defmodule Mix.Tasks.Escript.Build do
288
233
case :application . ensure_all_started ( app ) do
289
234
{ :ok , _ } -> :ok
290
235
{ :error , { app , reason } } ->
291
- io_error [ "Could not start application #{ app } : " ,
292
- :io_lib . format ( '~p~n' , [ reason ] ) ]
236
+ formatted_error = case :code . ensure_loaded ( Application ) do
237
+ { :module , Application } -> Application . format_error ( reason )
238
+ { :error , _ } -> :io_lib . format ( '~p' , [ reason ] )
239
+ end
240
+ io_error [ "Could not start application " ,
241
+ :erlang . atom_to_binary ( app , :utf8 ) ,
242
+ ": " , formatted_error , ?\n ]
293
243
:erlang . halt ( 1 )
294
244
end
295
245
end
@@ -298,5 +248,31 @@ defmodule Mix.Tasks.Escript.Build do
298
248
:io . put_chars ( :standard_error , message )
299
249
end
300
250
end
251
+
252
+ { :module , ^ name , binary , _ } = Module . create ( name , module_body , Macro.Env . location ( __ENV__ ) )
253
+ [ { '#{ name } .beam' , binary } ]
254
+ end
255
+
256
+ defp main_body_for ( :elixir ) do
257
+ quote do
258
+ case :application . ensure_all_started ( :elixir ) do
259
+ { :ok , _ } ->
260
+ load_config ( @ config )
261
+ start_app ( @ app )
262
+ args = Enum . map ( args , & List . to_string ( & 1 ) )
263
+ Kernel.CLI . run fn _ -> @ module . main ( args ) end , true
264
+ _ ->
265
+ :io . put_chars :standard_error , "Elixir is not available, aborting.\n "
266
+ :erlang . halt ( 1 )
267
+ end
268
+ end
269
+ end
270
+
271
+ defp main_body_for ( :erlang ) do
272
+ quote do
273
+ load_config ( @ config )
274
+ start_app ( @ app )
275
+ @ module . main ( args )
276
+ end
301
277
end
302
278
end
0 commit comments