Skip to content

Commit 1099498

Browse files
authored
Merge pull request #152 from paracycle/uk-add-script-flag
Add ability to pass scripts via a command line option
2 parents 8ff9ab0 + 1be1965 commit 1099498

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

lib/syntax_tree/cli.rb

+37-18
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,24 @@ def source
5353
end
5454
end
5555

56-
# An item of work that corresponds to the stdin content.
57-
class STDINItem
56+
# An item of work that corresponds to a script content passed via the command line.
57+
class ScriptItem
58+
FILEPATH = :script
59+
60+
def initialize(source)
61+
@source = source
62+
end
63+
5864
def handler
5965
HANDLERS[".rb"]
6066
end
6167

6268
def filepath
63-
:stdin
69+
FILEPATH
6470
end
6571

6672
def source
67-
$stdin.read
73+
@source
6874
end
6975
end
7076

@@ -191,7 +197,7 @@ def run(item)
191197

192198
source = item.source
193199
formatted = item.handler.format(source, options.print_width)
194-
File.write(filepath, formatted) if filepath != :stdin
200+
File.write(filepath, formatted) if FileItem === item
195201

196202
color = source == formatted ? Color.gray(filepath) : filepath
197203
delta = ((Time.now - start) * 1000).round
@@ -206,25 +212,25 @@ def run(item)
206212
# The help message displayed if the input arguments are not correctly
207213
# ordered or formatted.
208214
HELP = <<~HELP
209-
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] FILE")}
215+
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
210216
Print out the AST corresponding to the given files
211217
212-
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] FILE")}
218+
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
213219
Check that the given files are formatted as syntax tree would format them
214220
215-
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] FILE")}
221+
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
216222
Check that the given files can be formatted idempotently
217223
218-
#{Color.bold("stree doc [--plugins=...] FILE")}
224+
#{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
219225
Print out the doc tree that would be used to format the given files
220226
221-
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] FILE")}
227+
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
222228
Print out the formatted version of the given files
223229
224-
#{Color.bold("stree json [--plugins=...] FILE")}
230+
#{Color.bold("stree json [--plugins=...] [-e SCRIPT] FILE")}
225231
Print out the JSON representation of the given files
226232
227-
#{Color.bold("stree match [--plugins=...] FILE")}
233+
#{Color.bold("stree match [--plugins=...] [-e SCRIPT] FILE")}
228234
Print out a pattern-matching Ruby expression that would match the given files
229235
230236
#{Color.bold("stree help")}
@@ -236,28 +242,32 @@ def run(item)
236242
#{Color.bold("stree version")}
237243
Output the current version of syntax tree
238244
239-
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] FILE")}
245+
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
240246
Read, format, and write back the source of the given files
241247
242248
--plugins=...
243249
A comma-separated list of plugins to load.
244250
245251
--print-width=NUMBER
246252
The maximum line width to use when formatting.
253+
254+
-e SCRIPT
255+
Parse an inline Ruby string.
247256
HELP
248257

249258
# This represents all of the options that can be passed to the CLI. It is
250259
# responsible for parsing the list and then returning the file paths at the
251260
# end.
252261
class Options
253-
attr_reader :print_width
262+
attr_reader :print_width, :scripts
254263

255264
def initialize(print_width: DEFAULT_PRINT_WIDTH)
256265
@print_width = print_width
266+
@scripts = []
257267
end
258268

259269
def parse(arguments)
260-
parser.parse(arguments)
270+
parser.parse!(arguments)
261271
end
262272

263273
private
@@ -283,6 +293,12 @@ def parser
283293
opts.on("--print-width=NUMBER", Integer) do |print_width|
284294
@print_width = print_width
285295
end
296+
297+
# If there is a script specified on the command line, then parse
298+
# it and add it to the list of scripts to run.
299+
opts.on("-e SCRIPT") do |script|
300+
@scripts << script
301+
end
286302
end
287303
end
288304
end
@@ -361,7 +377,7 @@ def run(argv)
361377

362378
# If we're not reading from stdin and the user didn't supply and
363379
# filepaths to be read, then we exit with the usage message.
364-
if $stdin.tty? && arguments.empty?
380+
if $stdin.tty? && arguments.empty? && options.scripts.empty?
365381
warn(HELP)
366382
return 1
367383
end
@@ -371,16 +387,19 @@ def run(argv)
371387

372388
# If we're reading from stdin, then we'll just add the stdin object to
373389
# the queue. Otherwise, we'll add each of the filepaths to the queue.
374-
if $stdin.tty? || arguments.any?
390+
if $stdin.tty? && (arguments.any? || options.scripts.any?)
375391
arguments.each do |pattern|
376392
Dir
377393
.glob(pattern)
378394
.each do |filepath|
379395
queue << FileItem.new(filepath) if File.file?(filepath)
380396
end
381397
end
398+
options.scripts.each do |script|
399+
queue << ScriptItem.new(script)
400+
end
382401
else
383-
queue << STDINItem.new
402+
queue << ScriptItem.new($stdin.read)
384403
end
385404

386405
# At the end, we're going to return whether or not this worker ever

test/cli_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def test_no_arguments_no_tty
133133
$stdin = stdin
134134
end
135135

136+
def test_inline_script
137+
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1"]) }
138+
assert_equal("1 + 1\n", stdio)
139+
end
140+
141+
def test_multiple_inline_scripts
142+
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1", "-e", "2+2"]) }
143+
assert_equal("1 + 1\n2 + 2\n", stdio)
144+
end
145+
136146
def test_generic_error
137147
SyntaxTree.stub(:format, ->(*) { raise }) do
138148
result = run_cli("format")

0 commit comments

Comments
 (0)