Class: Fast::Cli

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

Overview

Command Line Interface for Fast

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



46
47
48
49
50
51
52
53
54
# File 'lib/fast/cli.rb', line 46

def initialize(args)
  args = replace_args_with_shortcut(args) if args.first&.start_with?('.')

  @pattern, *@files = args.reject { |arg| arg.start_with? '-' }

  option_parser.parse! args

  @files = [*@files].reject { |arg| arg.start_with?('-') }
end

Instance Attribute Details

#from_codeObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def from_code
  @from_code
end

#helpObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def help
  @help
end

#patternObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def pattern
  @pattern
end

#pryObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def pry
  @pry
end

#show_sexpObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def show_sexp
  @show_sexp
end

#similarObject (readonly)

rubocop:disable Metrics/ClassLength



45
46
47
# File 'lib/fast/cli.rb', line 45

def similar
  @similar
end

Class Method Details

.run!(argv) ⇒ Object

Run a new command line interface digesting the arguments



116
117
118
119
# File 'lib/fast/cli.rb', line 116

def self.run!(argv)
  argv = argv.dup
  new(argv).run!
end

Instance Method Details

#debug(*info) ⇒ Object

Output information if #debug_mode? is true.



166
167
168
# File 'lib/fast/cli.rb', line 166

def debug(*info)
  puts(info) if debug_mode?
end

#debug_mode?Boolean

Returns true when “-d” or “–debug” option is passed.

Returns:

  • (Boolean)

    true when “-d” or “–debug” option is passed



161
162
163
# File 'lib/fast/cli.rb', line 161

def debug_mode?
  @debug == true
end

#execute_searchObject



152
153
154
155
156
157
158
# File 'lib/fast/cli.rb', line 152

def execute_search
  method_name = @captures ? :capture_all : :search_all
  (Fast.public_send(method_name, expression, @files) || []).each do |file, results|
    results = [results] unless results.is_a?(Array)
    yield file, results
  end
end

#exit_shortcut_not_found(name) ⇒ Object

Exit process with warning message bolding the shortcut that was not found. Prints available shortcuts as extra help and exit with code 1.



190
191
192
193
194
# File 'lib/fast/cli.rb', line 190

def exit_shortcut_not_found(name)
  puts "Shortcut \033[1m#{name}\033[0m not found :("
  puts "Available shortcuts are: #{Fast.shortcuts.keys.join(', ')}." if Fast.shortcuts.any?
  exit 1
end

#expressionObject

Create fast expression from node pattern using the command line



131
132
133
# File 'lib/fast/cli.rb', line 131

def expression
  Fast.expression(@pattern)
end

#find_shortcut(name) ⇒ Fast::Shortcut

Find shortcut by name. Preloads all ‘Fastfiles` before start.

Parameters:

  • name (String)

Returns:



179
180
181
182
183
184
185
186
# File 'lib/fast/cli.rb', line 179

def find_shortcut(name)
  require 'fast/shortcut'
  Fast.load_fast_files!

  shortcut = Fast.shortcuts[name] || Fast.shortcuts[name.to_sym]

  shortcut || exit_shortcut_not_found(name)
end

#option_parserObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



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
102
103
# File 'lib/fast/cli.rb', line 56

def option_parser # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  @option_parser ||= OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
    opts.banner = 'Usage: fast expression <files> [options]'
    opts.on('-d', '--debug', 'Debug fast engine') do
      @debug = true
    end

    opts.on('--ast', 'Print AST instead of code') do
      @show_sexp = true
    end

    opts.on('--captures', 'Print only captures of the patterns and skip node results') do
      @captures = true
    end

    opts.on('--headless', 'Print results without the file name in the header') do
      @headless = true
    end

    opts.on('--pry', 'Jump into a pry session with results') do
      @pry = true
      require 'pry'
    end

    opts.on('-c', '--code', 'Create a pattern from code example') do
      if @pattern
        @from_code = true
        @pattern = Fast.ast(@pattern).to_sexp
        debug 'Expression from AST:', @pattern
      end
    end

    opts.on('-s', '--similar', 'Search for similar code.') do
      @similar = true
      @pattern = Fast.expression_from(Fast.ast(@pattern))
      debug "Looking for code similar to #{@pattern}"
    end

    opts.on_tail('--version', 'Show version') do
      puts Fast::VERSION
      exit
    end

    opts.on_tail('-h', '--help', 'Show help. More at https://jonatas.github.io/fast') do
      @help = true
    end
  end
end

#replace_args_with_shortcut(args) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/fast/cli.rb', line 105

def replace_args_with_shortcut(args)
  shortcut = find_shortcut args.first[1..-1]
  if shortcut.single_run_with_block?
    shortcut.run
    exit
  else
    args.one? ? shortcut.args : shortcut.merge_args(args[1..-1])
  end
end

#report(result, file) ⇒ Object

Report results using the actual options binded from command line.

See Also:



172
173
174
# File 'lib/fast/cli.rb', line 172

def report(result, file)
  Fast.report(result, file: file, show_sexp: @show_sexp, headless: @headless)
end

#run!Object

Show help or search for node patterns



122
123
124
125
126
127
128
# File 'lib/fast/cli.rb', line 122

def run!
  if @help || @files.empty? && @pattern.nil?
    puts option_parser.help
  else
    search
  end
end

#searchObject

Search for each file independent. If -d (debug option) is enabled, it will output details of each search. If capture option is enabled it will only print the captures, otherwise it prints all the results.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fast/cli.rb', line 139

def search
  if debug_mode?
    Fast.debug { execute_search }
  else
    execute_search do |file, results|
      results.each do |result|
        binding.pry if @pry # rubocop:disable Lint/Debugger
        report(result, file)
      end
    end
  end
end