class Unparser::CLI

Unparser CLI implementation

Constants

EXIT_FAILURE
EXIT_SUCCESS

Public Class Methods

new(arguments) click to toggle source

Initialize object

@param [Array<String>] arguments

@return [undefined]

@api private

# File lib/unparser/cli.rb, line 74
def initialize(arguments)
  @ignore  = Set.new
  @targets = []

  @fail_fast  = false
  @success    = true
  @validation = :validation
  @verbose    = false

  opts = OptionParser.new do |builder|
    add_options(builder)
  end

  opts.parse!(arguments).each do |name|
    @targets.concat(targets(name))
  end
end
run(*arguments) click to toggle source

Run CLI

@param [Array<String>] arguments

@return [Integer]

the exit status

@api private

# File lib/unparser/cli.rb, line 63
def self.run(*arguments)
  new(*arguments).exit_status
end

Public Instance Methods

add_options(builder) click to toggle source

Add options

@param [OptionParser] builder

@return [undefined]

@api private

rubocop:disable Metrics/MethodLength

# File lib/unparser/cli.rb, line 101
def add_options(builder)
  builder.banner = 'usage: unparse [options] FILE [FILE]'
  builder.separator('')
  builder.on('-e', '--evaluate SOURCE') do |source|
    @targets << Target::String.new(source)
  end
  builder.on('--start-with FILE') do |path|
    @start_with = targets(path).first
  end
  builder.on('-v', '--verbose') do
    @verbose = true
  end
  builder.on('-l', '--literal') do
    @validation = :literal_validation
  end
  builder.on('--ignore FILE') do |file|
    @ignore.merge(targets(file))
  end
  builder.on('--fail-fast') do
    @fail_fast = true
  end
end
exit_status() click to toggle source

Return exit status

@return [Integer]

@api private

# File lib/unparser/cli.rb, line 131
def exit_status
  effective_targets.each do |target|
    process_target(target)
    break if @fail_fast && !@success
  end

  @success ? EXIT_SUCCESS : EXIT_FAILURE
end

Private Instance Methods

effective_targets() click to toggle source
# File lib/unparser/cli.rb, line 154
def effective_targets
  if @start_with
    reject = true
    @targets.reject do |targets|
      if reject && targets.eql?(@start_with)
        reject = false
      end

      reject
    end
  else
    @targets
  end.reject(&@ignore.method(:include?))
end
process_target(target) click to toggle source
# File lib/unparser/cli.rb, line 142
def process_target(target)
  validation = target.public_send(@validation)
  if validation.success?
    puts validation.report if @verbose
    puts "Success: #{validation.identification}"
  else
    puts validation.report
    puts "Error: #{validation.identification}"
    @success = false
  end
end
targets(file_name) click to toggle source
# File lib/unparser/cli.rb, line 169
def targets(file_name)
  if File.directory?(file_name)
    Dir.glob(File.join(file_name, '**/*.rb')).sort
  elsif File.file?(file_name)
    [file_name]
  else
    Dir.glob(file_name).sort
  end.map { |file| Target::Path.new(Pathname.new(file)) }
end