more direct ingetration stuff

This commit is contained in:
John Bintz 2011-08-18 13:09:49 -04:00
parent 8fac9351ce
commit 5e1c05b85f
4 changed files with 82 additions and 15 deletions

View File

@ -2,8 +2,9 @@
require 'rubygems' require 'rubygems'
require 'thor' require 'thor'
require 'penchant'
class Penchant < Thor class PenchantCLI < Thor
include Thor::Actions include Thor::Actions
source_root File.expand_path('../..', __FILE__) source_root File.expand_path('../..', __FILE__)
@ -13,6 +14,11 @@ class Penchant < Thor
directory 'template/script', options[:dir] directory 'template/script', options[:dir]
Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) } Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) }
end end
desc "gemfile ENV", "Switch the gemfile environment"
def gemfile(env)
Penchant::Gemfile.do_full_env_switch!(env)
end
end end
Penchant.start PenchantCLI.start

View File

@ -1,8 +1,23 @@
require 'erb'
module Penchant module Penchant
class Gemfile class Gemfile
attr_reader :path 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 @path = path
end end
@ -26,10 +41,25 @@ module Penchant
File.readlines(gemfile_path).first.strip[%r{environment: (.*)}, 1] File.readlines(gemfile_path).first.strip[%r{environment: (.*)}, 1]
end 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 private
def file_in_path(file) def file_in_path(file)
File.join(@path, file) File.join(@path, file)
end end
def env(check, &block)
instance_eval(&block) if check.to_s == @env.to_s
end
end end
end end

View File

@ -49,15 +49,51 @@ GEMFILE
its(:environment) { should == environment } its(:environment) { should == environment }
end end
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 end
context 'with gemfile.erb' do context 'with gemfile.erb' do
let(:erb_data) { 'whatever' }
before do before do
write_file(gemfile_erb_path) { "whatever" } write_file(gemfile_erb_path) { erb_data }
end end
it { should_not have_gemfile } it { should_not have_gemfile }
it { should have_gemfile_erb } 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
end end

View File

@ -1,16 +1,11 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
if !File.file?('Gemfile.erb') require 'rubygems'
puts "Not using Gemfile.erb, exiting." require 'penchant'
if Penchant::Gemfile.do_full_env_switch!(ARGV[0])
puts "Gemfile switched to #{ARGV[0]}"
else
exit 0 exit 0
end 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}"