Class: Rake::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/config.rb,
lib/rake/extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gl_run(*args, &aBlock) ⇒ Object

Execute command (as is) on local_host using greenletters to enable expect like behaviour (useful for dealling with passwords).



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/rake/config.rb', line 654

def self.gl_run(*args, &aBlock)
  $TRACE = Rake.application.options.trace;

  options = Hash.new;
  if args.last.is_a?(Construct) then 
    raise ConfigurationError, "gl_run should not be passed a Construct";
  elsif args.last.is_a?(Hash)
    options = args.pop;
  end
  Conf.timeout = Hash.new() unless Conf.has_key?(:timeout);
  Conf.timeout.maxTimeout = 10 unless Conf.timeout.has_key?(:maxTimeout);
  if options.has_key?(:timeout) then
    options[:timeout] = Conf.timeout.maxTimeout if Conf.timeout.maxTimeout.to_i < options[:timeout].to_i ;
  else
    options[:timeout] = 10;
  end
  Conf.retries = 5 unless Conf.has_key?(:retries)
  options[:retries] = Conf.retries unless options.has_key?(:retries)

  Rake::Application.mesg args.join(' ') if $TRACE
  Rake::Application.mesg "Timeout: #{options[:timeout]}" if $TRACE
  Rake::Application.mesg "#{options}" if $TRACE && !options.empty?() && options.has_key?(:verbose);
  Rake::Application.mesg "Retries: #{options[:retries]}" if $TRACE

  asyncTriggersBlocks = Array.new;
  if options.has_key? :asyncTriggersBlocks then
    asyncTriggersBlocks = options[:asyncTriggersBlocks];
  end

  exitStatus = 0;
  if options.has_key? :exitStatus then
    exitStatus = options[:exitStatus];
  end

  options[:env] = ENV unless options.has_key?(:env);
  options[:transcript] = Rake::Application.logger unless options.has_key?(:transcript);

  attempt = 0
  tryAgain = true
  cmdProcess = nil
  while(tryAgain) do 
    attempt += 1
    tryAgain = false
    begin
      cmdProcess = Greenletters::Process.new(*args, options);
      cmdProcess.start!;

      asyncTriggersBlocks.each do | asyncTriggersBlock |
        if asyncTriggersBlock.kind_of? Proc then
          asyncTriggersBlock.call(cmdProcess);
        end
      end

      aBlock.call(cmdProcess) unless aBlock.nil?;

      cmdProcess.wait_for(:exit, exitStatus);
    rescue StandardError => se
      Rake::Application.mesg "Failed attempt #{attempt}" if $TRACE
      tryAgain = true
      raise se unless attempt < options[:retries]
    end
  end

  cmdProcess
end

.local_sh(*args, &aBlock) ⇒ Object

Execute command using sh on local_host using greenletters.



639
640
641
642
643
644
645
646
647
648
649
# File 'lib/rake/config.rb', line 639

def self.local_sh(*args, &aBlock)
  options = if args.last.is_a?(Hash) then args.pop else {} end
  options = Conf.sh.data.merge(options);
  cmd     = [options[:command], 
             options[:cmdOptions],
             "-c", 
             args.flatten.join(' ')
            ].flatten

  gl_run(*cmd, options, &aBlock);
end

.remote_ssh(*args, &aBlock) ⇒ Object

Execute command using ssh to remote_host using greenletters.



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/rake/config.rb', line 617

def self.remote_ssh(*args, &aBlock)
  options = if args.last.is_a?(Hash) then args.pop else {} end
  options = Conf.ssh.data.merge(options);
  options[:timeout] = 10 unless options.has_key?(:timeout);
  sshPort = Array.new();
  sshPort = [ '-p', options[:remotePort]] if options.has_key?(:remotePort);
  cmd     = [options[:command], 
             options[:cmdOptions], 
             '-l',
             options[:userId],
             sshPort,
             options[:remoteHost]
            ].flatten

  gl_run(*cmd, options) do | p |
    p.wait_for(:output, Regexp.new(options[:commandPromptRegExp]));
    Rake::Application.mesg args.flatten.join(' ') if Rake.application.options.trace;
    p.puts args.flatten.join(' ') + '; exit';
  end
end

.rsync_from_remote(remotePath, localPath, options = {}, &aBlock) ⇒ Object

Execute the rsync command on the arguments provided using greenletters.



604
605
606
607
608
609
610
611
612
613
614
# File 'lib/rake/config.rb', line 604

def self.rsync_from_remote(remotePath, localPath, options = {}, &aBlock)
  options = Conf.rsync.data.merge(options);
  options[:timeout] = 10 unless options.has_key?(:timeout);
  cmd     = [options[:command], 
             options[:cmdOptions], 
             "#{options[:userId]}@#{options[:remoteHost]}:#{remotePath}",
             localPath,
            ].flatten

  gl_run(*cmd, options, &aBlock);
end

.rsync_to_remote(localPath, remotePath, options = {}, &aBlock) ⇒ Object

Execute the rsync command on the arguments provided using greenletters.



588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/rake/config.rb', line 588

def self.rsync_to_remote(localPath, remotePath, options = {}, &aBlock)
  if localPath =~ /\*/ then
    localPath = Dir.glob(localPath);
  end
  options = Conf.rsync.data.merge(options);
  options[:timeout] = 10 unless options.has_key?(:timeout);
  cmd     = [options[:command], 
             options[:cmdOptions], 
             localPath,
             "#{options[:userId]}@#{options[:remoteHost]}:#{remotePath}",
            ].flatten

  gl_run(*cmd, options, &aBlock);
end

.scp_from_remote(remotePath, localPath, options = {}, &aBlock) ⇒ Object

Execute the rsync command on the arguments provided using greenletters.



575
576
577
578
579
580
581
582
583
584
585
# File 'lib/rake/config.rb', line 575

def self.scp_from_remote(remotePath, localPath, options = {}, &aBlock)
  options = Conf.scp.data.merge(options);
  options[:timeout] = 10 unless options.has_key?(:timeout);
  cmd     = [options[:command], 
             options[:cmdOptions], 
             "#{options[:userId]}@#{options[:remoteHost]}:#{remotePath}",
             localPath,
            ].flatten

  gl_run(*cmd, options, &aBlock);
end

.scp_to_remote_scp(localPath, remotePath, options = {}, &aBlock) ⇒ Object

Execute the rsync command on the arguments provided using greenletters.



562
563
564
565
566
567
568
569
570
571
572
# File 'lib/rake/config.rb', line 562

def self.scp_to_remote_scp(localPath, remotePath, options = {}, &aBlock)
  options = Conf.scp.data.merge(options);
  options[:timeout] = 10 unless options.has_key?(:timeout);
  cmd     = [options[:command], 
             options[:cmdOptions], 
             localPath,
             "#{options[:userId]}@#{options[:remoteHost]}:#{remotePath}",
            ].flatten

  gl_run(*cmd, options, &aBlock);
end

Instance Method Details

#execute(args = nil) ⇒ Object

Execute the actions associated with this task.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rake/extensions.rb', line 250

def execute(args=nil)
  args ||= EMPTY_TASK_ARGS
  if application.options.dryrun then
    Rake::Application.mesg "** Execute (dry run) #{name}"
    return
  end
  if application.options.trace then
    Rake::Application.mesg "** Execute #{name}"
  end
  application.enhance_with_matching_rule(name) if @actions.empty?
  @actions.each do |act|
    case act.arity
    when 1
      act.call(self)
    else
      act.call(self, args)
    end
  end
end

#invoke_with_call_chain(task_args, invocation_chain) ⇒ Object

Same as invoke, but explicitly pass a call chain to detect circular dependencies.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rake/extensions.rb', line 233

def invoke_with_call_chain(task_args, invocation_chain) # :nodoc:
  new_chain = InvocationChain.append(self, invocation_chain)
  @lock.synchronize do
    if application.options.trace then
      Rake::Application.mesg "** Invoke #{name} #{format_trace_flags}"
    end
    return if @already_invoked
    @already_invoked = true
    invoke_prerequisites(task_args, new_chain)
    execute(task_args) if needed?
  end
rescue Exception => ex
  add_chain_to(ex, new_chain)
  raise ex
end