Skip to content

Add ability to pass scripts via a command line option #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ def source
end
end

# An item of work that corresponds to the stdin content.
class STDINItem
# An item of work that corresponds to a script content passed via the command line.
class ScriptItem
FILEPATH = :script

def initialize(source)
@source = source
end

def handler
HANDLERS[".rb"]
end

def filepath
:stdin
FILEPATH
end

def source
$stdin.read
@source
end
end

Expand Down Expand Up @@ -191,7 +197,7 @@ def run(item)

source = item.source
formatted = item.handler.format(source, options.print_width)
File.write(filepath, formatted) if filepath != :stdin
File.write(filepath, formatted) if FileItem === item

color = source == formatted ? Color.gray(filepath) : filepath
delta = ((Time.now - start) * 1000).round
Expand All @@ -206,25 +212,25 @@ def run(item)
# The help message displayed if the input arguments are not correctly
# ordered or formatted.
HELP = <<~HELP
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] FILE")}
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Print out the AST corresponding to the given files

#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] FILE")}
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Check that the given files are formatted as syntax tree would format them

#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] FILE")}
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Check that the given files can be formatted idempotently

#{Color.bold("stree doc [--plugins=...] FILE")}
#{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
Print out the doc tree that would be used to format the given files

#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] FILE")}
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Print out the formatted version of the given files

#{Color.bold("stree json [--plugins=...] FILE")}
#{Color.bold("stree json [--plugins=...] [-e SCRIPT] FILE")}
Print out the JSON representation of the given files

#{Color.bold("stree match [--plugins=...] FILE")}
#{Color.bold("stree match [--plugins=...] [-e SCRIPT] FILE")}
Print out a pattern-matching Ruby expression that would match the given files

#{Color.bold("stree help")}
Expand All @@ -236,28 +242,32 @@ def run(item)
#{Color.bold("stree version")}
Output the current version of syntax tree

#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] FILE")}
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
Read, format, and write back the source of the given files

--plugins=...
A comma-separated list of plugins to load.

--print-width=NUMBER
The maximum line width to use when formatting.

-e SCRIPT
Parse an inline Ruby string.
HELP

# This represents all of the options that can be passed to the CLI. It is
# responsible for parsing the list and then returning the file paths at the
# end.
class Options
attr_reader :print_width
attr_reader :print_width, :scripts

def initialize(print_width: DEFAULT_PRINT_WIDTH)
@print_width = print_width
@scripts = []
end

def parse(arguments)
parser.parse(arguments)
parser.parse!(arguments)
end

private
Expand All @@ -283,6 +293,12 @@ def parser
opts.on("--print-width=NUMBER", Integer) do |print_width|
@print_width = print_width
end

# If there is a script specified on the command line, then parse
# it and add it to the list of scripts to run.
opts.on("-e SCRIPT") do |script|
@scripts << script
end
end
end
end
Expand Down Expand Up @@ -361,7 +377,7 @@ def run(argv)

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

# If we're reading from stdin, then we'll just add the stdin object to
# the queue. Otherwise, we'll add each of the filepaths to the queue.
if $stdin.tty? || arguments.any?
if $stdin.tty? && (arguments.any? || options.scripts.any?)
arguments.each do |pattern|
Dir
.glob(pattern)
.each do |filepath|
queue << FileItem.new(filepath) if File.file?(filepath)
end
end
options.scripts.each do |script|
queue << ScriptItem.new(script)
end
else
queue << STDINItem.new
queue << ScriptItem.new($stdin.read)
end

# At the end, we're going to return whether or not this worker ever
Expand Down
10 changes: 10 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def test_no_arguments_no_tty
$stdin = stdin
end

def test_inline_script
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1"]) }
assert_equal("1 + 1\n", stdio)
end

def test_multiple_inline_scripts
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1", "-e", "2+2"]) }
assert_equal("1 + 1\n2 + 2\n", stdio)
end

def test_generic_error
SyntaxTree.stub(:format, ->(*) { raise }) do
result = run_cli("format")
Expand Down