wow, even cooler\!

This commit is contained in:
John Bintz 2012-06-07 10:48:27 -04:00
parent 7f2e0d6301
commit f4cc4ec0a7
10 changed files with 113 additions and 7 deletions

View File

@ -63,6 +63,22 @@ class PenchantCLI < Thor
puts get_current_env puts get_current_env
end end
desc "bootstrap [DIR = ..]", "Download all referred-to git repos to the specified directory"
def bootstrap(dir = '..')
Penchant::Gemfile.defined_git_repos.each do |repo|
puts "Cloning #{repo} to #{dir}."
repo.clone_to(dir)
end
end
def method_missing(method, *args)
if Penchant::Gemfile.available_environments.include?(method)
gemfile(method, *args)
else
super(method, *args)
end
end
no_tasks do no_tasks do
def get_current_env def get_current_env
gemfile = Penchant::Gemfile.new gemfile = Penchant::Gemfile.new

View File

@ -157,3 +157,30 @@ Feature: Gemfiles
""" """
Scenario: Get the list of environments defined
Given I have the file "Gemfile.penchant" with the content:
"""
env :cat do
gem 'one', :path => '../%s'
end
env :dog do
gem 'two', :path => '../%s'
end
"""
When I request the list of environments available
Then I should get the following environments:
| cat |
| dog |
@wip
Scenario: Get the list of git repos defined
Given I have the file "Gemfile.penchant" with the content:
"""
gem 'one', :path => '../%s'
gem 'two', :git => 'git://github.cats/%s.git'
"""
When I request the list of git repositories
Then I should get the following repositories:
| git://github.cats/two.git |

View File

@ -0,0 +1,3 @@
Then /^I should get the following environments:$/ do |table|
@environments.collect(&:to_s).sort.should == table.raw.flatten.sort
end

View File

@ -0,0 +1,4 @@
Then /^I should get the following repositories:$/ do |table|
@repos.collect(&:to_s).sort.should == table.raw.flatten.sort
end

View File

@ -0,0 +1,3 @@
When /^I request the list of environments available$/ do
@environments = Penchant::Gemfile.available_environments
end

View File

@ -0,0 +1,3 @@
When /^I request the list of git repositories$/ do
@repos = Penchant::Gemfile.defined_git_repos
end

View File

@ -1,4 +1,5 @@
module Penchant module Penchant
autoload :Gemfile, 'penchant/gemfile' autoload :Gemfile, 'penchant/gemfile'
autoload :Repo, 'penchant/repo'
autoload :DotPenchant, 'penchant/dot_penchant' autoload :DotPenchant, 'penchant/dot_penchant'
end end

View File

@ -17,17 +17,26 @@ module Penchant
end end
def self.pre_switch(env, deployment = false) def self.pre_switch(env, deployment = false)
gemfile = Penchant::Gemfile.new gemfile = new
return false if !gemfile.has_processable_gemfile? return false if !gemfile.has_processable_gemfile?
gemfile.run_dot_penchant!(env, deployment) gemfile.run_dot_penchant!(env, deployment)
gemfile gemfile
end end
def self.available_environments
new.available_environments
end
def self.defined_git_repos
new.defined_git_repos
end
def current_env ; @env ; end def current_env ; @env ; end
def initialize(path = Dir.pwd) def initialize(path = Dir.pwd)
@path = path @path = path
@env = environment
end end
def gemfile_path def gemfile_path
@ -75,7 +84,7 @@ module Penchant
end end
class FileProcessor class FileProcessor
attr_reader :environment, :is_deployment attr_reader :environment, :is_deployment, :available_environments, :defined_git_repos
def self.result(data, *args) def self.result(data, *args)
new(data).result(*args) new(data).result(*args)
@ -91,6 +100,8 @@ module Penchant
def initialize(data) def initialize(data)
@data = data @data = data
@available_environments = []
@defined_git_repos = []
end end
def result(_env, _is_deployment) def result(_env, _is_deployment)
@ -105,6 +116,8 @@ module Penchant
end end
def env(*args) def env(*args)
@available_environments += args
yield if args.include?(environment) yield if args.include?(environment)
end end
@ -220,6 +233,10 @@ module Penchant
args = [ gem_name.first ] args = [ gem_name.first ]
args << options if !options.empty? args << options if !options.empty?
if options[:git]
@defined_git_repos << Penchant::Repo.new(options[:git])
end
@output << %{gem #{args_to_string(args)}} @output << %{gem #{args_to_string(args)}}
end end
@ -254,10 +271,20 @@ module Penchant
end end
end end
def available_environments
process
builder.available_environments
end
def defined_git_repos
process
builder.defined_git_repos
end
def switch_to!(gemfile_env = nil, deployment = false) def switch_to!(gemfile_env = nil, deployment = false)
@env, @is_deployment = gemfile_env, deployment @env, @is_deployment = gemfile_env, deployment
output = [ header, process(template) ] output = [ header, process ]
File.open(gemfile_path, 'wb') { |fh| fh.print output.join("\n") } File.open(gemfile_path, 'wb') { |fh| fh.print output.join("\n") }
end end
@ -289,15 +316,21 @@ module Penchant
File.join(@path, file) File.join(@path, file)
end end
def process(template) def process
builder = case File.extname(processable_gemfile_path) builder.result(@env, @is_deployment)
end
def builder
return @builder if @builder
klass = case File.extname(processable_gemfile_path)
when '.penchant' when '.penchant'
PenchantFile PenchantFile
when '.erb' when '.erb'
ERBFile ERBFile
end end
builder.result(template, @env, @is_deployment) @builder = klass.new(template)
end end
def template def template

16
lib/penchant/repo.rb Normal file
View File

@ -0,0 +1,16 @@
module Penchant
class Repo
def initialize(url)
@url = url
end
def clone_to(dir)
Dir.chdir(dir) do
system %{git clone #{@url}}
end
end
def to_s ; @url ; end
end
end

View File

@ -150,7 +150,7 @@ ERB
end end
describe '#switch_to!' do describe '#switch_to!' do
let(:template) { 'template' } let(:template) { 'source' }
let(:gemfile_path) { 'gemfile path' } let(:gemfile_path) { 'gemfile path' }
let(:header) { 'header' } let(:header) { 'header' }