Sha256: 3babfcfd9041a68c7934a63370c7ac94d6a3122bcaf358be54d9cc7d0042f1a9

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 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
      if command =~ /^bundle/
        @command = command
      else
        @command = "bundle exec #{command}"
      end
    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
      if @gemfile
        puts ">> BUNDLE_GEMFILE=#{@gemfile} #{@command}"
      else
        puts ">> #{@command}"
      end
    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

4 entries across 4 versions & 2 rubygems

Version Path
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/appraisal-0.5.1/lib/appraisal/command.rb
appraisal-0.5.2 lib/appraisal/command.rb
appraisal-0.5.1 lib/appraisal/command.rb
appraisal-0.5.0 lib/appraisal/command.rb