Module: SyntaxTree::CLI

Defined in:
lib/syntax_tree/cli.rb

Defined Under Namespace

Classes: AST, Action, Check, Color, Debug, Doc, Format, Write

Constant Summary collapse

HANDLERS =

This holds references to objects that respond to both #parse and #format so that we can use them in the CLI.

{}
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 help")}
    Display this help message

  #{Color.bold("stree lsp")}
    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

.register_handler(extension, handler) ⇒ Object

This is a hook provided so that plugins can register themselves as the handler for a particular file type.



12
13
14
# File 'lib/syntax_tree/cli.rb', line 12

def self.register_handler(extension, handler)
  HANDLERS[extension] = handler
end

.run(argv) ⇒ Object

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
# File 'lib/syntax_tree/cli.rb', line 189

def run(argv)
  name, *arguments = argv

  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

  if arguments.empty?
    warn(HELP)
    return 1
  end

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

  # 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

  errored = false
  arguments.each do |pattern|
    Dir.glob(pattern).each do |filepath|
      next unless File.file?(filepath)

      handler = HANDLERS[File.extname(filepath)]
      source = handler.read(filepath)

      begin
        action.run(handler, filepath, source)
      rescue Parser::ParseError => error
        warn("Error: #{error.message}")

        if error.lineno
          highlight_error(error, source)
        else
          warn(error.message)
          warn(error.backtrace)
        end

        errored = true
      rescue Check::UnformattedError, Debug::NonIdempotentFormatError
        errored = true
      rescue => error
        warn(error.message)
        warn(error.backtrace)
        errored = true
      end
    end
  end

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