Sha256: 927db51e28f0921a01ce337cfe1b322ec9678aa5212abbde519c4074767e0f61

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

module Airake #:nodoc:
  
  # Runs commands
  class Runner
    
    # Lifted from http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/a274d5d47feae95
    attr_reader :output, :cmd, :took, :process

    def initialize(cmd)
      @cmd = RUBY_PLATFORM =~ /win32/ ? "cmd.exe /c #{cmd} 2>&1" : "#{cmd} 2>&1"
    end
    
    def run(verbose = true)
      puts "Running: #{@cmd}" if verbose
    
      t1 = Time.now
      IO.popen(@cmd) do |f| 
        while s = f.read(1)
          printf s
          STDOUT.flush
        end
      end
      @process = $?
      @took = Time.now - t1
      
      if verbose
        success? or fail
        #puts "Took %.2fs" % [ @took ]
      end
      
    end    
    
    def fail
      raise <<-EOS 
[exited=#{exit_code}, pid=#{pid}] failed:
         
#{cmd}
          
EOS
    end
    
    def run?
      !@process.nil?
    end
    
    def exit_code
      @process ? @process.exitstatus : nil
    end
    
    def pid
      @process ? @process.pid : nil
    end

    def success?
      return run? && @process.success?
    end
    
    # Run airake command
    def self.run(command, method, *args)
      runner = self.new(command.send(method, *args))
      runner.run
    end
    
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
airake-0.2.11 lib/airake/runner.rb
airake-0.2.12 lib/airake/runner.rb
airake-0.2.13 lib/airake/runner.rb
airake-0.2.14 lib/airake/runner.rb
airake-0.3.1 lib/airake/runner.rb
airake-0.3.2 lib/airake/runner.rb
airake-0.4.0 lib/airake/runner.rb
airake-0.4.1 lib/airake/runner.rb
airake-0.4.4 lib/airake/runner.rb
airake-0.4.2 lib/airake/runner.rb
airake-0.4.3 lib/airake/runner.rb
airake-0.4.5 lib/airake/runner.rb