@@ -3,8 +3,8 @@ defmodule Kernel.CLI do
3
3
Module responsible for controlling Elixir's CLI
4
4
"""
5
5
6
- defrecord Config , commands: [ ] , output: "." ,
7
- compile: [ ] , halt: true , compiler_options : [ ]
6
+ defrecord Config , commands: [ ] , output: "." , compile: [ ] ,
7
+ halt: true , compiler_options: [ ] , errors : [ ]
8
8
9
9
# This is the API invoked by Elixir boot process.
10
10
@ doc false
@@ -15,7 +15,13 @@ defmodule Kernel.CLI do
15
15
:elixir_code_server . cast ( { :argv , argv } )
16
16
17
17
run fn ->
18
- Enum . map Enum . reverse ( config . commands ) , process_command ( & 1 , config )
18
+ command_results = Enum . map ( Enum . reverse ( config . commands ) , process_command ( & 1 , config ) )
19
+ command_errors = Enum . filter_map ( command_results , & 1 != :ok , fn { :error , msg } -> msg end )
20
+ errors = Enum . reverse ( config . errors ) ++ command_errors
21
+ if errors != [ ] do
22
+ Enum . each ( errors , fn msg -> IO . puts ( :stderr , msg ) end )
23
+ System . halt ( 1 )
24
+ end
19
25
end , config . halt
20
26
end
21
27
@@ -83,15 +89,11 @@ defmodule Kernel.CLI do
83
89
unless hooks == [ ] , do: at_exit ( status )
84
90
end
85
91
86
- defp invalid_option ( option ) do
87
- IO . puts ( :stderr , "Unknown option #{ option } " )
88
- System . halt ( 1 )
89
- end
90
-
91
92
defp shared_option? ( list , config , callback ) do
92
93
case process_shared ( list , config ) do
93
- { [ h | _ ] , _ } when h == hd ( list ) ->
94
- invalid_option h
94
+ { [ h | hs ] , _ } when h == hd ( list ) ->
95
+ new_config = config . update_errors [ "Unknown option #{ h } " | & 1 ]
96
+ callback . ( hs , new_config )
95
97
{ new_list , new_config } ->
96
98
callback . ( new_list , new_config )
97
99
end
@@ -129,8 +131,7 @@ defmodule Kernel.CLI do
129
131
defp process_shared ( [ "-r" , h | t ] , config ) do
130
132
files = Path . wildcard ( h )
131
133
if files == [ ] do
132
- IO . puts ( :stderr , "-r : No files matched pattern #{ h } " )
133
- System . halt ( 1 )
134
+ process_shared t , config . update_errors [ "-r : No files matched pattern #{ h } " | & 1 ]
134
135
else
135
136
process_shared t , Enum . reduce ( files , config , fn path , config ->
136
137
config . update_commands [ { :require , path } | & 1 ]
@@ -165,8 +166,7 @@ defmodule Kernel.CLI do
165
166
if exec do
166
167
{ config . update_commands ( [ { :require , exec } | & 1 ] ) , t }
167
168
else
168
- IO . puts ( :stderr , "Could not find executable #{ h } " )
169
- System . halt ( 1 )
169
+ { config . update_errors ( [ "-S : Could not find executable #{ h } " | & 1 ] ) , t }
170
170
end
171
171
end
172
172
@@ -223,32 +223,37 @@ defmodule Kernel.CLI do
223
223
224
224
defp process_command ( { :eval , expr } , _config ) when is_binary ( expr ) do
225
225
Code . eval ( expr , [ ] )
226
+ :ok
226
227
end
227
228
228
229
defp process_command ( { :app , app } , _config ) when is_binary ( app ) do
229
230
case Application.Behaviour . start ( binary_to_atom ( app ) ) do
230
231
{ :error , reason } ->
231
- IO . puts ( :stderr , "Could not start application #{ app } : #{ inspect reason } " )
232
- System . halt ( 1 )
232
+ { :error , "--app : Could not start application #{ app } : #{ inspect reason } " }
233
233
:ok ->
234
234
:ok
235
235
end
236
236
end
237
237
238
238
defp process_command ( { :require , file } , _config ) when is_binary ( file ) do
239
- Code . require_file ( file )
239
+ if File . regular? ( file ) do
240
+ Code . require_file ( file )
241
+ :ok
242
+ else
243
+ { :error , "-r : No file named #{ file } " }
244
+ end
240
245
end
241
246
242
247
defp process_command ( { :parallel_require , pattern } , _config ) when is_binary ( pattern ) do
243
248
files = Path . wildcard ( pattern )
244
249
files = Enum . uniq ( files )
245
250
files = Enum . filter files , File . regular? ( & 1 )
246
251
247
- if files == [ ] do
248
- IO . puts ( :stderr , "-pr : No files matched pattern #{ pattern } " )
249
- System . halt ( 1 )
250
- else
252
+ if files != [ ] do
251
253
Kernel.ParallelRequire . files ( files )
254
+ :ok
255
+ else
256
+ { :error , "-pr : No files matched pattern #{ pattern } " }
252
257
end
253
258
end
254
259
@@ -259,8 +264,13 @@ defmodule Kernel.CLI do
259
264
files = Enum . uniq ( List . concat ( files ) )
260
265
files = Enum . filter files , File . regular? ( & 1 )
261
266
262
- Code . compiler_options ( config . compiler_options )
263
- Kernel.ParallelCompiler . files_to_path ( files , config . output ,
264
- fn file -> IO . puts "Compiled #{ file } " end )
267
+ if files != [ ] do
268
+ Code . compiler_options ( config . compiler_options )
269
+ Kernel.ParallelCompiler . files_to_path ( files , config . output ,
270
+ fn file -> IO . puts "Compiled #{ file } " end )
271
+ :ok
272
+ else
273
+ { :error , "--compile : No files matched patterns #{ patterns } " }
274
+ end
265
275
end
266
276
end
0 commit comments