Sha256: d9ca135701f26b4a8128c365c2621a2d30924a799d30bd0af56cd5b6b0ac77ef

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

#
# Command
#

module VCSRuby
  class Command
    attr_reader :name
    def initialize name, command
      @name = name
      @command = which(command)
      @available = !!@command
    end

    def available?
      @available
    end

    def execute parameter, streams = 0, no_error = false
      raise "Command '#{name}' not available" unless available?
      puts "#{@command} #{parameter} #{streams}" if Tools.verbose?
      result = nil
      if Tools::windows?
        streams = '2> nul' if streams === 0

        result = `cmd /C #{@command} #{parameter} #{streams}`
      else
        streams = "2> /dev/null" if streams === 0

        result =`#{@command} #{parameter} #{streams}`
      end

      raise "#{@command} failed with return value '#{$?}'" unless $?.to_i == 0 || no_error
      return result
    end

private
    # http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
    def which cmd
      exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
      ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
        exts.each do |ext|
          exe = File.join(path, "#{cmd}#{ext}")
          return exe if File.executable?(exe) && !File.directory?(exe)
        end
      end
      return nil
    end
  end
 end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vcs_ruby-1.0.1 lib/command.rb
vcs_ruby-1.0.0 lib/command.rb