From f4cc4ec0a7a035d82b3d04c52d08dce4bcdae748 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 7 Jun 2012 10:48:27 -0400 Subject: [PATCH] wow, even cooler\! --- bin/penchant | 16 +++++++ features/ruby_gemfile.feature | 27 +++++++++++ ...i_should_get_the_following_environments.rb | 3 ++ ...i_should_get_the_following_repositories.rb | 4 ++ ...uest_the_list_of_environments_available.rb | 3 ++ .../i_request_the_list_of_git_repositories.rb | 3 ++ lib/penchant.rb | 1 + lib/penchant/gemfile.rb | 45 ++++++++++++++++--- lib/penchant/repo.rb | 16 +++++++ spec/lib/penchant/gemfile_spec.rb | 2 +- 10 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 features/step_definitions/then/i_should_get_the_following_environments.rb create mode 100644 features/step_definitions/then/i_should_get_the_following_repositories.rb create mode 100644 features/step_definitions/when/i_request_the_list_of_environments_available.rb create mode 100644 features/step_definitions/when/i_request_the_list_of_git_repositories.rb create mode 100644 lib/penchant/repo.rb diff --git a/bin/penchant b/bin/penchant index 8861946..6312df4 100755 --- a/bin/penchant +++ b/bin/penchant @@ -63,6 +63,22 @@ class PenchantCLI < Thor puts get_current_env 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 def get_current_env gemfile = Penchant::Gemfile.new diff --git a/features/ruby_gemfile.feature b/features/ruby_gemfile.feature index f65afe7..a41b0bd 100644 --- a/features/ruby_gemfile.feature +++ b/features/ruby_gemfile.feature @@ -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 | + diff --git a/features/step_definitions/then/i_should_get_the_following_environments.rb b/features/step_definitions/then/i_should_get_the_following_environments.rb new file mode 100644 index 0000000..8d14b63 --- /dev/null +++ b/features/step_definitions/then/i_should_get_the_following_environments.rb @@ -0,0 +1,3 @@ +Then /^I should get the following environments:$/ do |table| + @environments.collect(&:to_s).sort.should == table.raw.flatten.sort +end diff --git a/features/step_definitions/then/i_should_get_the_following_repositories.rb b/features/step_definitions/then/i_should_get_the_following_repositories.rb new file mode 100644 index 0000000..98cbfeb --- /dev/null +++ b/features/step_definitions/then/i_should_get_the_following_repositories.rb @@ -0,0 +1,4 @@ +Then /^I should get the following repositories:$/ do |table| + @repos.collect(&:to_s).sort.should == table.raw.flatten.sort +end + diff --git a/features/step_definitions/when/i_request_the_list_of_environments_available.rb b/features/step_definitions/when/i_request_the_list_of_environments_available.rb new file mode 100644 index 0000000..70db03b --- /dev/null +++ b/features/step_definitions/when/i_request_the_list_of_environments_available.rb @@ -0,0 +1,3 @@ +When /^I request the list of environments available$/ do + @environments = Penchant::Gemfile.available_environments +end diff --git a/features/step_definitions/when/i_request_the_list_of_git_repositories.rb b/features/step_definitions/when/i_request_the_list_of_git_repositories.rb new file mode 100644 index 0000000..4da6fab --- /dev/null +++ b/features/step_definitions/when/i_request_the_list_of_git_repositories.rb @@ -0,0 +1,3 @@ +When /^I request the list of git repositories$/ do + @repos = Penchant::Gemfile.defined_git_repos +end diff --git a/lib/penchant.rb b/lib/penchant.rb index 11dd46c..54f57e9 100644 --- a/lib/penchant.rb +++ b/lib/penchant.rb @@ -1,4 +1,5 @@ module Penchant autoload :Gemfile, 'penchant/gemfile' + autoload :Repo, 'penchant/repo' autoload :DotPenchant, 'penchant/dot_penchant' end diff --git a/lib/penchant/gemfile.rb b/lib/penchant/gemfile.rb index b8b0f94..7082c51 100644 --- a/lib/penchant/gemfile.rb +++ b/lib/penchant/gemfile.rb @@ -17,17 +17,26 @@ module Penchant end def self.pre_switch(env, deployment = false) - gemfile = Penchant::Gemfile.new + gemfile = new return false if !gemfile.has_processable_gemfile? gemfile.run_dot_penchant!(env, deployment) gemfile 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 initialize(path = Dir.pwd) @path = path + @env = environment end def gemfile_path @@ -75,7 +84,7 @@ module Penchant end class FileProcessor - attr_reader :environment, :is_deployment + attr_reader :environment, :is_deployment, :available_environments, :defined_git_repos def self.result(data, *args) new(data).result(*args) @@ -91,6 +100,8 @@ module Penchant def initialize(data) @data = data + @available_environments = [] + @defined_git_repos = [] end def result(_env, _is_deployment) @@ -105,6 +116,8 @@ module Penchant end def env(*args) + @available_environments += args + yield if args.include?(environment) end @@ -220,6 +233,10 @@ module Penchant args = [ gem_name.first ] args << options if !options.empty? + if options[:git] + @defined_git_repos << Penchant::Repo.new(options[:git]) + end + @output << %{gem #{args_to_string(args)}} end @@ -254,10 +271,20 @@ module Penchant 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) @env, @is_deployment = gemfile_env, deployment - output = [ header, process(template) ] + output = [ header, process ] File.open(gemfile_path, 'wb') { |fh| fh.print output.join("\n") } end @@ -289,15 +316,21 @@ module Penchant File.join(@path, file) end - def process(template) - builder = case File.extname(processable_gemfile_path) + def process + builder.result(@env, @is_deployment) + end + + def builder + return @builder if @builder + + klass = case File.extname(processable_gemfile_path) when '.penchant' PenchantFile when '.erb' ERBFile end - builder.result(template, @env, @is_deployment) + @builder = klass.new(template) end def template diff --git a/lib/penchant/repo.rb b/lib/penchant/repo.rb new file mode 100644 index 0000000..934f638 --- /dev/null +++ b/lib/penchant/repo.rb @@ -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 + diff --git a/spec/lib/penchant/gemfile_spec.rb b/spec/lib/penchant/gemfile_spec.rb index fe2bb87..381985e 100644 --- a/spec/lib/penchant/gemfile_spec.rb +++ b/spec/lib/penchant/gemfile_spec.rb @@ -150,7 +150,7 @@ ERB end describe '#switch_to!' do - let(:template) { 'template' } + let(:template) { 'source' } let(:gemfile_path) { 'gemfile path' } let(:header) { 'header' }