This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 d854184
Showing
21 changed files
with
465 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ def pre_deploy | |
end | ||
|
||
def package_assets | ||
packager.run | ||
@packager.run | ||
end | ||
|
||
def sync_github | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
begin | ||
require "jammit" | ||
rescue LoadError | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gabebw
Contributor
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Kumade | ||
class NoopPackager | ||
def self.assets_path | ||
"" | ||
end | ||
|
||
def self.package | ||
end | ||
|
||
def self.installed? | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
Shouldn't the module code go inside the
begin
, since the module code referencesJammit
?