Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Refactor Kumade::Packager
Browse files Browse the repository at this point in the history
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
Gabe Berke-Williams and Josh Clayton authored and joshuaclayton committed Sep 16, 2011
1 parent fe4be8b commit d854184
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 355 deletions.
9 changes: 5 additions & 4 deletions features/kumade_executable.feature
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
4 changes: 2 additions & 2 deletions features/kumade_without_jammit.feature
Expand Up @@ -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
Expand All @@ -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!"
1 change: 1 addition & 0 deletions kumade.gemspec
Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/kumade.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/kumade/deployer.rb
Expand Up @@ -32,7 +32,7 @@ def pre_deploy
end

def package_assets
packager.run
@packager.run
end

def sync_github
Expand Down
92 changes: 22 additions & 70 deletions lib/kumade/packager.rb
@@ -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
29 changes: 29 additions & 0 deletions lib/kumade/packager_list.rb
@@ -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
20 changes: 20 additions & 0 deletions lib/kumade/packagers/jammit_packager.rb
@@ -0,0 +1,20 @@
begin
require "jammit"
rescue LoadError

This comment has been minimized.

Copy link
@mike-burns

mike-burns Sep 23, 2011

Member

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

This comment has been minimized.

Copy link
@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
20 changes: 20 additions & 0 deletions lib/kumade/packagers/more_packager.rb
@@ -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
14 changes: 14 additions & 0 deletions lib/kumade/packagers/noop_packager.rb
@@ -0,0 +1,14 @@
module Kumade
class NoopPackager
def self.assets_path
""
end

def self.package
end

def self.installed?
false
end
end
end
30 changes: 30 additions & 0 deletions lib/kumade/rake_task_runner.rb
@@ -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
17 changes: 15 additions & 2 deletions spec/kumade/deployer_spec.rb
@@ -1,7 +1,5 @@
require 'spec_helper'

require 'jammit'

describe Kumade::Deployer, "#pre_deploy" do
let(:git) { subject.git }

Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions spec/kumade/packager_list_spec.rb
@@ -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

1 comment on commit d854184

@tapajos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Please sign in to comment.