class Ki::KiCommand

Common launcher for all Ki commands

Constants

CommandPrefix
KiExtensions

Shared command registry

Public Class Methods

register(name, clazz) click to toggle source
# File lib/cmd/cmd.rb, line 38
def self.register(name, clazz)
  KiExtensions.register(name, clazz)
end
register_cmd(name, clazz) click to toggle source

Command classes are registered using this method

# File lib/cmd/cmd.rb, line 34
def self.register_cmd(name, clazz)
  register(CommandPrefix + name, clazz)
end

Public Instance Methods

execute(args) click to toggle source

bin/ki command line tool calls this method, which finds the correct class to manage the execution

# File lib/cmd/cmd.rb, line 43
def execute(args)
  @use = []
  @require = []
  @load = []
  my_args = opts.parse(args.dup)
  require_files
  load_scripts
  if my_args.empty?
    KiCommandHelp.new.shell_command("#{0} help").execute(self, [])
  else
    find_cmd(my_args.delete_at(0)).execute(self, my_args)
  end
end
find_cmd(name) click to toggle source
# File lib/cmd/cmd.rb, line 95
def find_cmd(name)
  prefixed_command_names = user_pref.prefixes.map { |p| [p+name, p+"-"+name] }.flatten

  # Finds all matching combinations of prefix+name -> there should be exactly one
  all_commands = {}
  KiExtensions.find("/commands").each { |(command, clazz)| all_commands[command[CommandPrefix.size..-1]]=clazz }
  prefixed_command_names.unshift(name)
  found_command_names = prefixed_command_names.select { |p| all_commands.key?(p) }

  # abort if found_command_names.size != 1
  if found_command_names.size > 1
    raise "Multiple commands match: " + found_command_names.join(", ")
  elsif found_command_names.size == 0
    raise "No commands match: " + prefixed_command_names.join(", ")
  end
  found_command_name = found_command_names.first
  initialize_cmd(all_commands[found_command_name], found_command_name)
end
help() click to toggle source
# File lib/cmd/cmd.rb, line 122
    def help
<<EOF
"ki" is the main command line tool that starts all other Ki processes. Whenever ki command line tools
are executed, ki goes through the following startup process

1. Common command line parameters are parsed. These can used to set execution parameters for this invocation.
2. Extension scripts are loaded from repository. Version configuration is from either -u or user preferences
3. Find command by name
4. Execute the command and pass rest of the command line parameters

Examples

    ki build-version *.txt
    ki -u my/tools compile
    ki -u my/tools:scripts,tools compile

note: By default only files with tag "ki" are used. Use the 'my/tools:scripts,tools' to define additional tags.

Common parameters:

#{opts}
EOF
    end
initialize_cmd(cmd_class, name) click to toggle source
# File lib/cmd/cmd.rb, line 114
def initialize_cmd(cmd_class, name)
  cmd = cmd_class.new
  if cmd.respond_to?(:shell_command=)
    cmd.shell_command="#{$0} #{name}"
  end
  cmd
end
load_scripts() click to toggle source
# File lib/cmd/cmd.rb, line 81
def load_scripts
  # load all script files defined in UserPrefFile uses
  @use.flatten!
  uses = @use.empty? ? user_pref.uses : @use
  uses.each do |use_str|
    ver, tags_str = use_str.split(":")
    tags = tags_str ? tags_str.split(",") : "ki"
    version = ki_home.version(ver)
    version.find_files.tags(tags).file_list.each do |full_path|
      load full_path
    end
  end
end
opts() click to toggle source
# File lib/cmd/cmd.rb, line 57
def opts
  SimpleOptionParser.new do |opts|
    opts.on("-h", "--home HOME-PATH", "Path to Ki root directory") do |v|
      ki_home(KiHome.new(v))
    end
    opts.on("-u", "--use VER", "Use defined scripts") do |v|
      @use << v.split(",")
    end
    opts.on("--require RUBYFILE", "Require Ruby files, comma separated list") do |v|
      @require << v.split(",")
    end
  end
end
require_files() click to toggle source
# File lib/cmd/cmd.rb, line 71
def require_files
  @require.flatten!
  requires = @require.empty? ? user_pref.requires : @require
  requires.each do |req|
    Dir.glob(req).each do |path|
      require path
    end
  end
end