Module: SyntaxTree::CLI

Defined in:
lib/syntax_tree/cli.rb

Overview

Syntax Tree ships with the stree CLI, which can be used to inspect and manipulate Ruby code. This module is responsible for powering that CLI.

Defined Under Namespace

Classes: AST, Action, Check, Color, Debug, Doc, FileItem, Format, Json, Match, STDINItem, Write

Constant Summary collapse

HELP =

The help message displayed if the input arguments are not correctly ordered or formatted.

<<~HELP
  #{Color.bold("stree ast [OPTIONS] [FILE]")}
    Print out the AST corresponding to the given files

  #{Color.bold("stree check [OPTIONS] [FILE]")}
    Check that the given files are formatted as syntax tree would format them

  #{Color.bold("stree debug [OPTIONS] [FILE]")}
    Check that the given files can be formatted idempotently

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

  #{Color.bold("stree format [OPTIONS] [FILE]")}
    Print out the formatted version of the given files

  #{Color.bold("stree json [OPTIONS] [FILE]")}
    Print out the JSON representation of the given files

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

  #{Color.bold("stree help")}
    Display this help message

  #{Color.bold("stree lsp [OPTIONS]")}
    Run syntax tree in language server mode

  #{Color.bold("stree version")}
    Output the current version of syntax tree

  #{Color.bold("stree write [OPTIONS] [FILE]")}
    Read, format, and write back the source of the given files

  [OPTIONS]

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

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object

Run the CLI over the given array of strings that make up the arguments passed to the invocation.



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/syntax_tree/cli.rb', line 239

def run(argv)
  name, *arguments = argv

  # If there are any plugins specified on the command line, then load them
  # by requiring them here. We do this by transforming something like
  #
  #     stree format --plugins=haml template.haml
  #
  # into
  #
  #     require "syntax_tree/haml"
  #
  if arguments.first&.start_with?("--plugins=")
    plugins = arguments.shift[/^--plugins=(.*)$/, 1]
    plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
  end

  case name
  when "help"
    puts HELP
    return 0
  when "lsp"
    require "syntax_tree/language_server"
    LanguageServer.new.run
    return 0
  when "version"
    puts SyntaxTree::VERSION
    return 0
  end

  action =
    case name
    when "a", "ast"
      AST.new
    when "c", "check"
      Check.new
    when "debug"
      Debug.new
    when "doc"
      Doc.new
    when "j", "json"
      Json.new
    when "m", "match"
      Match.new
    when "f", "format"
      Format.new
    when "w", "write"
      Write.new
    else
      warn(HELP)
      return 1
    end

  # 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?
    warn(HELP)
    return 1
  end

  # We're going to build up a queue of items to process.
  queue = Queue.new

  # 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?
    arguments.each do |pattern|
      Dir
        .glob(pattern)
        .each do |filepath|
          queue << FileItem.new(filepath) if File.file?(filepath)
        end
    end
  else
    queue << STDINItem.new
  end

  # At the end, we're going to return whether or not this worker ever
  # encountered an error.
  errored =
    with_workers(queue) do |item|
      action.run(item)
      false
    rescue Parser::ParseError => error
      warn("Error: #{error.message}")
      highlight_error(error, item.source)
      true
    rescue Check::UnformattedError, Debug::NonIdempotentFormatError
      true
    rescue StandardError => error
      warn(error.message)
      warn(error.backtrace)
      true
    end

  if errored
    action.failure
    1
  else
    action.success
    0
  end
end