Class: RubyScope::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_scope/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_scope/cli.rb', line 6

def initialize(args)
  args = args.clone
  @scanner = RubyScope::Scanner.new
  @cache_path = FileUtils.pwd
  
  opts = OptionParser.new do |opts|    
    opts.banner = "Usage: ruby_scope [options] queries path"

    opts.separator ""
    opts.separator "Queries:"

    opts.on("--def NAME", "Find the definition of instance method NAME") do |name|
      @scanner.add_query("s(:defn, #{v name}, _, _)")
    end
    
    opts.on("--class-def NAME", "Find the definition of class method NAME") do |name|
      @scanner.add_query("s(:defs, _, #{v name}, _, _)")
    end
    
    opts.on("--call NAME", "Find method calls of NAME") do |name|
      @scanner.add_query("s(:call, _, #{v name}, _)")
    end
    
    opts.on("--class NAME", "Find definition of NAME") do |name|
      @scanner.add_query("s(:class, #{v name}, _, _)")
    end
    
    opts.on("--variable NAME", "Find references to variable NAME") do |name|
      tag = instance_variable?(name) ? 'ivar' : 'lvar'
      @scanner.add_query("s(:#{tag}, #{v name})")
    end
    
    # Finds block arguments, variable assignments, method arguments (in that order)
    opts.on("--assign NAME", "Find assignments to NAME") do |name|
      tag = instance_variable?(name) ? 'iasgn' : 'lasgn'
      @scanner.add_query("s(:#{tag}, #{v name}) | s(:#{tag}, #{v name}, _) | (t(:args) & SexpPath::Matcher::Block.new{|s| s[1..-1].any?{|a| a == #{v name}}} )")        
    end
    
    opts.on("--any NAME", "Find any reference to NAME (class, variable, number)") do |name|
      @scanner.add_query("include(#{v name})")
    end
    
    opts.on("--custom SEXP_PATH", "Search for a custom SexpPath") do |sexp|
      @scanner.add_query(sexp)
    end
    
    opts.separator ""
    opts.separator "Options:"
    opts.on("-R", "Recursively search folders") do
      @recurse = true
    end
    
    opts.on("--no-cache", "Do not use a cache") do
      @cache_path = nil
    end
    
    opts.on("--cache PATH", "Use the cache at PATH (defaults to current dir)", "Beware, the cache can get rather large") do |path|
      @cache_path = path if path
    end
    
    opts.on("-v", "--verbose", "Verbose output") do
      @scanner.verbose = true
    end
    
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end  
  end
  opts.parse!(args)
  
  @paths = args
  
  if @paths.empty?
    puts opts
    exit 1
  end
  
  @paths = expand_paths(@paths) if @recurse
  @scanner.cache = RubyScope::SexpCache.new(@cache_path) if @cache_path
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



3
4
5
# File 'lib/ruby_scope/cli.rb', line 3

def cache_path
  @cache_path
end

#pathsObject (readonly)

Returns the value of attribute paths.



2
3
4
# File 'lib/ruby_scope/cli.rb', line 2

def paths
  @paths
end

#scannerObject (readonly)

Returns the value of attribute scanner.



4
5
6
# File 'lib/ruby_scope/cli.rb', line 4

def scanner
  @scanner
end

Instance Method Details

#runObject



88
89
90
91
92
# File 'lib/ruby_scope/cli.rb', line 88

def run
  @paths.each do |path|
    @scanner.scan path
  end
end