Sha256: 5011ace08953a81187e19efc11cb74f94b375347fc062ccdf051fa8d2a87c062

Contents?: true

Size: 1.75 KB

Versions: 113

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'bundler'
require 'eac_ruby_utils/ruby/on_replace_objects'

module EacRubyUtils
  module Ruby
    class << self
      # Executes a block in an environment when the variables BUNDLE* and RUBY* are removed.
      def on_clean_environment(&block)
        OnCleanEnvironment.new(&block).perform
      end

      class OnCleanEnvironment
        ENVVARS_PREFIXES_TO_CLEAN = %w[BUNDLE RUBY].freeze

        attr_reader :block, :original_env

        def initialize(&block)
          @block = block
          @original_env = ::ENV.to_h
        end

        # @return [Array<String>]
        def envvars_prefixes_to_clean
          ENVVARS_PREFIXES_TO_CLEAN
        end

        def perform
          bundler_with_unbundled_env do
            on_clean_envvars
          end
        end

        private

        def bundler_with_unbundled_env(&block)
          with_bundler_modified do
            ::Bundler.send(bundler_with_env_method_name, &block)
          end
        end

        def bundler_with_env_method_name
          if ::Bundler.respond_to?(:with_unbundled_env)
            :with_unbundled_env
          else
            :with_clean_env
          end
        end

        def clean_env
          r = original_env.dup
          r.delete_if { |k, _| envvars_prefixes_to_clean.any? { |prefix| k.start_with?(prefix) } }
          r
        end

        def on_clean_envvars
          ::Bundler.send('with_env', clean_env) { block.call }
        end

        def with_bundler_modified(&block)
          cloned_env = original_env.dup
          ::EacRubyUtils::Ruby.on_replace_objects do |replacer|
            replacer.replace_self_method(::Bundler, :original_env) { cloned_env }
            block.call
          end
        end
      end
    end
  end
end

Version data entries

113 entries across 113 versions & 2 rubygems

Version Path
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/ruby/on_clean_environment.rb