From c4407f1690cc8ade00022aab618b12c458a473d5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 31 Aug 2011 14:03:48 -0400 Subject: [PATCH 1/6] start work on dotfile hook suttport --- lib/penchant.rb | 1 + lib/penchant/dot_penchant.rb | 16 ++++++++++++++++ lib/penchant/gemfile.rb | 10 ++++++++++ spec/lib/penchant/dot_penchant_spec.rb | 20 ++++++++++++++++++++ spec/lib/penchant/gemfile_spec.rb | 16 ++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 lib/penchant/dot_penchant.rb create mode 100644 spec/lib/penchant/dot_penchant_spec.rb diff --git a/lib/penchant.rb b/lib/penchant.rb index 0dfcd5d..11dd46c 100644 --- a/lib/penchant.rb +++ b/lib/penchant.rb @@ -1,3 +1,4 @@ module Penchant autoload :Gemfile, 'penchant/gemfile' + autoload :DotPenchant, 'penchant/dot_penchant' end diff --git a/lib/penchant/dot_penchant.rb b/lib/penchant/dot_penchant.rb new file mode 100644 index 0000000..7bfbfbe --- /dev/null +++ b/lib/penchant/dot_penchant.rb @@ -0,0 +1,16 @@ +module Penchant + class DotPenchant + class << self + def run(env = nil) + dot_penchant = new + dot_penchant.run(env) + dot_penchant + end + end + + def run(env = nil) + instance_eval(File.read('.penchant')) + end + end +end + diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index d5e6067..1ddd347 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -27,6 +27,10 @@ module Penchant File.file?('Gemfile') end + def has_dot_penchant? + File.file?('.penchant') + end + def gemfile_erb_path file_in_path('Gemfile.erb') end @@ -48,6 +52,8 @@ module Penchant fh.print ERB.new(template).result(binding) end + + run_dot_penchant!(gemfile_env) if has_dot_penchant? end private @@ -58,6 +64,10 @@ module Penchant def env(check, &block) instance_eval(&block) if check.to_s == @env.to_s end + + def run_dot_penchant!(env) + DotPenchant.run(env) + end end end diff --git a/spec/lib/penchant/dot_penchant_spec.rb b/spec/lib/penchant/dot_penchant_spec.rb new file mode 100644 index 0000000..73bf0cc --- /dev/null +++ b/spec/lib/penchant/dot_penchant_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Penchant::DotPenchant do + include FakeFS::SpecHelpers + + describe '.run' do + before do + File.open('.penchant', 'wb') { |fh| + fh.puts "@did_run = env" + } + end + + it 'should run the file in the environment' do + dot_file = Penchant::DotPenchant.run(:this) + + dot_file.instance_variable_get(:@did_run).should == :this + end + end +end + diff --git a/spec/lib/penchant/gemfile_spec.rb b/spec/lib/penchant/gemfile_spec.rb index cc22c51..356581e 100644 --- a/spec/lib/penchant/gemfile_spec.rb +++ b/spec/lib/penchant/gemfile_spec.rb @@ -103,6 +103,22 @@ ERB File.read('Gemfile').should_not include('not') File.read('Gemfile').should include('all') end + + it { should_not have_dot_penchant } + + context 'with .penchant' do + before do + File.open('.penchant', 'wb') + end + + it { should have_dot_penchant } + + it 'should process the file' do + subject.expects(:run_dot_penchant!).with(:not) + + subject.switch_to!(:not) + end + end end end end From b4e79b5daa16c1a25ea111b167487a0ba2001f14 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 31 Aug 2011 14:11:05 -0400 Subject: [PATCH 2/6] rake support --- lib/penchant/dot_penchant.rb | 12 ++++++++++++ spec/lib/penchant/dot_penchant_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/penchant/dot_penchant.rb b/lib/penchant/dot_penchant.rb index 7bfbfbe..7174201 100644 --- a/lib/penchant/dot_penchant.rb +++ b/lib/penchant/dot_penchant.rb @@ -11,6 +11,18 @@ module Penchant def run(env = nil) instance_eval(File.read('.penchant')) end + + def rake(*tasks) + command = [ "rake", *tasks ] + command.unshift("bundle exec") if gemfile? + Kernel.system command.join(' ') + end + + private + def gemfile? + File.file?('Gemfile') + end + end end diff --git a/spec/lib/penchant/dot_penchant_spec.rb b/spec/lib/penchant/dot_penchant_spec.rb index 73bf0cc..8be4d9e 100644 --- a/spec/lib/penchant/dot_penchant_spec.rb +++ b/spec/lib/penchant/dot_penchant_spec.rb @@ -16,5 +16,30 @@ describe Penchant::DotPenchant do dot_file.instance_variable_get(:@did_run).should == :this end end + + let(:dot_file) { described_class.new } + + describe '#rake' do + context 'without Gemfile' do + before do + Kernel.expects(:system).with('rake task1 task2') + end + + it 'should run the rake task via system' do + dot_file.rake("task1", "task2") + end + end + + context 'with Gemfile' do + before do + File.open('Gemfile', 'wb') + Kernel.expects(:system).with('bundle exec rake task1 task2') + end + + it 'should run the rake task via system' do + dot_file.rake("task1", "task2") + end + end + end end From f8e5c9fcf7448a4feee71526bb8c34668e828e6e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 31 Aug 2011 14:43:43 -0400 Subject: [PATCH 3/6] update docs for dotfile --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 410a1bd..d7eee43 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,17 @@ It then runs `bundle install`. You can also run `penchant gemfile ENV`. +### After-`gemfile` hooks? + +Drop a file called `.penchant` in your project directory. It'll get executed every time you switch environments using +Penchant. I use it to tell my Hydra clients to sync and update their Gemfiles, too: + +``` ruby +# rake knows if you need "bundle exec" or not. + +rake "hydra:sync hydra:remote:bundle" +``` + ### What environment are you currently using in that Gemfile? `head -n 1` that puppy, or `penchant gemfile-env`. From 380129984794b824af14492e19e7c81a73afaa90 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 31 Aug 2011 21:39:50 -0400 Subject: [PATCH 4/6] change how dotfiles are handled --- bin/penchant | 2 +- lib/penchant/gemfile.rb | 4 ++-- spec/lib/penchant/gemfile_spec.rb | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/penchant b/bin/penchant index 309735f..e8879c1 100755 --- a/bin/penchant +++ b/bin/penchant @@ -28,7 +28,7 @@ class PenchantCLI < Thor def gemfile(env = get_current_env) if env puts "[penchant] Rebunding for #{env} environment..." - !Penchant::Gemfile.do_full_env_switch!(env) + Penchant::Gemfile.do_full_env_switch!(env) end gemfile = Penchant::Gemfile.new diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index 1ddd347..b84cc26 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -7,6 +7,8 @@ module Penchant class << self def do_full_env_switch!(env) gemfile = Penchant::Gemfile.new + run_dot_penchant!(gemfile_env) if has_dot_penchant? + if !gemfile.has_gemfile_erb? return false end @@ -52,8 +54,6 @@ module Penchant fh.print ERB.new(template).result(binding) end - - run_dot_penchant!(gemfile_env) if has_dot_penchant? end private diff --git a/spec/lib/penchant/gemfile_spec.rb b/spec/lib/penchant/gemfile_spec.rb index 356581e..6b4d27b 100644 --- a/spec/lib/penchant/gemfile_spec.rb +++ b/spec/lib/penchant/gemfile_spec.rb @@ -114,8 +114,6 @@ ERB it { should have_dot_penchant } it 'should process the file' do - subject.expects(:run_dot_penchant!).with(:not) - subject.switch_to!(:not) end end From 459a391260e3aab5a4a4b459d199a04ab2000afa Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 31 Aug 2011 21:40:51 -0400 Subject: [PATCH 5/6] fix --- lib/penchant/gemfile.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index b84cc26..4f1f5bf 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -7,7 +7,7 @@ module Penchant class << self def do_full_env_switch!(env) gemfile = Penchant::Gemfile.new - run_dot_penchant!(gemfile_env) if has_dot_penchant? + gemfile.run_dot_penchant!(gemfile_env) if gemfile.has_dot_penchant? if !gemfile.has_gemfile_erb? return false @@ -56,6 +56,10 @@ module Penchant end end + def run_dot_penchant!(env) + DotPenchant.run(env) + end + private def file_in_path(file) File.join(@path, file) @@ -64,10 +68,6 @@ module Penchant def env(check, &block) instance_eval(&block) if check.to_s == @env.to_s end - - def run_dot_penchant!(env) - DotPenchant.run(env) - end end end From c5b64809963ac3291d75cd2bcaba8172e201ad05 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 6 Sep 2011 15:36:20 -0400 Subject: [PATCH 6/6] fix a thing --- lib/penchant/gemfile.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index 4f1f5bf..502151a 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -7,7 +7,7 @@ module Penchant class << self def do_full_env_switch!(env) gemfile = Penchant::Gemfile.new - gemfile.run_dot_penchant!(gemfile_env) if gemfile.has_dot_penchant? + gemfile.run_dot_penchant!(env) if !gemfile.has_gemfile_erb? return false @@ -57,7 +57,7 @@ module Penchant end def run_dot_penchant!(env) - DotPenchant.run(env) + DotPenchant.run(env || environment) if has_dot_penchant? end private