Class: GitPusshuTen::Command

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

Constant Summary collapse

BLACKLISTED =

Contains an array of blacklisted commands These are files that aren’t actually CLI commands but just classes that are used by Git プ ッ シ ュ 天

%w[base]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, configuration, hooks, environment) ⇒ Command

Initializes the specified command if it exists or errors out when it does not exist in the commands/*.rb



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitpusshuten/command.rb', line 31

def initialize(cli, configuration, hooks, environment)
  @cli           = cli
  @configuration = configuration
  @hooks         = hooks
  @environment   = environment
  
  if cli.command.nil?
    display_commands
    exit
  end
  
  unless available_commands.include?(cli.command)
    GitPusshuTen::Log.error "Command <#{cli.command.color(:red)}> not found."
    exit
  end
end

Instance Attribute Details

#cliObject

Command-line Interface



14
15
16
# File 'lib/gitpusshuten/command.rb', line 14

def cli
  @cli
end

#configurationObject

Configuration (Environment)



18
19
20
# File 'lib/gitpusshuten/command.rb', line 18

def configuration
  @configuration
end

#environmentObject

Environment connection



26
27
28
# File 'lib/gitpusshuten/command.rb', line 26

def environment
  @environment
end

#hooksObject

Contains pre/post-deployment hooks



22
23
24
# File 'lib/gitpusshuten/command.rb', line 22

def hooks
  @hooks
end

Instance Method Details

#available_commandsObject

Returns an array of available commands



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitpusshuten/command.rb', line 64

def available_commands
  commands = commands_directory.map do |command|
    unless blacklisted?(command)
      find(command)
    end
  end
  
  if configuration.respond_to?(:additional_modules)
    module_directory.each do |command|
      configuration.additional_modules.each do |additional_module|
        if command =~ /\/modules\/(#{additional_module})\/command\.rb/
          commands << $1
        end
      end
    end
  end
  
  commands.flatten.compact.uniq
end

#blacklisted?(command) ⇒ Boolean

Determines whether the provided command is blacklisted or not

Returns:

  • (Boolean)


97
98
99
# File 'lib/gitpusshuten/command.rb', line 97

def blacklisted?(command)
  BLACKLISTED.include?(find(command))
end

#classify_with_plural(word) ⇒ Object

Classifies the command, but re-pluralizes it in case it ended with the ‘s’ character to ensure the correct command is being invoked



151
152
153
154
155
156
157
# File 'lib/gitpusshuten/command.rb', line 151

def classify_with_plural(word)
  if word =~ /s$/
    word.classify + 's'
  else
    word.classify
  end
end

#commandObject

Wrapper for the command instance



58
59
60
# File 'lib/gitpusshuten/command.rb', line 58

def command
  @command ||= "GitPusshuTen::Commands::#{classify_with_plural(cli.command)}".constantize.new(cli, configuration, hooks, environment)
end

#commands_directoryObject

Returns the absolute path to each command (ruby file) insidethe commands directory and returns an array of each entry



87
88
89
# File 'lib/gitpusshuten/command.rb', line 87

def commands_directory
  Dir[File.expand_path(File.join(File.dirname(__FILE__), 'commands/*.rb'))]
end

#display_commandsObject

Displays a list of available commands in the CLI



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gitpusshuten/command.rb', line 110

def display_commands
  puts "\nGit Pusshu Ten\n\s\s\s\sプッシュ天\n\n"
  puts "[Aliases]\n\n"
  puts "\s\s#{y('gitpusshuten')}, #{y('heavenly')}, #{y('ten')}\n\n"
  puts "[Commands]\n\n"
  available_commands.compact.sort.each do |command|
    puts "\s\s" + y(command) + (command.length < 6 ? "\t" : "") + "\t" + get_constant_for(command).description          
  end
  puts "\n[Command Specific Help]\n\n" + "\s\sheavenly help <command>\n".color(:yellow)
  puts "For more information, visit: #{y 'http://gitpusshuten.com/'}"
end

#display_usage(command) ⇒ Object

Displays command specific details in the CLI



124
125
126
127
128
129
130
131
132
133
# File 'lib/gitpusshuten/command.rb', line 124

def display_usage(command)
  puts "\nGit Pusshu Ten\n\s\s\s\sプッシュ天\n\n"
  puts "[Command]\n\n\s\s#{y(command)}\n\n"
  puts "[Description]\n\n\s\s#{get_constant_for(command).description}\n\n"
  puts "#{get_constant_for(command).long_description.gsub(/^/, "\s\s")}\n\n" unless get_constant_for(command).long_description.nil?
  puts "[Usage]\n\n\s\s#{y get_constant_for(command).usage}\n\n"
  puts "[Examples]\n#{get_constant_for(command).example}\n\n"
  puts "For a list of all commands: #{y 'heavenly help'}"
  puts "For more information, visit: #{y 'http://gitpusshuten.com/'}"
end

#find(command) ⇒ Object

Expects a (full) path to the command ruby file and returns only the file name without the .rb extension



104
105
106
# File 'lib/gitpusshuten/command.rb', line 104

def find(command)
  command.gsub(/\.rb/, '').split('/').last
end

#get_constant_for(command) ⇒ Object

Returns the constant of a command



137
138
139
# File 'lib/gitpusshuten/command.rb', line 137

def get_constant_for(command)
  "GitPusshuTen::Commands::#{classify_with_plural(command)}".constantize
end

#module_directoryObject



91
92
93
# File 'lib/gitpusshuten/command.rb', line 91

def module_directory
  Dir[File.expand_path(File.join(File.dirname(__FILE__), 'modules', '*', 'command.rb'))]
end

#perform!Object

Performs the target command, based on the CLI and Configuration



50
51
52
53
54
# File 'lib/gitpusshuten/command.rb', line 50

def perform!
  %w[validate! pre_perform! perform! post_perform!].each do |action|
    command.send(action)
  end
end

#y(value) ⇒ Object

Wrapper for coloring text yellow



143
144
145
# File 'lib/gitpusshuten/command.rb', line 143

def y(value)
  value.to_s.color(:yellow)
end