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

Commit

Permalink
Browse files Browse the repository at this point in the history
Use Kumade::Configuration to handle environment and pretending.
  • Loading branch information
Gabe Berke-Williams committed Sep 11, 2011
1 parent 681bc8f commit 469761b
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 130 deletions.
6 changes: 5 additions & 1 deletion lib/kumade.rb
Expand Up @@ -8,6 +8,10 @@ module Kumade
autoload :Configuration, "kumade/configuration"

def self.configuration
@@configuration ||= Kumade::Configuration.new
@@configuration ||= Configuration.new
end

def self.configuration=(new_configuration)
@@configuration = new_configuration
end
end
5 changes: 2 additions & 3 deletions lib/kumade/base.rb
Expand Up @@ -2,14 +2,13 @@

module Kumade
class Base < Thor::Shell::Color
attr_reader :pretending
def initialize
super()
end

def run_or_error(command, error_message)
say_status(:run, command)
if !pretending
if ! Kumade.configuration.pretending?
error(error_message) unless run(command)
end
end
Expand All @@ -33,4 +32,4 @@ def success(message)
say("==> #{message}", :green)
end
end
end
end
22 changes: 10 additions & 12 deletions lib/kumade/cli.rb
Expand Up @@ -14,13 +14,15 @@ def deployer
def initialize(args = ARGV, out = StringIO.new)
@options = {}
parse_arguments!(args)
@environment = args.shift || 'staging'

Kumade.configuration.pretending = !!@options[:pretend]
Kumade.configuration.environment = args.shift || 'staging'

self.class.swapping_stdout_for(out, print_output?) do
deploy
end
end

def self.swapping_stdout_for(io, print_output = false)
if print_output
yield
Expand All @@ -41,12 +43,12 @@ def self.swapping_stdout_for(io, print_output = false)
private

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

def parse_arguments!(args)
Expand All @@ -73,16 +75,12 @@ def parse_arguments!(args)
end.parse!(args)
end

def pretending?
!!@options[:pretend]
end

def verbose?
@options[:verbose]
end

def print_output?
pretending? || verbose?
Kumade.configuration.pretending? || verbose?
end
end
end
6 changes: 3 additions & 3 deletions lib/kumade/configuration.rb
@@ -1,8 +1,8 @@
module Kumade
class Configuration
def initialize
@environment = 'staging'
@pretending = false
def initialize(environment = 'staging', pretending = false )
@environment = environment
@pretending = pretending
end

def pretending?
Expand Down
34 changes: 16 additions & 18 deletions lib/kumade/deployer.rb
Expand Up @@ -4,14 +4,12 @@
module Kumade
class Deployer < Base
DEPLOY_BRANCH = "deploy"
attr_reader :environment, :pretending, :git
attr_reader :git

def initialize(environment = 'staging', pretending = false)
def initialize
super()
@environment = environment
@pretending = pretending
@git = Git.new(environment, pretending)
@branch = @git.current_branch
@git = Git.new
@branch = @git.current_branch
end

def deploy
Expand All @@ -38,12 +36,12 @@ def sync_github

def sync_heroku
git.create(DEPLOY_BRANCH)
git.push("#{DEPLOY_BRANCH}:master", environment, true)
git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
end

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

def post_deploy
Expand All @@ -56,13 +54,13 @@ def heroku(command)
else
"bundle exec heroku"
end
run_or_error("#{heroku_command} #{command} --remote #{environment}",
run_or_error("#{heroku_command} #{command} --remote #{Kumade.configuration.environment}",
"Failed to run #{command} on Heroku")
end

def cedar?
return @cedar unless @cedar.nil?
@cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{environment}").run.split("\n").grep(/\*/).any? do |line|
@cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{Kumade.configuration.environment}").run.split("\n").grep(/\*/).any? do |line|
line.include?("cedar")
end
end
Expand All @@ -81,7 +79,7 @@ def package_with_jammit
begin
success_message = "Packaged assets with Jammit"

if pretending
if Kumade.configuration.pretending?
success(success_message)
else
Jammit.package!
Expand All @@ -96,7 +94,7 @@ def package_with_jammit

def package_with_more
success_message = "Packaged assets with More"
if pretending
if Kumade.configuration.pretending?
success(success_message)
else
begin
Expand All @@ -113,7 +111,7 @@ def package_with_more

def invoke_custom_task
success("Running kumade:before_asset_compilation task")
Rake::Task["kumade:before_asset_compilation"].invoke unless pretending
Rake::Task["kumade:before_asset_compilation"].invoke unless Kumade.configuration.pretending?
end

def git_add_and_commit_all_assets_in(dir)
Expand Down Expand Up @@ -156,14 +154,14 @@ def custom_task?
end

def ensure_heroku_remote_exists
if git.remote_exists?(environment)
if git.remote_exists?(Kumade.configuration.environment)
if git.heroku_remote?
success("#{environment} is a Heroku remote")
success("#{Kumade.configuration.environment} is a Heroku remote")
else
error(%{Cannot deploy: "#{environment}" remote does not point to Heroku})
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
end
else
error(%{Cannot deploy: "#{environment}" remote does not exist})
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/kumade/git.rb
@@ -1,15 +1,13 @@
require 'cocaine'
module Kumade
class Git < Base
attr_reader :environment
def initialize(environment = 'staging', pretending = false)
def initialize
super()
@pretending = pretending
@environment = environment
end

def heroku_remote?
`git config --get remote.#{environment}.url`.strip =~ /^git@heroku\..+:(.+)\.git$/
remote_url = `git config --get remote.#{Kumade.configuration.environment}.url`.strip
!! remote_url.strip.match(/^git@heroku\..+:(.+)\.git$/)
end

def self.environments
Expand Down Expand Up @@ -48,7 +46,7 @@ def current_branch
end

def remote_exists?(remote_name)
if @pretending
if Kumade.configuration.pretending?
true
else
`git remote` =~ /^#{remote_name}$/
Expand All @@ -60,7 +58,7 @@ def dirty?
end

def ensure_clean_git
if ! @pretending && dirty?
if ! Kumade.configuration.pretending? && dirty?
error("Cannot deploy: repo is not clean.")
else
success("Git repo is clean")
Expand Down
38 changes: 18 additions & 20 deletions spec/kumade/base_spec.rb
Expand Up @@ -18,35 +18,33 @@
end

describe Kumade::Base, "#run_or_error" do
let(:command) { "command" }
let(:error_message) { "error_message" }
before(:each) do
let(:command) { "dummy command" }
let(:error_message) { "dummy error message" }

before do
subject.should_receive(:say_status).with(:run, command)
end

context "when pretending" do
it "should never call run" do
subject.should_receive(:pretending).and_return(true)
subject.should_receive(:run).never
subject.run_or_error(command, error_message)
before do
Kumade.configuration.pretending = true
end
end

context "when not pretending" do

before(:each) do
subject.should_receive(:pretending).and_return(false)
it "does not run the command" do
subject.should_not_receive(:run)
subject.run_or_error("dummy command", "dummy error message")
end
end

context "when not pretending" do
context "with success" do
it "should call not call error" do
it "should not call error" do
subject.should_receive(:run).and_return(true)
subject.should_receive(:error).never
subject.run_or_error(command, error_message)
end
end

context "without success" do
it "should call CommandLine.run and error with error_message" do
subject.should_receive(:run).and_return(false)
Expand All @@ -60,18 +58,18 @@
describe Kumade::Base, "#run" do
let(:comand_line_mock) { mock("Cocaine::CommandLine") }
let(:command) { "command" }

before(:each) do
Cocaine::CommandLine.should_receive(:new).with(command).and_return(comand_line_mock)
end

it "should return true when success" do
comand_line_mock.should_receive(:run)
subject.run(command).should be_true
end

it "should return false when not success" do
comand_line_mock.should_receive(:run).and_raise(Cocaine::ExitStatusError)
subject.run(command).should be_false
end
end
end
38 changes: 30 additions & 8 deletions spec/kumade/cli_spec.rb
Expand Up @@ -3,7 +3,6 @@
describe Kumade::CLI do
let(:out) { StringIO.new }
let(:environment) { 'my-environment' }

let(:deployer) { stub("Deployer", :new => deployer_instance) }
let(:deployer_instance) { stub("DeployerInstance", :deploy => nil) }

Expand All @@ -12,23 +11,43 @@

context "when pretending" do
%w(-p --pretend).each do |pretend_flag|
subject { Kumade::CLI.new([pretend_flag, environment], out) }

context pretend_flag do
subject { Kumade::CLI.new([pretend_flag, environment], out) }
it "sets pretending to true" do
subject
Kumade.configuration.pretending.should == true
end

it "deploys correctly" do
deployer.should_receive(:new).with(environment, true)
it "deploys" do
deployer_instance.should_receive(:deploy)
subject
end
end
end
end

context "with no command-line arguments" do
subject { Kumade::CLI.new([], out) }

it "sets the environment to staging" do
Kumade.configuration.environment.should == 'staging'
end

it "sets pretending to false" do
Kumade.configuration.pretending.should == false
end
end

context "running normally" do
subject { Kumade::CLI.new([environment], out) }

it "deploys correctly" do
deployer.should_receive(:new).with(environment, false)
it "sets pretending to false" do
subject
Kumade.configuration.pretending.should == false
end

it "deploys" do
deployer_instance.should_receive(:deploy)
subject
end
Expand All @@ -37,7 +56,10 @@

describe Kumade::CLI, ".deployer" do
after { Kumade::CLI.deployer = nil }
it { Kumade::CLI.deployer.should == Kumade::Deployer }

it "sets the deployer to the Deployer class by default" do
Kumade::CLI.deployer.should == Kumade::Deployer
end

it "can override deployer" do
Kumade::CLI.deployer = "deployer!"
Expand Down Expand Up @@ -78,4 +100,4 @@
end
end
end
end
end

0 comments on commit 469761b

Please sign in to comment.