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?.
- #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
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/build-tool/commands.rb', line 191 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)
315 316 317 |
# File 'lib/build-tool/commands.rb', line 315 def debug( *args ) logger.debug( *args ) end |
#debug2(*args) ⇒ Object
319 320 321 |
# File 'lib/build-tool/commands.rb', line 319 def debug2( *args ) logger.debug2( *args ) end |
#do_complete(args) ⇒ Object
154 155 156 157 |
# File 'lib/build-tool/commands.rb', line 154 def do_complete( args ) return complete_arguments( args ) do_complete( args ) end |
#do_execute ⇒ Object
177 178 179 |
# File 'lib/build-tool/commands.rb', line 177 def do_execute raise NotImplementedError, "#{self.class}.do_execute() not implemented" end |
#each_option ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/build-tool/commands.rb', line 159 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)
329 330 331 |
# File 'lib/build-tool/commands.rb', line 329 def error( *args ) logger.error( *args ) end |
#execute(args) ⇒ Object
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 264 |
# File 'lib/build-tool/commands.rb', line 212 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).
272 273 274 275 276 277 278 |
# File 'lib/build-tool/commands.rb', line 272 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)
300 301 302 |
# File 'lib/build-tool/commands.rb', line 300 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?
282 283 284 |
# File 'lib/build-tool/commands.rb', line 282 def log? false end |
#setup_command ⇒ Object
181 182 |
# File 'lib/build-tool/commands.rb', line 181 def setup_command end |
#setup_options ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/build-tool/commands.rb', line 202 def @options = OptionParser.new @options.set_program_name( name ) if long_description @options.separator '' @options.separator long_description end end |
#show_help(args = []) ⇒ Object
286 287 288 289 290 291 |
# File 'lib/build-tool/commands.rb', line 286 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
295 296 297 |
# File 'lib/build-tool/commands.rb', line 295 def skip_command @skip_command = true end |
#summarize ⇒ Object
187 188 |
# File 'lib/build-tool/commands.rb', line 187 def summarize end |
#teardown_command ⇒ Object
184 185 |
# File 'lib/build-tool/commands.rb', line 184 def teardown_command end |
#trace(*args) ⇒ Object
Print out a verbose message (if enabled)
310 311 312 |
# File 'lib/build-tool/commands.rb', line 310 def trace( *args ) logger.trace( *args ) end |
#usage(error) ⇒ Object
333 334 335 336 337 |
# File 'lib/build-tool/commands.rb', line 333 def usage( error ) error( error ) show_help return -2 end |
#verbose(*args) ⇒ Object
Print out a verbose message (if enabled)
305 306 307 |
# File 'lib/build-tool/commands.rb', line 305 def verbose( *args ) logger.verbose( *args ) end |
#warn(*args) ⇒ Object
Print out a warning message (if enabled)
324 325 326 |
# File 'lib/build-tool/commands.rb', line 324 def warn( *args ) logger.warn( *args ) end |