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

Commit

Permalink
WIP: actually use Outputter, and remove Base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Oct 14, 2011
1 parent 92a4add commit 6d7c867
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 145 deletions.
10 changes: 9 additions & 1 deletion lib/kumade.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Kumade
autoload :Base, "kumade/base"
autoload :Git, "kumade/git"
autoload :Deployer, "kumade/deployer"
autoload :CLI, "kumade/cli"
Expand All @@ -14,6 +13,7 @@ module Kumade
autoload :PackagerList, "kumade/packager_list"
autoload :RakeTaskRunner, "kumade/rake_task_runner"
autoload :CommandLine, "kumade/command_line"
autoload :Outputter, "kumade/outputter"

def self.configuration
@@configuration ||= Configuration.new
Expand All @@ -22,4 +22,12 @@ def self.configuration
def self.configuration=(new_configuration)
@@configuration = new_configuration
end

def self.outputter
@@outputter ||= Outputter.new
end

def self.outputter=(new_outputter)
@@outputter = new_outputter
end
end
18 changes: 0 additions & 18 deletions lib/kumade/base.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/kumade/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def self.swapping_stdout_for(io, print_output = false)

def deploy
if Kumade.configuration.pretending?
puts "==> In Pretend Mode"
Kumade.outputter.info("In Pretend Mode")
end
puts "==> Deploying to: #{Kumade.configuration.environment}"
Kumade.outputter.info("Deploying to: #{Kumade.configuration.environment}")
self.class.deployer.new.deploy
puts "==> Deployed to: #{Kumade.configuration.environment}"
Kumade.outputter.info("Deployed to: #{Kumade.configuration.environment}")
end

def parse_arguments!(args)
Expand Down
7 changes: 3 additions & 4 deletions lib/kumade/command_line.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
require 'cocaine'

module Kumade
class CommandLine < Base
class CommandLine
def initialize(command_to_run)
super()
@command_line = Cocaine::CommandLine.new(command_to_run)
end

def run_or_error(error_message = nil)
run_with_status || error(error_message)
run_with_status || Kumade.outputter.error(error_message)
end

def run_with_status
say_status(:run, command)
Kumade.outputter.info(command)
Kumade.configuration.pretending? || run
end

Expand Down
9 changes: 4 additions & 5 deletions lib/kumade/deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
require 'cocaine'

module Kumade
class Deployer < Base
class Deployer
attr_reader :git, :heroku, :packager

def initialize
super()
@git = Git.new
@heroku = Heroku.new
@branch = @git.current_branch
Expand Down Expand Up @@ -50,12 +49,12 @@ def ensure_clean_git
def ensure_heroku_remote_exists
if git.remote_exists?(Kumade.configuration.environment)
if git.heroku_remote?
success("#{Kumade.configuration.environment} is a Heroku remote")
Kumade.outputter.success("#{Kumade.configuration.environment} is a Heroku remote")
else
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
Kumade.outputter.error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
end
else
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
Kumade.outputter.error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
end
end
end
Expand Down
20 changes: 8 additions & 12 deletions lib/kumade/git.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require 'cocaine'
module Kumade
class Git < Base
def initialize
super()
end

class Git
def heroku_remote?
remote_url = `git config --get remote.#{Kumade.configuration.environment}.url`.strip
!! remote_url.strip.match(/^git@heroku\..+:(.+)\.git$/)
Expand All @@ -23,27 +19,27 @@ def push(branch, remote = 'origin', force = false)

command_line = CommandLine.new(command)
command_line.run_or_error("Failed to push #{branch} -> #{remote}")
success("Pushed #{branch} -> #{remote}")
Kumade.outputter.success("Pushed #{branch} -> #{remote}")
end

def create(branch)
unless has_branch?(branch)
CommandLine.new("git branch #{branch}").run_or_error("Failed to create #{branch}")
CommandLine.new("git branch #{branch} >/dev/null").run_or_error("Failed to create #{branch}")
end
end

def delete(branch_to_delete, branch_to_checkout)
command_line = CommandLine.new("git checkout #{branch_to_checkout} && git branch -D #{branch_to_delete}")
command_line = CommandLine.new("git checkout #{branch_to_checkout} 2>/dev/null && git branch -D #{branch_to_delete}")
command_line.run_or_error("Failed to clean up #{branch_to_delete} branch")
end

def add_and_commit_all_assets_in(dir)
command = ["git checkout -b #{Kumade::Heroku::DEPLOY_BRANCH}",
command = ["git checkout -b #{Kumade::Heroku::DEPLOY_BRANCH} 2>/dev/null",
"git add -f #{dir}",
"git commit -m 'Compiled assets.'"].join(' && ')
command_line = CommandLine.new(command)
command_line.run_or_error("Cannot deploy: couldn't commit assets")
success("Added and committed all assets")
Kumade.outputter.success("Added and committed all assets")
end

def current_branch
Expand All @@ -64,9 +60,9 @@ def dirty?

def ensure_clean_git
if ! Kumade.configuration.pretending? && dirty?
error("Cannot deploy: repo is not clean.")
Kumade.outputter.error("Cannot deploy: repo is not clean.")
else
success("Git repo is clean")
Kumade.outputter.success("Git repo is clean")
end
end

Expand Down
5 changes: 2 additions & 3 deletions lib/kumade/heroku.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'cocaine'

module Kumade
class Heroku < Base
class Heroku
DEPLOY_BRANCH = "deploy"
attr_reader :git

def initialize
super()
@git = Git.new
@branch = @git.current_branch
end
Expand All @@ -18,7 +17,7 @@ def sync

def migrate_database
heroku("rake db:migrate") unless Kumade.configuration.pretending?
success("Migrated #{Kumade.configuration.environment}")
Kumade.outputter.success("Migrated #{Kumade.configuration.environment}")
end

def delete_deploy_branch
Expand Down
16 changes: 16 additions & 0 deletions lib/kumade/outputter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Kumade
class Outputter
def success(message)
puts "==> #{message}"
end

def info(message)
puts "==> #{message}"
end

def error(message)
puts "==> ! #{message}"
raise Kumade::DeploymentError, message
end
end
end
11 changes: 5 additions & 6 deletions lib/kumade/packager.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Kumade
class Packager < Base
class Packager
def initialize(git, packager = Packager.available_packager)
super()
@packager = packager
@git = git
end
Expand All @@ -18,20 +17,20 @@ def self.available_packager
private

def precompile_assets
RakeTaskRunner.new("kumade:before_asset_compilation", self).invoke
RakeTaskRunner.new("kumade:before_asset_compilation").invoke
end

def package
return success(success_message) if Kumade.configuration.pretending?
return Kumade.outputter.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)
Kumade.outputter.success(success_message)
end
rescue => packager_exception
error("Error: #{packager_exception.class}: #{packager_exception.message}")
Kumade.outputter.error("Error: #{packager_exception.class}: #{packager_exception.message}")
end
end

Expand Down
5 changes: 2 additions & 3 deletions lib/kumade/rake_task_runner.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module Kumade
class RakeTaskRunner
def initialize(task_name, runner)
def initialize(task_name)
@task_name = task_name
@runner = runner
end

def invoke
return unless task_defined?

@runner.success("Running rake task: #{@task_name}")
Kumade.outputter.success("Running rake task: #{@task_name}")
Rake::Task[@task_name].invoke if task_should_be_run?
end

Expand Down
17 changes: 0 additions & 17 deletions spec/kumade/base_spec.rb

This file was deleted.

24 changes: 11 additions & 13 deletions spec/kumade/command_line_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
require 'spec_helper'

describe Kumade::CommandLine, "#run_or_error" do
let(:command_line) { stub("Cocaine::CommandLine instance", :run => nil) }
subject { Kumade::CommandLine.new("echo") }

before do
subject.stubs(:error)
subject.stubs(:say_status)

Cocaine::CommandLine.stubs(:new).returns(command_line)
end
subject { Kumade::CommandLine.new("echo") }

context "when pretending" do
let(:command_line) { stub("Cocaine::CommandLine instance", :run => nil, :command => 'command') }

before do
Cocaine::CommandLine.stubs(:new).returns(command_line)
Kumade.configuration.pretending = true
end

Expand All @@ -21,12 +16,16 @@

command_line.should have_received(:run).never
end

it "prints the command" do
subject.run_or_error
Kumade.outputter.should have_received(:info).with(command_line.command).once
end
end

context "when successful" do
before do
Kumade.configuration.pretending = false
command_line.stubs(:run => true)
end

it "returns true" do
Expand All @@ -43,7 +42,7 @@
it "prints an error message" do
subject.run_or_error("something bad")

subject.should have_received(:error).with("something bad")
Kumade.outputter.should have_received(:error).with("something bad")
end
end
end
Expand All @@ -55,13 +54,12 @@

before do
Cocaine::CommandLine.stubs(:new).returns(command_line)
subject.stubs(:say_status)
end

it "prints the command" do
subject.run_with_status

subject.should have_received(:say_status).with(:run, "echo").once
Kumade.outputter.should have_received(:info).with(command).once
end

context "when pretending" do
Expand Down
Loading

0 comments on commit 6d7c867

Please sign in to comment.