class Shanty::Cli

Public: Handle the CLI interface between the user and the registered tasks and plugins.

Public Class Methods

new(task_env) click to toggle source
# File lib/shanty/cli.rb, line 11
def initialize(task_env)
  @task_env = task_env
end

Public Instance Methods

run() click to toggle source
# File lib/shanty/cli.rb, line 21
def run
  program :name, 'Shanty'
  program :version, '0.1.0'
  program :description, 'Something'

  setup_tasks
  run!
end
tasks() click to toggle source
# File lib/shanty/cli.rb, line 15
def tasks
  TaskSet.task_sets.reduce({}) do |acc, task_set|
    acc.merge(task_set.tasks)
  end
end

Private Instance Methods

add_action_to_command(name, task, command) click to toggle source
# File lib/shanty/cli.rb, line 53
def add_action_to_command(name, task, command)
  command.action do |args, options|
    task = tasks[name]
    options.default(Hash[defaults_for_options(task)])
    execute_task(name, task, args, options)
  end
end
add_options_to_command(task, command) click to toggle source
# File lib/shanty/cli.rb, line 47
def add_options_to_command(task, command)
  task[:options].each do |option_name, option|
    command.option(syntax_for_option(option_name, option), I18n.t(option[:desc], default: option[:desc]))
  end
end
default_for_type(option) click to toggle source
# File lib/shanty/cli.rb, line 90
def default_for_type(option)
  case option[:type]
  when :boolean
    option[:default] || false
  else
    option[:default]
  end
end
defaults_for_options(task) click to toggle source
# File lib/shanty/cli.rb, line 73
def defaults_for_options(task)
  task[:options].map do |option_name, option|
    [option_name, default_for_type(option)]
  end
end
execute_task(name, task, args, options) click to toggle source
# File lib/shanty/cli.rb, line 61
def execute_task(name, task, args, options)
  # We use allocate here beccause we do not want this to blow up because the class defines a constructor.
  # We cannot and do not support taskset classes needing constructors.
  klass = task[:klass].allocate
  arity = klass.method(name).arity

  args.unshift(@task_env) if arity >= 2
  args.unshift(options) if arity >= 1

  klass.send(name, *args)
end
setup_task(name, task) click to toggle source
# File lib/shanty/cli.rb, line 38
def setup_task(name, task)
  command(name) do |c|
    c.description = I18n.t(task[:desc], default: task[:desc])
    c.syntax = task[:syntax]
    add_options_to_command(task, c)
    add_action_to_command(name, task, c)
  end
end
setup_tasks() click to toggle source
# File lib/shanty/cli.rb, line 32
def setup_tasks
  tasks.each do |name, task|
    setup_task(name, task)
  end
end
syntax_for_option(name, option) click to toggle source
# File lib/shanty/cli.rb, line 79
def syntax_for_option(name, option)
  syntax = case option[:type]
           when :boolean
             "--#{name}"
           else
             "--#{name} #{(option[:type] || 'string').upcase}"
           end

  option[:required] ? "[#{syntax}]" : syntax
end