class Evoke::CLI

The command-line-interface for Evoke.

Public Class Methods

new() click to toggle source
# File lib/evoke/cli.rb, line 4
def initialize
  @command = ARGV.shift
  @arguments = ARGV.dup

  ARGV.clear
end

Public Instance Methods

start() click to toggle source

Starts the CLI. This will check lib/tasks in the current working directory for evoke tasks and invoke the task supplied on the command line.

# File lib/evoke/cli.rb, line 13
def start
  load_tasks

  return no_tasks if tasks.empty?
  return usage if @command.nil?
  return syntax if @command == 'help'

  task = Evoke.find_task(@command) unless @command.nil?

  return unknown_command if task.nil?

  Evoke.invoke(task, *@arguments)
end

Private Instance Methods

evoke_file() click to toggle source

Gets the path for the local evoke.rb file. This doesn’t check if the file actually exists, it only returns the location where it might be.

@return [String] The path for the evoke file.

# File lib/evoke/cli.rb, line 58
def evoke_file
  @evoke_file = File.join(Dir.pwd, 'evoke.rb')
end
load_tasks() click to toggle source

Loads the Evoke tasks. This will first search for a file named ‘evoke.rb` in the current working directory. If one is found it will be loaded. If none is found the default working directory’s lib/tasks folder will be used to load the tasks.

# File lib/evoke/cli.rb, line 66
def load_tasks
  return load(evoke_file) if File.file?(evoke_file)

  # Load the tasks from the current working directory.
  Evoke.load_tasks('lib/tasks')
end
no_tasks() click to toggle source

Tells the user there are no tasks to invoke and exits with status 1.

# File lib/evoke/cli.rb, line 74
def no_tasks
  $stderr.puts 'No tasks found in the current working directory.'
  exit(1)
end
syntax() click to toggle source

Prints the syntax usage of the task requested by help.

# File lib/evoke/cli.rb, line 30
def syntax
  return usage if @arguments.empty?

  @command = @arguments.shift

  task = Evoke.find_task(@command)

  return unknown_command if task.nil?

  task.print_syntax

  exit(2)
end
task_names() click to toggle source

Gets the underscored names of all the tasks and caches it. These are the names are that used on the command line.

@return [Array] The name of all the tasks.

# File lib/evoke/cli.rb, line 96
def task_names
  @task_names ||= tasks.map { |task| task.name.underscore }
end
tasks() click to toggle source

Finds and caches all the Evoke tasks.

@return [Array] The tasks.

# File lib/evoke/cli.rb, line 88
def tasks
  @tasks ||= Evoke.tasks
end
unknown_command() click to toggle source

Tells the user that the supplied task could not be found.

# File lib/evoke/cli.rb, line 80
def unknown_command
  $stderr.puts "No task named #{@command.inspect}"
  exit(1)
end
usage() click to toggle source

Prints the usage for all the discovered tasks.

# File lib/evoke/cli.rb, line 45
def usage
  grouped_tasks = task_names.group_by(&:size)
  name_sizes = grouped_tasks.keys
  biggest_name = name_sizes.max || 0
  tasks.each { |task| task.print_usage(biggest_name + 2) }

  exit(2)
end