Sha256: 073ae623c083832032aa90f326e589cb11a5b289e998bc2ca349e826af3e4741

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Appraisal
  # Executes commands with a clean environment
  class Command
    BUNDLER_ENV_VARS = %w(RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE).freeze

    def self.from_args(gemfile)
      command = ([$0] + ARGV.slice(1, ARGV.size)).join(' ')
      new(command, gemfile)
    end

    def initialize(command, gemfile = nil)
      @original_env = {}
      @gemfile = gemfile
      @command = command
    end

    def run
      announce
      with_clean_env do
        unless Kernel.system(@command)
          exit(1)
        end
      end
    end

    def exec
      announce
      with_clean_env { Kernel.exec(@command) }
    end

    private

    def with_clean_env
      unset_bundler_env_vars
      ENV['BUNDLE_GEMFILE'] = @gemfile
      yield
    ensure
      restore_env
    end

    def announce
      puts ">> BUNDLE_GEMFILE=#{@gemfile} #{@command}"
    end

    def unset_bundler_env_vars
      BUNDLER_ENV_VARS.each do |key|
        @original_env[key] = ENV[key]
        ENV[key] = nil
      end
    end

    def restore_env
      @original_env.each { |key, value| ENV[key] = value }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appraisal-0.1 lib/appraisal/command.rb