@@ -53,10 +53,13 @@ def source
53
53
end
54
54
end
55
55
56
- # An item of work that corresponds to a script content passed via the command line.
56
+ # An item of work that corresponds to a script content passed via the
57
+ # command line.
57
58
class ScriptItem
58
59
FILEPATH = :script
59
60
61
+ attr_reader :source
62
+
60
63
def initialize ( source )
61
64
@source = source
62
65
end
@@ -68,10 +71,6 @@ def handler
68
71
def filepath
69
72
FILEPATH
70
73
end
71
-
72
- def source
73
- @source
74
- end
75
74
end
76
75
77
76
# The parent action class for the CLI that implements the basics.
@@ -197,7 +196,7 @@ def run(item)
197
196
198
197
source = item . source
199
198
formatted = item . handler . format ( source , options . print_width )
200
- File . write ( filepath , formatted ) if FileItem === item
199
+ File . write ( filepath , formatted ) if item . filepath != :script
201
200
202
201
color = source == formatted ? Color . gray ( filepath ) : filepath
203
202
delta = ( ( Time . now - start ) * 1000 ) . round
@@ -259,13 +258,19 @@ def run(item)
259
258
# responsible for parsing the list and then returning the file paths at the
260
259
# end.
261
260
class Options
262
- attr_reader :print_width , :scripts
261
+ attr_reader :plugins , : print_width, :scripts , :target_ruby_version
263
262
264
263
def initialize ( print_width : DEFAULT_PRINT_WIDTH )
264
+ @plugins = [ ]
265
265
@print_width = print_width
266
266
@scripts = [ ]
267
+ @target_ruby_version = nil
267
268
end
268
269
270
+ # TODO: This function causes a couple of side-effects that I really don't
271
+ # like to have here. It mutates the global state by requiring the plugins,
272
+ # and mutates the global options hash by adding the target ruby version.
273
+ # That should be done on a config-by-config basis, not here.
269
274
def parse ( arguments )
270
275
parser . parse! ( arguments )
271
276
end
@@ -285,7 +290,8 @@ def parser
285
290
# require "syntax_tree/haml"
286
291
#
287
292
opts . on ( "--plugins=PLUGINS" ) do |plugins |
288
- plugins . split ( "," ) . each { |plugin | require "syntax_tree/#{ plugin } " }
293
+ @plugins = plugins . split ( "," )
294
+ @plugins . each { |plugin | require "syntax_tree/#{ plugin } " }
289
295
end
290
296
291
297
# If there is a print width specified on the command line, then
@@ -296,8 +302,13 @@ def parser
296
302
297
303
# If there is a script specified on the command line, then parse
298
304
# it and add it to the list of scripts to run.
299
- opts . on ( "-e SCRIPT" ) do |script |
300
- @scripts << script
305
+ opts . on ( "-e SCRIPT" ) { |script | @scripts << script }
306
+
307
+ # If there is a target ruby version specified on the command line,
308
+ # parse that out and use it when formatting.
309
+ opts . on ( "--target-ruby-version=VERSION" ) do |version |
310
+ @target_ruby_version = Gem ::Version . new ( version )
311
+ Formatter ::OPTIONS [ :target_ruby_version ] = @target_ruby_version
301
312
end
302
313
end
303
314
end
@@ -395,9 +406,7 @@ def run(argv)
395
406
queue << FileItem . new ( filepath ) if File . file? ( filepath )
396
407
end
397
408
end
398
- options . scripts . each do |script |
399
- queue << ScriptItem . new ( script )
400
- end
409
+ options . scripts . each { |script | queue << ScriptItem . new ( script ) }
401
410
else
402
411
queue << ScriptItem . new ( $stdin. read )
403
412
end
0 commit comments