Sha256: 84b32a72789bf48f01907702904fead7360fec7e9378364c39786cc7b2c197b5

Contents?: true

Size: 1.92 KB

Versions: 23

Compression:

Stored size: 1.92 KB

Contents

require "foreman"
require "shellwords"

class Foreman::Process

  attr_reader :command
  attr_reader :env

  # Create a Process
  #
  # @param [String] command  The command to run
  # @param [Hash]   options
  #
  # @option options [String] :cwd (./)  Change to this working directory before executing the process
  # @option options [Hash]   :env ({})  Environment variables to set for this process
  #
  def initialize(command, options={})
    @command = command
    @options = options.dup

    @options[:env] ||= {}
  end

  # Get environment-expanded command for a +Process+
  #
  # @param [Hash] custom_env ({}) Environment variables to merge with defaults
  #
  # @return [String]  The expanded command
  #
  def expanded_command(custom_env={})
    env = @options[:env].merge(custom_env)
    expanded_command = command.dup
    env.each do |key, val|
      expanded_command.gsub!("$#{key}", val)
    end
    expanded_command
  end

  # Run a +Process+
  #
  # @param [Hash] options
  #
  # @option options :env    ({})       Environment variables to set for this execution
  # @option options :output ($stdout)  The output stream
  #
  # @returns [Fixnum] pid  The +pid+ of the process
  #
  def run(options={})
    env    = @options[:env].merge(options[:env] || {})
    output = options[:output] || $stdout
    runner = "#{Foreman.runner}".shellescape
    
    Dir.chdir(cwd) do
      Process.spawn env, expanded_command(env), :out => output, :err => output
    end
  end

  # Exec a +Process+
  #
  # @param [Hash] options
  #
  # @option options :env ({}) Environment variables to set for this execution
  #
  # @return Does not return
  def exec(options={})
    env = @options[:env].merge(options[:env] || {})
    env.each { |k, v| ENV[k] = v }
    Dir.chdir(cwd)
    Kernel.exec expanded_command(env)
  end

  # Returns the working directory for this +Process+
  #
  # @returns [String]
  #
  def cwd
    File.expand_path(@options[:cwd] || ".")
  end

end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
foreman-0.88.1 lib/foreman/process.rb
foreman-0.87.2 lib/foreman/process.rb
foreman-0.87.1 lib/foreman/process.rb
ypadlyak-foreman-0.85.0 lib/foreman/process.rb
ypadlyak-foreman-0.81.0 lib/foreman/process.rb
foreman-0.87.0 lib/foreman/process.rb
foreman-0.86.0 lib/foreman/process.rb
foreman-0.85.0 lib/foreman/process.rb
foreman-0.84.0 lib/foreman/process.rb
foreman-0.83.0 lib/foreman/process.rb
foreman-systemd-0.78.0 lib/foreman/process.rb
foreman-0.82.0 lib/foreman/process.rb
foreman-0.81.0 lib/foreman/process.rb
foreman-0.78.0 lib/foreman/process.rb
foreman-0.77.0 lib/foreman/process.rb
foreman-0.76.0 lib/foreman/process.rb
foreman-0.75.0 lib/foreman/process.rb
foreman-0.74.0 lib/foreman/process.rb
foreman-0.73.0 lib/foreman/process.rb
foreman-0.71.0 lib/foreman/process.rb