class Cassie::Tasks::TaskRunner

Attributes

args[R]
command[R]
options[R]
raw_args[R]

Public Class Methods

new(args) click to toggle source
# File lib/cassie/tasks/task_runner.rb, line 9
def initialize(args)
  @args = args
  @command = nil
  @command = args.delete_at(0) if args.first =~ /\A[^-]/
  @options = {}
end

Public Instance Methods

display_info() click to toggle source
# File lib/cassie/tasks/task_runner.rb, line 34
def display_info
  case
  when command && !task
    puts "'#{command}' is not a supported command.\n\n"
    print_documentation
  when options[:show_help]
    print_documentation
  when options[:show_version]
    puts Cassie::VERSION
  else
    print_documentation
  end
end
run() click to toggle source
# File lib/cassie/tasks/task_runner.rb, line 16
def run
  build_options
  Cassie.logger.level = ::Logger::WARN unless options[:debug]
  Cassie.env = options[:environment] if options[:environment]
  Cassie::Tasks::IO.trace! if options[:trace]

  run_command || display_info
rescue OptionParser::InvalidOption => e
  puts("#{e.message}\n\n")
  display_info
end
task() click to toggle source

@returns [Rake::Task, nil] nil if task is not defined, otherwise the task object itself

# File lib/cassie/tasks/task_runner.rb, line 29
def task
  task_name = "cassie:#{command}"
  Rake::Task[task_name] if Rake::Task.task_defined?(task_name)
end

Protected Instance Methods

build_options() click to toggle source
# File lib/cassie/tasks/task_runner.rb, line 80
def build_options
  @options.tap do |h|
    # Options Parsers doesn't work well unles
    # all options are passed up to a single parser.
    # Since we don't want to shadow options and we
    # do want sub-task --help to work
    #
    # As is, a sub task with dependencies
    # may cause issues since the pre-task could
    # have optiosn that cause a parsing error.
    # Need to revisit and probably ditch rake tasks.
    h[:trace] = args.delete("-t") || args.delete("--trace")
    h[:debug] = args.delete("-d") || args.delete("--debug")
    if env_index = (args.index("-e") || args.index("--env"))
      h[:environment] = args.delete_at(env_index + 1)
      args.delete_at(env_index)
    end
    h[:show_version] = args.include?("-v") || args.include?("--version")
    h[:show_help] = args.include?("-h") || args.include?("--help")
  end
end
print_documentation() click to toggle source
run_command() click to toggle source

@returns [Boolean] if task was invoked

# File lib/cassie/tasks/task_runner.rb, line 51
def run_command
  task && task.invoke
end