Sha256: 39f5817454df0a2a1c58c622ac301d97e91123761daefb8fcaa3d3f9990e4d32

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'albacore/support/failure'

module RunCommand
  include Failure
  
  attr_accessor :path_to_command, :require_valid_command, :command_directory
  
  def initialize
    @require_valid_command = true
    @command_directory = Dir.pwd
    super()
  end
  
  def run_command(command_name="Command Line", command_parameters="")
    if @require_valid_command
      return false unless valid_command_exists
    end
    
    command = "\"#{@path_to_command}\" #{command_parameters}"
    @logger.debug "Executing #{command_name}: #{command}"
    
    set_working_directory    
    result = system command
    reset_working_directory
    
    result
  end
  
  def valid_command_exists
    return true if File.exist?(@path_to_command || "")
    msg = "Command not found: #{@path_to_command}"
    @logger.fatal msg
  end
  
  def set_working_directory
    @original_directory = Dir.pwd
    return if @command_directory == @original_directory
    Dir.chdir(@command_directory)
  end
  
  def reset_working_directory
    return if Dir.pwd == @original_directory
    Dir.chdir(@original_directory)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
albacore-0.1.0 lib/albacore/support/runcommand.rb