class Swiftproj::Command
Public Class Methods
command_name()
click to toggle source
# File lib/swiftproj/commands/command.rb, line 62 def self.command_name() command_name = self.name \ .split("::").last \ .gsub(/Command$/, "") .gsub(/([a-z]+)([A-Z])([a-z]+)/, '\1-\2\3')\ .downcase if not command_name.nil? and command_name.empty? return nil end return command_name end
description()
click to toggle source
# File lib/swiftproj/commands/command.rb, line 74 def self.description() return "" end
help_message()
click to toggle source
# File lib/swiftproj/commands/command.rb, line 82 def self.help_message() return self.options .map { |name, description| " " + name.blue.ljust(30) + description } .join("\n") end
new(core:, ui:, file:, project_class:, scheme_class:)
click to toggle source
# File lib/swiftproj/commands/command.rb, line 3 def initialize(core:, ui:, file:, project_class:, scheme_class:) @core = core @ui = ui @file = file @project_class = project_class @scheme_class = scheme_class end
options()
click to toggle source
# File lib/swiftproj/commands/command.rb, line 78 def self.options() return {} end
Public Instance Methods
command_class(command_name)
click to toggle source
# File lib/swiftproj/commands/command.rb, line 36 def command_class(command_name) name = "#{command_name}-command".split("-").map { |s| s.capitalize }.join begin return Swiftproj.const_get(name) rescue raise Swiftproj::UnknownCommandError end end
get_command(command_name)
click to toggle source
# File lib/swiftproj/commands/command.rb, line 26 def get_command(command_name) return self.command_class(command_name).new( :core => @core, :ui => @ui, :file => @file, :project_class => @project_class, :scheme_class => @scheme_class, ) end
parse_options(argv)
click to toggle source
# File lib/swiftproj/commands/command.rb, line 45 def parse_options(argv) options = Hash.new return options if argv.nil? current_key = nil for arg in argv if arg.start_with? "-" current_key = arg options[current_key] = nil elsif not current_key.nil? options[current_key] = arg current_key = nil end end return options end
run(argv)
click to toggle source
# File lib/swiftproj/commands/command.rb, line 11 def run(argv) begin command_name = argv[0] || 'help' command = self.get_command(command_name) options = self.parse_options(argv[1..-1]) if options.include?("--help") or options.include?("-h") @ui.puts(command.class.help_message) else command.run(options) end rescue Exception => e @ui.puts("[!] #{e.message}".red) end end