Class: Swiftproj::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/swiftproj/commands/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core:, ui:, file:, project_class:, scheme_class:) ⇒ Command

Returns a new instance of Command.



3
4
5
6
7
8
9
# 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

Class Method Details

.command_nameObject



62
63
64
65
66
67
68
69
70
71
72
# 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

.descriptionObject



74
75
76
# File 'lib/swiftproj/commands/command.rb', line 74

def self.description()
  return ""
end

.help_messageObject



82
83
84
85
86
# 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

.optionsObject



78
79
80
# File 'lib/swiftproj/commands/command.rb', line 78

def self.options()
  return {}
end

Instance Method Details

#command_class(command_name) ⇒ Object



36
37
38
39
40
41
42
43
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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