From 9713be8dfd1f500389aee5e4b8f740683261c906 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 5 Jul 2012 18:27:59 -0400 Subject: [PATCH] add property support, too --- features/ruby_gemfile.feature | 14 ++++++++++++++ lib/penchant/gemfile.rb | 27 ++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/features/ruby_gemfile.feature b/features/ruby_gemfile.feature index 297ef91..8156346 100644 --- a/features/ruby_gemfile.feature +++ b/features/ruby_gemfile.feature @@ -249,3 +249,17 @@ Feature: Gemfiles # generated by penchant, environment: local gem "one", "1.2.3", {:path=>"../cats"} """ + + Scenario: Define special expansions for properties + Given I have the file "Gemfile.penchant" with the content: + """ + defaults_for env(:local), :path => '../%s' + property(:github) { |name| { :git => "git://github.com/#{name}/%s.git" } } + gem 'one', :github => 'john' + """ + When I rebuild the Gemfile for "local" mode + Then the file "Gemfile" should have the following content: + """ + # generated by penchant, environment: local + gem "one", {:git=>"git://github.com/john/one.git"} + """ diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index 4017a56..27b9f30 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -121,6 +121,7 @@ module Penchant @available_environments = [] @defined_git_repos = [] @defaults = {} + @properties = {} @_current_env_defaults = {} end @@ -150,6 +151,10 @@ module Penchant end end + def property(name, &block) + @properties[name] = block + end + def for_environment?(envs) envs.include?(environment) || environment == ANY_ENVIRONMENT end @@ -192,13 +197,25 @@ module Penchant end def process_options(gem_name, template = {}) - Hash[ - _defaults_for(gem_name).dup.merge(template).collect { |key, value| + properties = {} + + property_stack = _defaults_for(gem_name).dup.merge(template).to_a + + while !property_stack.empty? + key, value = property_stack.shift + + if @properties[key] + @properties[key].call(*([ value ].flatten)).each do |k, v| + property_stack.push([ k, v ]) + end + else value = value % gem_name if value.respond_to?(:%) - [ key, value ] - }.sort - ] + properties[key] = value + end + end + + Hash[properties.sort] end def _defaults_for(gem_name)