From c858dcdd69b6721c52ffff86f8fd7b8019a1d272 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 4 Jun 2012 15:39:22 -0400 Subject: [PATCH] a new way to manage lists of gems with commoon things --- .gitignore | 1 + Gemfile | 1 + Guardfile | 11 ++++-- config/cucumber.yml | 8 ++++ features/gemfile.feature | 38 +++++++++++++++++++ .../i_rebuild_the_gemfile_for_local_mode.rb | 4 ++ features/support/cuke-pack.rb | 20 ++++++++++ lib/penchant/gemfile.rb | 34 +++++++++++++++-- 8 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 config/cucumber.yml create mode 100644 features/step_definitions/when/i_rebuild_the_gemfile_for_local_mode.rb create mode 100644 features/support/cuke-pack.rb diff --git a/.gitignore b/.gitignore index 4040c6c..2f0a9ac 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .bundle Gemfile.lock pkg/* +.DS_Store diff --git a/Gemfile b/Gemfile index a71c8ab..b0da8eb 100644 --- a/Gemfile +++ b/Gemfile @@ -13,3 +13,4 @@ gem 'rspec', '~> 2.6.0' gem 'rake' gem 'cucumber' +gem 'cuke-pack', :path => '../cuke-pack' diff --git a/Guardfile b/Guardfile index fbb8889..370138a 100644 --- a/Guardfile +++ b/Guardfile @@ -9,10 +9,13 @@ group :rspec do end end -group :cucumber do - guard 'cucumber' do - watch(%r{^features/.+\.feature$}) +# added by cuke-pack + +group :wip do + guard 'cucumber', :env => :cucumber, :cli => '-p wip' do + watch(%r{^features/.+.feature$}) + watch(%r{^(app|lib).*}) { 'features' } watch(%r{^features/support/.+$}) { 'features' } - watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } + watch(%r{^features/step_definitions/(.+).rb$}) { 'features' } end end diff --git a/config/cucumber.yml b/config/cucumber.yml new file mode 100644 index 0000000..cd59005 --- /dev/null +++ b/config/cucumber.yml @@ -0,0 +1,8 @@ +<% +std_opts = "-r features --format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} -f Cucumber::StepWriter --out features/step_definitions --strict" +%> +default: <%= std_opts %> features +wip: <%= std_opts %> --tags @wip features +precommit: FAILFAST=true <%= std_opts %> --tags ~@wip:0 features +cleanup: <%= std_opts %> -f Cucumber::CleanupFormatter --out unused.txt features + diff --git a/features/gemfile.feature b/features/gemfile.feature index 0886c07..980442d 100644 --- a/features/gemfile.feature +++ b/features/gemfile.feature @@ -31,3 +31,41 @@ Feature: Gemfiles # generated by penchant, environment: local this is content """ + + Scenario: Use placeholder expansion + Given I have the file "Gemfile.erb" with the content: + """ + <% env :local, :path => '../%s' do %> + gem 'test' + <% end %> + """ + When I rebuild the Gemfile for "local" mode + Then the file "Gemfile" should have the following content: + """ + # generated by penchant, environment: local + + gem 'test', :path => %{../test} + + """ + + Scenario: Use a gem list for an operation + Given I have the file "Gemfile.erb" with the content: + """ + <% with_gem_list 'test' do %> + <% env :local, :path => '../%s' do %> + <%= gem %> + <% end %> + <% end %> + """ + When I rebuild the Gemfile for "local" mode + Then the file "Gemfile" should have the following content: + """ + # generated by penchant, environment: local + + + + gem 'test', :path => %{../test} + + + """ + diff --git a/features/step_definitions/when/i_rebuild_the_gemfile_for_local_mode.rb b/features/step_definitions/when/i_rebuild_the_gemfile_for_local_mode.rb new file mode 100644 index 0000000..b2ff0ef --- /dev/null +++ b/features/step_definitions/when/i_rebuild_the_gemfile_for_local_mode.rb @@ -0,0 +1,4 @@ +When /^I rebuild the Gemfile for "(.*?)" mode$/ do |env| + Penchant::Gemfile.do_full_env_switch!(env) +end + diff --git a/features/support/cuke-pack.rb b/features/support/cuke-pack.rb new file mode 100644 index 0000000..b9d9998 --- /dev/null +++ b/features/support/cuke-pack.rb @@ -0,0 +1,20 @@ +require 'cuke-pack/support/pause' +require 'cuke-pack/support/pending' + +Before do + # if you want pending steps to pause before marking the step as pending, + # set @pause_ok to true + + @pause_ok = false +end + +require 'cuke-pack/support/step_writer' +require 'cuke-pack/support/wait_for' +require 'cuke-pack/support/failfast' + +# set the level of flaying on the step definitions +# set it to false to skip flaying +flay_level = 32 + +require 'cuke-pack/support/flay' + diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index c11aeee..674812e 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -61,7 +61,7 @@ module Penchant def switch_to!(gemfile_env = nil, deployment = false) @env, @is_deployment = gemfile_env, deployment - output = [ header, ERB.new(template).result(binding) ] + output = [ header, ERB.new(template, nil, nil, '@_erbout').result(binding) ] File.open(gemfile_path, 'wb') { |fh| fh.print output.join("\n") } end @@ -97,8 +97,36 @@ module Penchant File.read(gemfile_erb_path) end - def env(check, &block) - instance_eval(&block) if check.to_s == @env.to_s + def env(check, template = {}, &block) + if check.to_s == @env.to_s + original_erbout = @_erbout.dup + + output = instance_eval(&block).lines.to_a + + output.each do |line| + if gem_name = line[%r{gem ['"]([^'"]+)['"]}, 1] + new_line = line.rstrip + template.each do |key, value| + new_line += ", #{key.inspect} => %{#{value % gem_name}}" + end + new_line += "\n" + line.replace(new_line) + end + end + + @_erbout = original_erbout + output.join + end + end + + def with_gem_list(*gems) + gems.each do |gem| + @_current_gem = gem + yield + end + end + + def gem + "gem '#{@_current_gem}'" end def no_deployment(&block)