class Fast::Cli

Command Line Interface for Fast

Attributes

from_code[R]
help[R]
pattern[R]
pry[R]
show_sexp[R]
similar[R]

Public Class Methods

new(args) click to toggle source
# File lib/fast/cli.rb, line 54
def initialize(args)
  args = replace_args_with_shortcut(args) if args.first&.start_with?('.')

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

  option_parser.parse! args

  @files = [*@files].reject { |arg| arg.start_with?('-') }
end
run!(argv) click to toggle source

Run a new command line interface digesting the arguments

# File lib/fast/cli.rb, line 143
def self.run!(argv)
  argv = argv.dup
  new(argv).run!
end

Public Instance Methods

debug(*info) click to toggle source

Output information if debug_mode? is true.

# File lib/fast/cli.rb, line 201
def debug(*info)
  puts(info) if debug_mode?
end
debug_mode?() click to toggle source

@return [Boolean] true when “-d” or “–debug” option is passed

# File lib/fast/cli.rb, line 196
def debug_mode?
  @debug == true
end
exit_shortcut_not_found(name) click to toggle source

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

# File lib/fast/cli.rb, line 234
def exit_shortcut_not_found(name)
  puts "Shortcut \033[1m#{name}\033[0m not found :("
  if Fast.shortcuts.any?
    puts "Available shortcuts are: #{Fast.shortcuts.keys.join(', ')}."
    Fast.load_fast_files!
  end
  exit 1
end
expression() click to toggle source

Create fast expression from node pattern using the command line @return [Array<Fast::Find>] with the expression from string.

# File lib/fast/cli.rb, line 161
def expression
  Fast.expression(@pattern)
end
find_shortcut(name) click to toggle source

Find shortcut by name. Preloads all `Fastfiles` before start. @param name [String] @return [Fast::Shortcut]

# File lib/fast/cli.rb, line 224
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_parser() click to toggle source
# File lib/fast/cli.rb, line 65
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('--link', 'Print link to repository URL instead of code') do
      require 'fast/git'
      @show_link = true
    end

    opts.on('-p', '--parallel', 'Paralelize search') do
      @parallel = 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('--bodyless', 'Print results without the code details') do
      @bodyless = 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('--no-color', 'Disable color output') do
      @colorize = false
    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
parallel?() click to toggle source
# File lib/fast/cli.rb, line 205
def parallel?
  @parallel == true
end
replace_args_with_shortcut(args) click to toggle source
# File lib/fast/cli.rb, line 131
def replace_args_with_shortcut(args)
  shortcut = find_shortcut args.first[1..]

  if shortcut.single_run_with_block?
    shortcut.run
    exit
  else
    args.one? ? shortcut.args : shortcut.merge_args(args[1..])
  end
end
report(file, result) click to toggle source

Report results using the actual options binded from command line. @see Fast.report

# File lib/fast/cli.rb, line 211
def report(file, result)
  Fast.report(result,
              file: file,
              show_link: @show_link,
              show_sexp: @show_sexp,
              headless: @headless,
              bodyless: @bodyless,
              colorize: @colorize)
end
run!() click to toggle source

Show help or search for node patterns

# File lib/fast/cli.rb, line 149
def run!
  raise 'pry and parallel options are incompatible :(' if @parallel && @pry

  if @help || @files.empty? && @pattern.nil?
    puts option_parser.help
  else
    search
  end
end
search_method_name() click to toggle source

@return [Symbol] with `:capture_all` or `:search_all` depending the command line options

# File lib/fast/cli.rb, line 191
def search_method_name
  @captures ? :capture_all : :search_all
end