From 08803b76cedf796873fabf9846aeced3bc645979 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 20 Jun 2012 09:34:47 -0400 Subject: [PATCH] defaults_for --- README.md | 11 +++++++---- features/ruby_gemfile.feature | 26 ++++++++++++++++++++++++++ lib/penchant/gemfile.rb | 15 +++++++++++++-- lib/penchant/version.rb | 2 +- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ef6e8f2..00bfd57 100644 --- a/README.md +++ b/README.md @@ -40,19 +40,22 @@ no_deployment do dev_gems = %w{flowerbox guard-flowerbox} + # set up defaults for certain gems that are probably being used in envs + defaults_for dev_gems, :require => nil + env :local do # expands to: # - # gem 'flowerbox', :path => '../flowerbox' - # gem 'guard-flowerbox', :path => '../guard-flowerbox' + # gem 'flowerbox', :path => '../flowerbox', :require => nil + # gem 'guard-flowerbox', :path => '../guard-flowerbox', :require => nil gems dev_gems, :path => '../%s' end env :remote do # expands to: # - # gem 'flowerbox', :git => 'git://github.com/johnbintz/flowerbox.git' - # gem 'guard-flowerbox', :git => 'git://github.com/johnbintz/guard-flowerbox.git' + # gem 'flowerbox', :git => 'git://github.com/johnbintz/flowerbox.git', :require => nil + # gem 'guard-flowerbox', :git => 'git://github.com/johnbintz/guard-flowerbox.git', :require => nil gems dev_gems, :git => 'git://github.com/johnbintz/%s.git' end diff --git a/features/ruby_gemfile.feature b/features/ruby_gemfile.feature index a840fca..74579b2 100644 --- a/features/ruby_gemfile.feature +++ b/features/ruby_gemfile.feature @@ -182,3 +182,29 @@ Feature: Gemfiles Then I should get the following repositories: | git://github.cats/two.git | + Scenario: Propose defaults for a gem + Given I have the file "Gemfile.penchant" with the content: + """ + defaults_for 'one', :path => '../%s' + gem 'one', '1.2.3' + """ + 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"} + """ + + @wip + Scenario: Propose defaults for an array of gems + Given I have the file "Gemfile.penchant" with the content: + """ + defaults_for ['one'], :path => '../%s' + gem 'one', '1.2.3' + """ + 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 1f54de1..e53fb6a 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -102,6 +102,7 @@ module Penchant @data = data @available_environments = [] @defined_git_repos = [] + @defaults = {} end def result(_env, _is_deployment) @@ -129,6 +130,12 @@ module Penchant yield if args.include?(current_os) end + def defaults_for(*args) + defaults = args.pop + + args.flatten.each { |gem| @defaults[gem.to_s] = defaults.dup } + end + protected def args_to_string(args) args.inspect[1..-2] @@ -154,7 +161,7 @@ module Penchant def process_options(gem_name, template = {}) Hash[ - template.collect { |key, value| + template.merge(_defaults_for(gem_name)).collect { |key, value| value = value % gem_name if value.respond_to?(:%) [ key, value ] @@ -162,6 +169,10 @@ module Penchant ] end + def _defaults_for(gem_name) + @defaults[gem_name.to_s] || {} + end + def current_os require 'rbconfig' case host_os = RbConfig::CONFIG['host_os'] @@ -242,7 +253,7 @@ module Penchant version = args.first - options = process_options(gem_name, template) + options = process_options(gem_name.first, template) args = [ gem_name.first ] args << version if version diff --git a/lib/penchant/version.rb b/lib/penchant/version.rb index a46aa20..531f561 100644 --- a/lib/penchant/version.rb +++ b/lib/penchant/version.rb @@ -1,3 +1,3 @@ module Penchant - VERSION = "0.2.5" + VERSION = "0.2.6" end