class Chusaku::CLI

Enables flags for the `chusaku` executable.

Constants

Finished
NotARailsProject
STATUS_ERROR
STATUS_SUCCESS

Attributes

options[R]

Public Class Methods

new() click to toggle source

Initializes a new instance of `Chusaku::CLI`.

@return [Chusaku::CLI] Instance of this class

# File lib/chusaku/cli.rb, line 20
def initialize
  @options = {}
end

Public Instance Methods

call(args = ARGV) click to toggle source

Parse CLI flags, if any, and handle applicable behaviors.

@param args [Array<String>] CLI arguments @return [Integer] 0 on success, 1 on error

# File lib/chusaku/cli.rb, line 28
def call(args = ARGV)
  optparser.parse!(args)
  check_for_rails_project
  Chusaku.call(options)
rescue NotARailsProject
  warn('Please run chusaku from the root of your project.')
  STATUS_ERROR
rescue Finished
  STATUS_SUCCESS
end

Private Instance Methods

add_dry_run_flag(opts) click to toggle source

Adds `–dry-run` flag.

@param opts [OptionParser] OptionParser instance @return [void]

# File lib/chusaku/cli.rb, line 81
def add_dry_run_flag(opts)
  opts.on(
    '--dry-run',
    'Run without file modifications'
  ) do
    @options[:dry] = true
  end
end
add_error_on_annotation_flag(opts) click to toggle source

Adds `–exit-with-error-on-annotation` flag.

@param opts [OptionParser] OptionParser instance @return [void]

# File lib/chusaku/cli.rb, line 68
def add_error_on_annotation_flag(opts)
  opts.on(
    '--exit-with-error-on-annotation',
    'Fail if any file was annotated'
  ) do
    @options[:error_on_annotation] = true
  end
end
add_help_flag(opts) click to toggle source

Adds `–help` flag.

@param opts [OptionParser] OptionParser instance @return [void]

# File lib/chusaku/cli.rb, line 109
def add_help_flag(opts)
  opts.on(
    '-h',
    '--help',
    'Show this help message and quit'
  ) do
    puts(opts)
    raise Finished
  end
end
add_version_flag(opts) click to toggle source

Adds `–version` flag.

@param opts [OptionParser] OptionParser instance @return [void]

# File lib/chusaku/cli.rb, line 94
def add_version_flag(opts)
  opts.on(
    '-v',
    '--version',
    'Show Chusaku version number and quit'
  ) do
    puts(Chusaku::VERSION)
    raise Finished
  end
end
check_for_rails_project() click to toggle source

Raises exception if Rails project cannot be detected.

@raise [Chusaku::CLI::NotARailsProject] Exception if not Rails project @return [void]

# File lib/chusaku/cli.rb, line 45
def check_for_rails_project
  has_controllers = File.directory?('./app/controllers')
  has_rakefile = File.exist?('./Rakefile')
  raise NotARailsProject unless has_controllers && has_rakefile
end
optparser() click to toggle source

Returns an instance of OptionParser with supported flags.

@return [OptionParser] Preconfigured OptionParser instance

# File lib/chusaku/cli.rb, line 54
def optparser
  OptionParser.new do |opts|
    opts.banner = 'Usage: chusaku [options]'
    add_error_on_annotation_flag(opts)
    add_dry_run_flag(opts)
    add_version_flag(opts)
    add_help_flag(opts)
  end
end