From 98239ee141f2589af089bf76825f54bfb0b5567a Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 20 Jun 2012 13:25:43 -0400 Subject: [PATCH] defaults for things in environments --- features/ruby_gemfile.feature | 16 +++++++++++++++- lib/penchant/gemfile.rb | 31 +++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/features/ruby_gemfile.feature b/features/ruby_gemfile.feature index 74579b2..fe6ba85 100644 --- a/features/ruby_gemfile.feature +++ b/features/ruby_gemfile.feature @@ -195,7 +195,6 @@ Feature: Gemfiles gem "one", "1.2.3", {:path=>"../one"} """ - @wip Scenario: Propose defaults for an array of gems Given I have the file "Gemfile.penchant" with the content: """ @@ -208,3 +207,18 @@ Feature: Gemfiles # generated by penchant, environment: local gem "one", "1.2.3", {:path=>"../one"} """ + + Scenario: Propose defaults for an environment + Given I have the file "Gemfile.penchant" with the content: + """ + defaults_for env(:local), :path => '../%s' + env :local do + gem 'one', '1.2.3' + end + """ + When I rebuild the Gemfile for "local" mode + Then the file "Gemfile" should have the following content: + """ + # generated by penchant, environment: local + gem "one", "1.2.3", {:path=>"../one"} + """ diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index e53fb6a..55ff56e 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -83,6 +83,22 @@ module Penchant gemfile_header['deployment mode'] != nil end + class Env + attr_accessor :name + + def initialize(name) + @name = name.to_s + end + + def ==(other) + @name == other.name + end + + def to_s + "@#{name}" + end + end + class FileProcessor attr_reader :environment, :is_deployment, :available_environments, :defined_git_repos @@ -103,6 +119,8 @@ module Penchant @available_environments = [] @defined_git_repos = [] @defaults = {} + + @_current_env_defaults = {} end def result(_env, _is_deployment) @@ -119,7 +137,15 @@ module Penchant def env(*args) @available_environments += args - yield if args.include?(environment) + if block_given? + if args.include?(environment) + @_current_env_defaults = _defaults_for(Env.new(environment)) + yield + @_current_env_defaults = {} + end + else + Penchant::Gemfile::Env.new(args.shift) + end end def no_deployment @@ -170,7 +196,8 @@ module Penchant end def _defaults_for(gem_name) - @defaults[gem_name.to_s] || {} + result = @_current_env_defaults + result.merge(@defaults[gem_name.to_s] || {}) end def current_os