Class: Querly::CLI::Console

Inherits:
Object
  • Object
show all
Includes:
Querly::Concerns::BacktraceFormatter
Defined in:
lib/querly/cli/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Querly::Concerns::BacktraceFormatter

#format_backtrace

Constructor Details

#initialize(paths:) ⇒ Console

Returns a new instance of Console.



10
11
12
# File 'lib/querly/cli/console.rb', line 10

def initialize(paths:)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/querly/cli/console.rb', line 8

def paths
  @paths
end

Instance Method Details

#analyzerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/querly/cli/console.rb', line 35

def analyzer
  return @analyzer if @analyzer

  @analyzer = Analyzer.new(config: nil, rule: nil)

  ScriptEnumerator.new(paths: paths, config: nil).each do |path, script|
    case script
    when Script
      @analyzer.scripts << script
    when StandardError
      p path: path, script: script.inspect
      p script.backtrace
    end
  end

  @analyzer
end

#loopObject



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/querly/cli/console.rb', line 53

def loop
  while line = Readline.readline("> ", true)
    case line
    when "quit"
      exit
    when "reload!"
      STDOUT.print "reloading..."
      STDOUT.flush
      reload!
      STDOUT.puts " done"
    when /^find (.+)/
      begin
        pattern = Pattern::Parser.parse($1, where: {})

        count = 0

        analyzer.find(pattern) do |script, pair|
          path = script.path.to_s
          line = pair.node.loc.first_line

          while true
            parent = pair.parent

            if parent && parent.node.loc.first_line == line
              pair = pair.parent
            else
              break
            end
          end

          src = Rainbow(pair.node.loc.expression.source.split(/\n/).first).blue
          col = pair.node.loc.column

          puts "  #{path}:#{line}:#{col}\t#{src}"

          count += 1
        end

        puts "#{count} results"
      rescue => exn
        STDOUT.puts Rainbow("Error: #{exn}").red
        STDOUT.puts "Backtrace:"
        STDOUT.puts format_backtrace(exn.backtrace)
      end
    else
      puts_commands
    end
  end
end

#puts_commandsObject



103
104
105
106
107
108
109
110
111
# File 'lib/querly/cli/console.rb', line 103

def puts_commands
  puts <<-Message
Commands:
  - find PATTERN   Find PATTERN from given paths
  - reload!        Reload program from paths
  - quit

  Message
end

#reload!Object



30
31
32
33
# File 'lib/querly/cli/console.rb', line 30

def reload!
  @analyzer = nil
  analyzer
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/querly/cli/console.rb', line 14

def start
  puts <<-Message
Querly #{VERSION}, interactive console

  Message

  puts_commands

  STDOUT.print "Loading..."
  STDOUT.flush
  reload!
  STDOUT.puts " ready!"

  loop
end