Class: Ki::KiCommand

Inherits:
Object show all
Defined in:
lib/cmd/cmd.rb

Overview

Common launcher for all Ki commands

  • all command classes can register themselves using the register_cmd method

Constant Summary collapse

KiExtensions =

Shared command registry

ServiceRegistry.new
CommandPrefix =
"/commands/"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register(name, clazz) ⇒ Object



38
39
40
# File 'lib/cmd/cmd.rb', line 38

def self.register(name, clazz)
  KiExtensions.register(name, clazz)
end

.register_cmd(name, clazz) ⇒ Object

Command classes are registered using this method



34
35
36
# File 'lib/cmd/cmd.rb', line 34

def self.register_cmd(name, clazz)
  register(CommandPrefix + name, clazz)
end

Instance Method Details

#execute(args) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

#helpObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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) ⇒ Object



114
115
116
117
118
119
120
# 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_scriptsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#optsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_filesObject



71
72
73
74
75
76
77
78
79
# 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