Class: BuildTool::Commands::Base
- Inherits:
-
Object
- Object
- BuildTool::Commands::Base
- Includes:
- ANSI::Code, HelpText
- Defined in:
- lib/build-tool/commands.rb
Overview
Base class for all commands
Direct Known Subclasses
Defined Under Namespace
Classes: UsageError
Instance Attribute Summary collapse
-
#cmd ⇒ Object
The commands entry in the history db or nil.
-
#options ⇒ Object
readonly
The OptionParser instance associated with this command.
-
#parent ⇒ Object
readonly
The parent command if this is a sub command.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #applicable? ⇒ Boolean
-
#cleanup_after_vcs_access ⇒ Object
Cleanup after vcs_access.
- #complete_arguments(args) ⇒ Object
- #complete_readline(string) ⇒ Object
- #configuration ⇒ Object
-
#debug(*args) ⇒ Object
Print out a verbose message (if enabled).
- #debug2(*args) ⇒ Object
- #do_complete(args) ⇒ Object
- #do_execute ⇒ Object
- #each_option ⇒ Object
-
#error(*args) ⇒ Object
Print out a error message (if enabled).
- #execute(args) ⇒ Object
-
#fullname ⇒ String
Return the commands full name.
-
#info(*args) ⇒ Object
Print out a normal message (if enabled).
-
#initialize(parent = nil) ⇒ Base
constructor
A new instance of Base.
- #initialize_options ⇒ Object
-
#log? ⇒ Boolean
Should this command be logged?.
-
#quiet(*args) ⇒ Object
Print out a normal message (if enabled).
- #setup_command ⇒ Object
- #setup_options ⇒ Object
- #show_help(args = []) ⇒ Object
-
#skip_command ⇒ Object
During option parsing use this method to tell the class to not execute the command.
- #summarize ⇒ Object
- #teardown_command ⇒ Object
-
#trace(*args) ⇒ Object
Print out a verbose message (if enabled).
- #usage(error) ⇒ Object
-
#verbose(*args) ⇒ Object
Print out a verbose message (if enabled).
-
#warn(*args) ⇒ Object
Print out a warning message (if enabled).
Methods included from HelpText
#cmdalias, #description, included, #long_description, #name
Constructor Details
#initialize(parent = nil) ⇒ Base
Returns a new instance of Base.
98 99 100 101 102 103 |
# File 'lib/build-tool/commands.rb', line 98 def initialize(parent = nil) @skip_command = false @parent = parent @cmd = nil end |
Instance Attribute Details
#cmd ⇒ Object
The commands entry in the history db or nil
96 97 98 |
# File 'lib/build-tool/commands.rb', line 96 def cmd @cmd end |
#options ⇒ Object (readonly)
The OptionParser instance associated with this command
90 91 92 |
# File 'lib/build-tool/commands.rb', line 90 def @options end |
#parent ⇒ Object (readonly)
The parent command if this is a sub command
93 94 95 |
# File 'lib/build-tool/commands.rb', line 93 def parent @parent end |
Instance Method Details
#<=>(other) ⇒ Object
85 86 87 |
# File 'lib/build-tool/commands.rb', line 85 def <=> (other) return self.name <=> other.name end |
#applicable? ⇒ Boolean
105 106 107 |
# File 'lib/build-tool/commands.rb', line 105 def applicable? true end |
#cleanup_after_vcs_access ⇒ Object
Cleanup after vcs_access
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/build-tool/commands.rb', line 190 def cleanup_after_vcs_access if MJ::Tools::SSH::has_keys? info( "" ) info( "#### Cleaning up the ssh keys" ) MJ::Tools::SSH::keys.each do |file| info( " - Removing key #{file}" ) end MJ::Tools::SSH::cleanup() end end |
#complete_arguments(args) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/build-tool/commands.rb', line 138 def complete_arguments( args ) res = each_option.collect { |sw| sw.long.collect { |long| res = [] long = long.clone if long.sub!( '[no-]', 'no-' ) res << long if long.start_with? args[-1] long = long.sub( 'no-', '' ) end res << long if long.start_with? args[-1] res } } res.flatten.compact.sort end |
#complete_readline(string) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/build-tool/commands.rb', line 116 def complete_readline( string ) # We have three sources of information (ruby 1.9 only) # argument: # See Readline::completer_word_break_characters . The part # of the command line to complete # Readline::line_buffer # The complete command line # Readline::point # The position of the cursor in commandline # Ruby 1.9 <start> # First check if we have to complete the first shellword (command) args = Shellwords.shellwords( Readline.line_buffer ) # And the string to complete in string. If the last character is a space we # complete from that. if not we take the last arg if string.empty? or string[-1] == 32 args << "" end do_complete( args ) end |
#configuration ⇒ Object
109 110 111 |
# File 'lib/build-tool/commands.rb', line 109 def configuration Application::instance.configuration end |
#debug(*args) ⇒ Object
Print out a verbose message (if enabled)
319 320 321 |
# File 'lib/build-tool/commands.rb', line 319 def debug( *args ) logger.debug( *args ) end |
#debug2(*args) ⇒ Object
323 324 325 |
# File 'lib/build-tool/commands.rb', line 323 def debug2( *args ) logger.debug2( *args ) end |
#do_complete(args) ⇒ Object
154 155 156 |
# File 'lib/build-tool/commands.rb', line 154 def do_complete( args ) return complete_arguments( args ) end |
#do_execute ⇒ Object
176 177 178 |
# File 'lib/build-tool/commands.rb', line 176 def do_execute raise NotImplementedError, "#{self.class}.do_execute() not implemented" end |
#each_option ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/build-tool/commands.rb', line 158 def each_option list = .instance_variable_get( "@stack" ) if block_given? list.each do |l| l.list.each do |sw| next if sw.is_a? String yield sw end end else list.collect { |l| l.list.collect { |sw| sw unless not sw.is_a? OptionParser::Switch } }.flatten.compact end end |
#error(*args) ⇒ Object
Print out a error message (if enabled)
333 334 335 |
# File 'lib/build-tool/commands.rb', line 333 def error( *args ) logger.error( *args ) end |
#execute(args) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/build-tool/commands.rb', line 211 def execute( args ) # Reinitialize the option parser. begin realargs = @options.parse( args ) rescue OptionParser::ParseError => e error( "#{name}: #{e}" ) @skip_command = true end setup_command # Log the command into history state = BuildTool::History::CommandLog::FINISHED_SUCCESSFUL rc = -1 # -1 for @skip_command if !@skip_command # Initialize the command log if necessary if log? @cmd = BuildTool::History::CommandLog.new( :command => "#{self.fullname} #{args.join( ' ')}", :logdir => log_directory.to_s.encode( 'UTF-8' ) ) @cmd.started end # Now execute the command begin rc = do_execute( realargs ) if rc != 0 state = BuildTool::History::CommandLog::FINISHED_WITH_ERRORS end rescue BuildTool::Error => e state = BuildTool::History::CommandLog::FINISHED_WITH_ERRORS error( e. ) verbose( e.backtrace.join("\n") ) rc = -1 rescue Interrupt => e state = BuildTool::History::CommandLog::CANCELED_BY_USER raise e ensure if @cmd @cmd.finished( state ) @cmd = nil end teardown_command summarize end end # Either we skipped the command or we catched an exception return rc; end |
#fullname ⇒ String
Return the commands full name.
The full name includes the parents name (if any).
271 272 273 274 275 276 277 |
# File 'lib/build-tool/commands.rb', line 271 def fullname if parent && parent.fullname return parent.fullname + " " + name else return name end end |
#info(*args) ⇒ Object
Print out a normal message (if enabled)
304 305 306 |
# File 'lib/build-tool/commands.rb', line 304 def info( *args ) logger.info( *args ) end |
#initialize_options ⇒ Object
113 114 |
# File 'lib/build-tool/commands.rb', line 113 def end |
#log? ⇒ Boolean
Should this command be logged?
281 282 283 |
# File 'lib/build-tool/commands.rb', line 281 def log? false end |
#quiet(*args) ⇒ Object
Print out a normal message (if enabled)
299 300 301 |
# File 'lib/build-tool/commands.rb', line 299 def quiet( *args ) logger.quiet( *args ) end |
#setup_command ⇒ Object
180 181 |
# File 'lib/build-tool/commands.rb', line 180 def setup_command end |
#setup_options ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/build-tool/commands.rb', line 201 def @options = OptionParser.new @options.set_program_name( name ) if long_description @options.separator '' @options.separator long_description end end |
#show_help(args = []) ⇒ Object
285 286 287 288 289 290 |
# File 'lib/build-tool/commands.rb', line 285 def show_help( args = [] ) # Just ignore potential arguments info( .help ) skip_command return 0 end |
#skip_command ⇒ Object
During option parsing use this method to tell the class to not execute the command
294 295 296 |
# File 'lib/build-tool/commands.rb', line 294 def skip_command @skip_command = true end |
#summarize ⇒ Object
186 187 |
# File 'lib/build-tool/commands.rb', line 186 def summarize end |
#teardown_command ⇒ Object
183 184 |
# File 'lib/build-tool/commands.rb', line 183 def teardown_command end |
#trace(*args) ⇒ Object
Print out a verbose message (if enabled)
314 315 316 |
# File 'lib/build-tool/commands.rb', line 314 def trace( *args ) logger.trace( *args ) end |
#usage(error) ⇒ Object
337 338 339 340 341 |
# File 'lib/build-tool/commands.rb', line 337 def usage( error ) error( error ) show_help return -2 end |
#verbose(*args) ⇒ Object
Print out a verbose message (if enabled)
309 310 311 |
# File 'lib/build-tool/commands.rb', line 309 def verbose( *args ) logger.verbose( *args ) end |
#warn(*args) ⇒ Object
Print out a warning message (if enabled)
328 329 330 |
# File 'lib/build-tool/commands.rb', line 328 def warn( *args ) logger.warn( *args ) end |