Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d0f13fd

Browse files
committed
New --plugins option on the CLI
1 parent bf0b343 commit d0f13fd

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9-
## [2.0.0] - 2022-03-30
9+
## [2.0.0] - 2022-03-31
1010

1111
### Added
1212

1313
- The new `SyntaxTree.register_handler` hook for plugins.
14+
- The new `--plugins=` option on the CLI.
1415

1516
### Changed
1617

lib/syntax_tree/cli.rb

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,19 @@ def run(handler, filepath, source)
150150
# The help message displayed if the input arguments are not correctly
151151
# ordered or formatted.
152152
HELP = <<~HELP
153-
#{Color.bold("stree ast [FILE]")}
153+
#{Color.bold("stree ast [OPTIONS] [FILE]")}
154154
Print out the AST corresponding to the given files
155155
156-
#{Color.bold("stree check [FILE]")}
156+
#{Color.bold("stree check [OPTIONS] [FILE]")}
157157
Check that the given files are formatted as syntax tree would format them
158158
159-
#{Color.bold("stree debug [FILE]")}
159+
#{Color.bold("stree debug [OPTIONS] [FILE]")}
160160
Check that the given files can be formatted idempotently
161161
162-
#{Color.bold("stree doc [FILE]")}
162+
#{Color.bold("stree doc [OPTIONS] [FILE]")}
163163
Print out the doc tree that would be used to format the given files
164164
165-
#{Color.bold("stree format [FILE]")}
165+
#{Color.bold("stree format [OPTIONS] [FILE]")}
166166
Print out the formatted version of the given files
167167
168168
#{Color.bold("stree help")}
@@ -174,17 +174,22 @@ def run(handler, filepath, source)
174174
#{Color.bold("stree version")}
175175
Output the current version of syntax tree
176176
177-
#{Color.bold("stree write [FILE]")}
177+
#{Color.bold("stree write [OPTIONS] [FILE]")}
178178
Read, format, and write back the source of the given files
179+
180+
[OPTIONS]
181+
182+
--plugins=...
183+
A comma-separated list of plugins to load.
179184
HELP
180185

181186
class << self
182187
# Run the CLI over the given array of strings that make up the arguments
183188
# passed to the invocation.
184189
def run(argv)
185-
arg, *patterns = argv
190+
name, *arguments = argv
186191

187-
case arg
192+
case name
188193
when "help"
189194
puts HELP
190195
return 0
@@ -197,13 +202,13 @@ def run(argv)
197202
return 0
198203
end
199204

200-
if patterns.empty?
205+
if arguments.empty?
201206
warn(HELP)
202207
return 1
203208
end
204209

205210
action =
206-
case arg
211+
case name
207212
when "a", "ast"
208213
AST.new
209214
when "c", "check"
@@ -221,17 +226,31 @@ def run(argv)
221226
return 1
222227
end
223228

229+
# If there are any plugins specified on the command line, then load them
230+
# by requiring them here. We do this by transforming something like
231+
#
232+
# stree format --plugins=haml template.haml
233+
#
234+
# into
235+
#
236+
# require "syntax_tree/haml"
237+
#
238+
if arguments.first.start_with?("--plugins=")
239+
plugins = arguments.shift[/^--plugins=(.*)$/, 1]
240+
plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
241+
end
242+
224243
errored = false
225-
patterns.each do |pattern|
244+
arguments.each do |pattern|
226245
Dir.glob(pattern).each do |filepath|
227246
next unless File.file?(filepath)
228247

229248
handler = HANDLERS[File.extname(filepath)]
230-
source = SyntaxTree.read(filepath)
249+
source = handler.read(filepath)
231250

232251
begin
233252
action.run(handler, filepath, source)
234-
rescue ParseError => error
253+
rescue Parser::ParseError => error
235254
warn("Error: #{error.message}")
236255

237256
if error.lineno

lib/syntax_tree/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SyntaxTree
4-
VERSION = "1.2.0"
4+
VERSION = "2.0.0"
55
end

0 commit comments

Comments
 (0)