lib/autoproj/variable_expansion.rb in autoproj-2.0.0.rc37 vs lib/autoproj/variable_expansion.rb in autoproj-2.0.0.rc38

- old
+ new

@@ -24,44 +24,19 @@ end end result end - # Expand build options in +value+. - # - # The method will expand in +value+ patterns of the form $NAME, replacing - # them with the corresponding build option. - def self.expand_environment(value) - return value if !contains_expansion?(value) - - # Perform constant expansion on the defined environment variables, - # including the option set - options = flatten_recursive_hash(config.validated_values) - - loop do - new_value = single_expansion(value, options) - if new_value == value - break - else - value = new_value - end - end - value - end - # Does a non-recursive expansion in +data+ of configuration variables # ($VAR_NAME) listed in +definitions+ # # If the values listed in +definitions+ also contain configuration # variables, they do not get expanded - def self.single_expansion(data, definitions, options = Hash.new) - options = Kernel.validate_options options, config: Autoproj.config - + def self.single_expansion(data, definitions) if !data.respond_to?(:to_str) return data end - definitions = { 'HOME' => ENV['HOME'] }.merge(definitions) data = data.gsub(/(.|^)\$(\w+)/) do |constant_name| prefix = constant_name[0, 1] if prefix == "\\" next(constant_name[1..-1]) @@ -70,15 +45,14 @@ prefix, constant_name = "", constant_name[1..-1] else constant_name = constant_name[2..-1] end - if !(value = definitions[constant_name]) - if !(value = options[:config].get(constant_name)) - if !block_given? || !(value = yield(constant_name)) - raise ArgumentError, "cannot find a definition for $#{constant_name}" - end + value = definitions[constant_name] + if value.nil? + if !block_given? || !(value = yield(constant_name)) + raise ArgumentError, "cannot find a definition for $#{constant_name}" end end "#{prefix}#{value}" end data @@ -107,28 +81,27 @@ # True if the given string contains expansions def self.contains_expansion?(string); string =~ /\$/ end def self.resolve_one_constant(name, value, result, definitions) - result[name] = single_expansion(value, result) do |missing_name| - result[missing_name] = resolve_one_constant(missing_name, definitions.delete(missing_name), result, definitions) + result[name] ||= single_expansion(value, result) do |missing_name| + result[missing_name] = + resolve_one_constant(missing_name, definitions[missing_name], result, definitions) end end # Resolves all possible variable references from +constants+ # # I.e. replaces variables by their values, so that no value in +constants+ # refers to variables defined in +constants+ - def self.resolve_constant_definitions(constants) - constants = constants.dup - constants['HOME'] = ENV['HOME'] + def self.resolve_constant_definitions(constants, definitions = Hash.new) + definitions = definitions.merge(constants) - result = Hash.new - while !constants.empty? - name = constants.keys.first - value = constants.delete(name) - resolve_one_constant(name, value, result, constants) + all_resolutions = Hash.new + resolution_cache = Hash.new + constants.each do |key, value| + all_resolutions[key] = resolve_one_constant(key, value, resolution_cache, definitions) end - result + all_resolutions end end