Class: Rbr::CLI
- Inherits:
-
Object
- Object
- Rbr::CLI
- Defined in:
- lib/rbr/cli.rb
Instance Method Summary collapse
- #check_arg_count ⇒ Object
- #expand_path(path) ⇒ Object
- #filenames ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #matching_nodes ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
9 10 11 12 13 14 |
# File 'lib/rbr/cli.rb', line 9 def initialize check_arg_count @matcher = ARGV[0].to_sym @condition = ARGV[1] end |
Instance Method Details
#check_arg_count ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rbr/cli.rb', line 52 def check_arg_count return true if ARGV.count >= 2 warn <<~USAGE Usage: rbr MATCHER PATTERN FILE Examples: # find assignments to lvalue :@author in the file test/book.rb rbr assignment @author test/book.rb # find strings matching /Author.*name/ in the file test/book.rb rbr str "Author.*name" test/book.rb USAGE false end |
#expand_path(path) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/rbr/cli.rb', line 45 def (path) return [path] if File.file?(path) # if path is not a file, then glob all .rb files beneath it Dir.glob("#{path}/**/*.rb") end |
#filenames ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rbr/cli.rb', line 33 def filenames pattern = if ARGV.count > 2 ARGV[2..] else ["."] end pattern.map { |arg| (arg) } .flatten .select { |filename| File.file?(filename) } end |
#matching_nodes ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rbr/cli.rb', line 22 def matching_nodes filenames.to_h do |filename| root, comments = Parser::CurrentRuby.parse_file_with_comments(filename) [filename, Query.new(@matcher, @condition).run(root, comments)] rescue EncodingError, Parser::SyntaxError => e warn "# Error parsing #{filename}: #{e}" [filename, []] end end |
#start ⇒ Object
16 17 18 19 20 |
# File 'lib/rbr/cli.rb', line 16 def start matching_nodes.each do |filename, nodes| nodes.each { |node| puts "#{filename}:#{node.pretty_print}" } end end |