Class: BuildTool::Commands::Base

Inherits:
Object
  • Object
show all
Includes:
HelpText
Defined in:
lib/build-tool/commands.rb

Overview

Base class for all commands

Direct Known Subclasses

Standard, SubCommands

Defined Under Namespace

Classes: UsageError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelpText

#cmdalias, #description, included, #long_description, #name

Constructor Details

#initialize(parent = nil) ⇒ Base

Returns a new instance of Base.



93
94
95
96
97
# File 'lib/build-tool/commands.rb', line 93

def initialize(parent = nil)
    @skip_command = false
    @parent = parent
    @cmd = nil
end

Instance Attribute Details

#cmdObject

The commands entry in the history db or nil



91
92
93
# File 'lib/build-tool/commands.rb', line 91

def cmd
  @cmd
end

#optionsObject (readonly)

The OptionParser instance associated with this command



85
86
87
# File 'lib/build-tool/commands.rb', line 85

def options
  @options
end

#parentObject (readonly)

The parent command if this is a sub command



88
89
90
# File 'lib/build-tool/commands.rb', line 88

def parent
  @parent
end

Instance Method Details

#<=>(other) ⇒ Object



80
81
82
# File 'lib/build-tool/commands.rb', line 80

def <=> (other)
    return self.name <=> other.name
end

#applicable?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/build-tool/commands.rb', line 99

def applicable?
    true
end

#cleanup_after_vcs_accessObject

Cleanup after vcs_access



218
219
220
221
222
223
224
225
226
227
# File 'lib/build-tool/commands.rb', line 218

def cleanup_after_vcs_access
    if MJ::Tools::SSH::has_keys?
        logger.info ""
        logger.info "#### Cleaning up the ssh keys"
        MJ::Tools::SSH::keys.each do |file|
            logger.info "  - Removing key #{file}"
        end
        MJ::Tools::SSH::cleanup()
    end
end

#complete(string) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/build-tool/commands.rb', line 166

def complete( string )
    if Readline.respond_to? "line_buffer"
        complete_readline_1_9( string )
    else
        complete_readline_1_8( string )
    end
end

#complete_arguments(args) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/build-tool/commands.rb', line 174

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_1_8(string) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/build-tool/commands.rb', line 110

def complete_readline_1_8( string )
    # Readline in 1.8 does not provide all needed information. We have to hand made
    # it ourselves. We get the complete commandline here.

    # We want the complete commandline in line_buffer
    line_buffer = string
    # An array of completed arguments/options in args
    args = Shellwords.shellwords( 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 line_buffer.empty? or line_buffer[-1] == 32
        args << ""
    end

    do_complete_1_8( args )
end

#complete_readline_1_9(string) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/build-tool/commands.rb', line 140

def complete_readline_1_9( 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( string )

    # 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_1_9( args )
end

#configurationObject



103
104
105
# File 'lib/build-tool/commands.rb', line 103

def configuration
    Application::instance.configuration
end

#do_complete_1_8(args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/build-tool/commands.rb', line 128

def do_complete_1_8( args )
    # puts "do_complete_1_8( #{args.inspect} )"
    compl = complete_arguments( args )
    if compl.length >= 1
        return compl.collect do |res|
            args.dup[0..-2].push(res).join(" ")
        end
    else
        return []
    end
end

#do_complete_1_9(args) ⇒ Object



162
163
164
# File 'lib/build-tool/commands.rb', line 162

def do_complete_1_9( args )
    return complete_arguments( args )
end

#do_executeObject

Raises:

  • (NotImplementedError)


204
205
206
# File 'lib/build-tool/commands.rb', line 204

def do_execute
    raise NotImplementedError, "#{self.class}.do_execute() not implemented"
end

#each_optionObject



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/build-tool/commands.rb', line 190

def each_option
    list = options.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 sw.is_a? String } }.flatten.compact
    end
end

#execute(args) ⇒ Object



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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/build-tool/commands.rb', line 229

def execute( args )
    @options = OptionParser.new
    @options.set_program_name( name )
    @options.separator long_description if long_description

    @parsing = true
    initialize_options
    begin
        realargs =  @options.parse( args )
    rescue OptionParser::ParseError => e
        logger.error( "#{e}" )
        @skip_command = true
    end
    @parsing = false

    setup_command

    # Log the command into history
    state = BuildTool::History::CommandLog::FINISHED_SUCCESSFUL
    rc = -1 # -1 for @skip_command
    if log?
        @cmd = BuildTool::History::CommandLog.new( :command => "#{self.fullname} #{args.join( ' ')}", :logdir => log_directory  )
        @cmd.started
    end
    if !@skip_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
            logger.error   e.message
            logger.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

#fullnameString

Return the commands full name.

The full name includes the parents name (if any).

Returns:

  • (String)

    The commands full name



290
291
292
293
294
295
296
# File 'lib/build-tool/commands.rb', line 290

def fullname
    if parent && parent.fullname
        return parent.fullname + " " + name
    else
        return name
    end
end

#initialize_optionsObject



107
108
# File 'lib/build-tool/commands.rb', line 107

def initialize_options
end

#log?Boolean

Should this command be logged?

Returns:

  • (Boolean)


300
301
302
# File 'lib/build-tool/commands.rb', line 300

def log?
    false
end

#say(*args) ⇒ Object



317
318
319
# File 'lib/build-tool/commands.rb', line 317

def say( *args )
    logger.info( *args )
end

#setup_commandObject



208
209
# File 'lib/build-tool/commands.rb', line 208

def setup_command
end

#show_help(args = []) ⇒ Object



304
305
306
307
308
309
# File 'lib/build-tool/commands.rb', line 304

def show_help( args = [] )
    # Just ignore potential arguments
    say @options.help
    skip_command
    return 0
end

#skip_commandObject

During option parsing use this method to tell the class to not execute the command



313
314
315
# File 'lib/build-tool/commands.rb', line 313

def skip_command
    @skip_command = true
end

#summarizeObject



214
215
# File 'lib/build-tool/commands.rb', line 214

def summarize
end

#teardown_commandObject



211
212
# File 'lib/build-tool/commands.rb', line 211

def teardown_command
end

#usage(error) ⇒ Object



321
322
323
324
325
# File 'lib/build-tool/commands.rb', line 321

def usage( error )
    logger.error error
    show_help
    return -2
end