brew/Library/Homebrew/build_environment.rb
Jack Nagel 2503cedf2c Object#instance_exec for Ruby 1.8.6
Not thread safe! But I don't think we care.

We want to evaluate the env DSL block in the context of ENV for asthetic
reasons, but we also want access to methods on the requirement instance.
We can use #instance_exec to pass the requirement itself into the block:

  class Foo < Requirement
    env do |req|
      append 'PATH', req.some_path
    end

    def some_path
      which 'something'
    end
  end

Also add a simplified version of Object#instance_exec for Ruby 1.8.6.
2013-01-21 17:24:11 -06:00

46 lines
748 B
Ruby

require 'set'
class BuildEnvironment
def initialize(*settings)
@settings = Set.new(settings)
end
def <<(o)
@settings << o
self
end
def std?
@settings.include? :std
end
def userpaths?
@settings.include? :userpaths
end
def modify_build_environment(context=nil)
p = @settings.find { |s| Proc === s }
ENV.instance_exec(context, &p) unless p.nil?
end
def _dump(*)
@settings.dup.reject { |s| Proc === s }.join(":")
end
def self._load(s)
new(*s.split(":").map(&:to_sym))
end
end
module BuildEnvironmentDSL
def env(*settings, &block)
@env ||= BuildEnvironment.new
if block_given?
@env << block
else
settings.each { |s| @env << s }
end
@env
end
end