Permalink
Browse files

Refactor Kumade::Packager

This moves the logic of each of the packagers into their own classes,
introduces a PackagerList class that maintains all the various
packagers, and improves tests around the packager.
  • Loading branch information...
1 parent fe4be8b commit d854184744a70d1786e65a3a4bbd6429988b7f00 Gabe Berke-Williams and Josh Clayton committed with joshuaclayton Sep 16, 2011
@@ -21,7 +21,7 @@ Feature: Kumade executable
And the output should contain:
"""
==> Git repo is clean
- ==> Packaged assets with Jammit
+ ==> Packaged with Kumade::JammitPackager
run git push origin master
==> Pushed master -> origin
run git branch deploy
@@ -31,7 +31,7 @@ Feature: Kumade executable
run git checkout master && git branch -D deploy
==> Deployed to: pretend-staging
"""
- But the output should not contain "==> Packaged assets with More"
+ But the output should not contain "==> Packaged with Kumade::MorePackager"
Scenario: Default environment is staging
When I run kumade with "-p"
@@ -52,7 +52,7 @@ Feature: Kumade executable
Then the output should contain:
"""
==> Git repo is clean
- ==> Packaged assets with Jammit
+ ==> Packaged with Kumade::JammitPackager
run git push origin new_branch
==> Pushed new_branch -> origin
run git branch deploy
@@ -95,4 +95,5 @@ Feature: Kumade executable
end
"""
When I run kumade with "pretend-staging -p"
- Then the output should match /kumade:before_asset_compilation.*Packaged assets with Jammit/
+ Then the output should contain "kumade:before_asset_compilation"
+ And the output should contain "==> Packaged with Kumade::JammitPackager"
@@ -23,7 +23,7 @@ Feature: Kumade without jammit
end
"""
When I run kumade with "pretend-staging"
- Then the output should contain "Running kumade:before_asset_compilation task"
+ Then the output should contain "Running rake task: kumade:before_asset_compilation"
And the output should contain "Hi!"
Scenario: Don't run rake task in pretend mode
@@ -36,5 +36,5 @@ Feature: Kumade without jammit
end
"""
When I run kumade with "pretend-staging -p"
- Then the output should contain "Running kumade:before_asset_compilation task"
+ Then the output should contain "Running rake task: kumade:before_asset_compilation"
And the output should not contain "Hi!"
View
@@ -26,5 +26,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('cucumber', '~> 1.0.2')
s.add_development_dependency('aruba', '~> 0.4.3')
s.add_development_dependency('jammit', '~> 0.6.3')
+ s.add_development_dependency('less', '~> 2.0')
s.add_development_dependency('bourne')
end
View
@@ -8,6 +8,11 @@ module Kumade
autoload :Configuration, "kumade/configuration"
autoload :Heroku, "kumade/heroku"
autoload :Packager, "kumade/packager"
+ autoload :MorePackager, "kumade/packagers/more_packager"
+ autoload :JammitPackager, "kumade/packagers/jammit_packager"
+ autoload :NoopPackager, "kumade/packagers/noop_packager"
+ autoload :PackagerList, "kumade/packager_list"
+ autoload :RakeTaskRunner, "kumade/rake_task_runner"
def self.configuration
@@configuration ||= Configuration.new
View
@@ -32,7 +32,7 @@ def pre_deploy
end
def package_assets
- packager.run
+ @packager.run
end
def sync_github
View
@@ -1,94 +1,46 @@
module Kumade
class Packager < Base
- attr_reader :git
-
- def initialize(git)
+ def initialize(git, packager = Packager.available_packager)
super()
- @git = git
+ @packager = packager
+ @git = git
end
def run
- invoke_custom_task if custom_task?
- package_with_jammit if jammit_installed?
- package_with_more if more_installed?
+ precompile_assets
+ package
end
- def invoke_custom_task
- success "Running kumade:before_asset_compilation task"
- Rake::Task["kumade:before_asset_compilation"].invoke unless Kumade.configuration.pretending?
+ def self.available_packager
+ Kumade::PackagerList.new.first
end
- def custom_task?
- load("Rakefile") if File.exist?("Rakefile")
- Rake::Task.task_defined?("kumade:before_asset_compilation")
- end
+ private
- def package_with_jammit
- begin
- success_message = "Packaged assets with Jammit"
+ def precompile_assets
+ RakeTaskRunner.new("kumade:before_asset_compilation", self).invoke
+ end
- if Kumade.configuration.pretending?
- success(success_message)
- else
- Jammit.package!
+ def package
+ return success(success_message) if Kumade.configuration.pretending?
+ begin
+ @packager.package
+ if @git.dirty?
+ git_add_and_commit_all_assets_in(@packager.assets_path)
success(success_message)
- git_add_and_commit_all_assets_in(jammit_assets_path)
end
- rescue => jammit_error
- error("Error: #{jammit_error.class}: #{jammit_error.message}")
+ rescue => packager_exception
+ error("Error: #{packager_exception.class}: #{packager_exception.message}")
end
end
- def package_with_more
- success_message = "Packaged assets with More"
- if Kumade.configuration.pretending?
- success(success_message)
- else
- begin
- run "bundle exec rake more:generate"
- if git.dirty?
- success(success_message)
- git_add_and_commit_all_assets_in(more_assets_path)
- end
- rescue => more_error
- error("Error: #{more_error.class}: #{more_error.message}")
- end
- end
+ def success_message
+ "Packaged with #{@packager.name}"
end
def git_add_and_commit_all_assets_in(dir)
- git.add_and_commit_all_in(dir, Kumade::Heroku::DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
- end
-
- def jammit_assets_path
- File.join(Jammit::PUBLIC_ROOT, Jammit.package_path)
- end
-
- def more_assets_path
- File.join('public', ::Less::More.destination_path)
- end
-
- def jammit_installed?
- @jammit_installed ||=
- (defined?(Jammit) ||
- begin
- require 'jammit'
- true
- rescue LoadError
- false
- end)
- end
-
- def more_installed?
- @more_installed ||=
- (defined?(Less::More) ||
- begin
- require 'less/more'
- true
- rescue LoadError
- false
- end)
+ @git.add_and_commit_all_in(dir, Kumade::Heroku::DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
end
end
end
@@ -0,0 +1,29 @@
+module Kumade
+ class PackagerList
+ include Enumerable
+
+ PACKAGERS = [MorePackager, JammitPackager]
+
+ def initialize
+ @packagers = build_packagers_list
+ end
+
+ def each(&block)
+ @packagers.each(&block)
+ end
+
+ private
+
+ def build_packagers_list
+ if installed_packagers.any?
+ installed_packagers
+ else
+ [NoopPackager]
+ end
+ end
+
+ def installed_packagers
+ PACKAGERS.select(&:installed?)
+ end
+ end
+end
@@ -0,0 +1,20 @@
+begin
+ require "jammit"
+rescue LoadError
@mike-burns

mike-burns Sep 23, 2011

Owner

Shouldn't the module code go inside the begin, since the module code references Jammit?

@gabebw

gabebw Sep 23, 2011

Contributor

Packagers only run if packager.installed? is true, so that's effectively a guard over the entire class.

+end
+
+module Kumade
+ class JammitPackager
+ def self.assets_path
+ File.join(Jammit::PUBLIC_ROOT, Jammit.package_path)
+ end
+
+ def self.installed?
+ !!defined?(Jammit)
+ end
+
+ def self.package
+ Jammit.package!
+ end
+ end
+end
@@ -0,0 +1,20 @@
+begin
+ require "less/more"
+rescue LoadError
+end
+
+module Kumade
+ class MorePackager
+ def self.assets_path
+ File.join("public", ::Less::More.destination_path)
+ end
+
+ def self.installed?
+ !!defined?(Less::More)
+ end
+
+ def self.package
+ ::Less::More.generate_all
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module Kumade
+ class NoopPackager
+ def self.assets_path
+ ""
+ end
+
+ def self.package
+ end
+
+ def self.installed?
+ false
+ end
+ end
+end
@@ -0,0 +1,30 @@
+module Kumade
+ class RakeTaskRunner
+ def initialize(task_name, runner)
+ @task_name = task_name
+ @runner = runner
+ end
+
+ def invoke
+ return unless task_defined?
+
+ @runner.success("Running rake task: #{@task_name}")
+ Rake::Task[@task_name].invoke if task_should_be_run?
+ end
+
+ private
+
+ def task_defined?
+ load_rakefile
+ Rake::Task.task_defined?(@task_name)
+ end
+
+ def task_should_be_run?
+ !Kumade.configuration.pretending?
+ end
+
+ def load_rakefile
+ load("Rakefile") if File.exist?("Rakefile")
+ end
+ end
+end
@@ -1,7 +1,5 @@
require 'spec_helper'
-require 'jammit'
-
describe Kumade::Deployer, "#pre_deploy" do
let(:git) { subject.git }
@@ -114,3 +112,18 @@
end
end
end
+
+describe Kumade::Deployer, "packaging" do
+ let(:git) { stub("git", :current_branch => "awesome", :delete => true) }
+ let(:packager) { stub("packager", :run => true) }
+
+ before do
+ Kumade::Git.stubs(:new => git)
+ Kumade::Packager.stubs(:new => packager)
+ end
+
+ it "builds the correct packager" do
+ subject.deploy
+ Kumade::Packager.should have_received(:new).with(git)
+ end
+end
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Kumade::PackagerList, "detecting packages" do
+ it "returns an array containing the Jammit packager if Jammit is installed" do
+ Kumade::JammitPackager.stubs(:installed? => true)
+ Kumade::MorePackager.stubs(:installed? => false)
+
+ Kumade::PackagerList.new.to_a.should == [Kumade::JammitPackager]
+ end
+
+ it "returns an array containing the More packager if More is installed" do
+ Kumade::JammitPackager.stubs(:installed? => false)
+ Kumade::MorePackager.stubs(:installed? => true)
+
+ Kumade::PackagerList.new.to_a.should == [Kumade::MorePackager]
+ end
+
+ it "returns multiple packagers if they are installed" do
+ Kumade::JammitPackager.stubs(:installed? => true)
+ Kumade::MorePackager.stubs(:installed? => true)
+
+ Kumade::PackagerList.new.to_a.should =~ [Kumade::JammitPackager, Kumade::MorePackager]
+ end
+
+ it "returns an array containing the no-op packager if no other packagers are found" do
+ Kumade::JammitPackager.stubs(:installed? => false)
+ Kumade::MorePackager.stubs(:installed? => false)
+
+ Kumade::PackagerList.new.to_a.should == [Kumade::NoopPackager]
+ end
+end
Oops, something went wrong.

1 comment on commit d854184

Contributor

tapajos commented on d854184 Sep 19, 2011

Great!

Please sign in to comment.