From 5e1c05b85fffea915fb3d305ea8cf6f8d8a96f43 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 18 Aug 2011 13:09:49 -0400 Subject: [PATCH] more direct ingetration stuff --- bin/penchant | 10 ++++++-- lib/penchant/gemfile.rb | 32 +++++++++++++++++++++++++- spec/lib/penchant/gemfile_spec.rb | 38 ++++++++++++++++++++++++++++++- template/script/gemfile | 17 +++++--------- 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/bin/penchant b/bin/penchant index 30be87b..b6eecbe 100755 --- a/bin/penchant +++ b/bin/penchant @@ -2,8 +2,9 @@ require 'rubygems' require 'thor' +require 'penchant' -class Penchant < Thor +class PenchantCLI < Thor include Thor::Actions source_root File.expand_path('../..', __FILE__) @@ -13,6 +14,11 @@ class Penchant < Thor directory 'template/script', options[:dir] Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) } end + + desc "gemfile ENV", "Switch the gemfile environment" + def gemfile(env) + Penchant::Gemfile.do_full_env_switch!(env) + end end -Penchant.start +PenchantCLI.start diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index eaad68c..ec640fd 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -1,8 +1,23 @@ +require 'erb' + module Penchant class Gemfile attr_reader :path - def initialize(path) + class << self + def do_full_env_switch!(env) + gemfile = Penchant::Gemfile.new + if !gemfile.has_gemfile_erb? + puts "Not using Gemfile.erb, exiting." + return false + end + + gemfile.switch_to!(env) + system %{bundle} + end + end + + def initialize(path = Dir.pwd) @path = path end @@ -26,10 +41,25 @@ module Penchant File.readlines(gemfile_path).first.strip[%r{environment: (.*)}, 1] end + def switch_to!(gemfile_env) + @env = gemfile_env + template = File.read(gemfile_erb_path) + + File.open(gemfile_path, 'wb') do |fh| + fh.puts "# generated by penchant, environment: #{@env}" + + fh.print ERB.new(template).result(binding) + end + end + private def file_in_path(file) File.join(@path, file) end + + def env(check, &block) + instance_eval(&block) if check.to_s == @env.to_s + end end end diff --git a/spec/lib/penchant/gemfile_spec.rb b/spec/lib/penchant/gemfile_spec.rb index c3bf923..1fcf153 100644 --- a/spec/lib/penchant/gemfile_spec.rb +++ b/spec/lib/penchant/gemfile_spec.rb @@ -49,15 +49,51 @@ GEMFILE its(:environment) { should == environment } end end + + describe '#switch_to!' do + it 'should raise an exception' do + expect { subject.switch_to!(:whatever) }.to raise_error(Errno::ENOENT) + end + end end context 'with gemfile.erb' do + let(:erb_data) { 'whatever' } + before do - write_file(gemfile_erb_path) { "whatever" } + write_file(gemfile_erb_path) { erb_data } end it { should_not have_gemfile } it { should have_gemfile_erb } + + describe '#switch_to!' do + let(:erb_data) { <<-ERB } +<% env :test do %> + test +<% end %> + +<% env :not do %> + not +<% end %> + +all +ERB + + it 'should render test data' do + subject.switch_to!(:test) + + File.read('Gemfile').should include('test') + File.read('Gemfile').should include('all') + end + + it 'should not render test data' do + subject.switch_to!(:not) + + File.read('Gemfile').should include('not') + File.read('Gemfile').should include('all') + end + end end end diff --git a/template/script/gemfile b/template/script/gemfile index a067d07..eb78755 100755 --- a/template/script/gemfile +++ b/template/script/gemfile @@ -1,16 +1,11 @@ #!/usr/bin/env ruby -if !File.file?('Gemfile.erb') - puts "Not using Gemfile.erb, exiting." +require 'rubygems' +require 'penchant' + +if Penchant::Gemfile.do_full_env_switch!(ARGV[0]) + puts "Gemfile switched to #{ARGV[0]}" +else exit 0 end -require 'erb' - -env = ARGV[0] - -File.open('Gemfile', 'w') { |fh| fh.print ERB.new(File.read('Gemfile.erb')).result(binding) } -system %{bundle} - -puts "Gemfile switched to #{env}" -