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

Commit

Permalink
Merge branch 'master' into verbose
Browse files Browse the repository at this point in the history
Conflicts:
	lib/kumade/cli.rb
	spec/kumade/cli_spec.rb
  • Loading branch information
tapajos committed Sep 10, 2011
2 parents 09ca948 + ee40dbb commit 5a4af13
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 175 deletions.
2 changes: 1 addition & 1 deletion bin/kumade
Expand Up @@ -2,4 +2,4 @@

require 'kumade'

Kumade::CLI.run
Kumade::CLI.new
73 changes: 39 additions & 34 deletions lib/kumade/cli.rb
Expand Up @@ -4,39 +4,61 @@
module Kumade
class CLI
class << self
attr_reader :environment
attr_writer :deployer

def deployer
@deployer || Kumade::Deployer
end
end

def self.run(args=ARGV, out=StringIO.new)
@out = out
@options = parse_arguments!(args)
def initialize(args = ARGV, out = StringIO.new)
@options = {}
parse_arguments!(args)
@environment = args.shift || 'staging'

swapping_stdout_for(@out) do
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
else
begin
real_stdout = $stdout
$stdout = io
yield
rescue Kumade::DeploymentError
io.rewind
real_stdout.print(io.read)
ensure
$stdout = real_stdout
end
end
end

def self.deploy
private

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

def self.parse_arguments!(args)
options = {}
def parse_arguments!(args)
OptionParser.new do |opts|
opts.banner = "Usage: kumade <environment> [options]"

opts.on("-p", "--pretend", "Pretend mode: print what kumade would do") do |p|
options[:pretend] = p
@options[:pretend] = p
end

opts.on_tail("-v", "--verbose", "Print what kumade is doing") do
options[:verbose] = true
@options[:verbose] = true
end

opts.on_tail('--version', 'Show version') do
Expand All @@ -49,34 +71,17 @@ def self.parse_arguments!(args)
exit
end
end.parse!(args)

options
end

def self.swapping_stdout_for(io)
begin
$real_stdout = $stdout
$stdout = io unless print_output?
yield
rescue Kumade::DeploymentError
unless print_output?
io.rewind
$real_stdout.print(io.read)
end
ensure
$stdout = $real_stdout
end
end

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

def self.verbose?
def verbose?
@options[:verbose]
end

def self.print_output?
def print_output?
pretending? || verbose?
end
end
Expand Down
22 changes: 14 additions & 8 deletions lib/kumade/deployer.rb
@@ -1,4 +1,5 @@
require "rake"
require 'cocaine'

module Kumade
class Deployer < Base
Expand All @@ -9,16 +10,20 @@ def initialize(environment = 'staging', pretending = false)
super()
@environment = environment
@pretending = pretending
@git = Git.new(pretending, environment)
@git = Git.new(environment, pretending)
@branch = @git.current_branch
end

def deploy
ensure_heroku_remote_exists
pre_deploy
sync_heroku
heroku_migrate
post_deploy
begin
ensure_heroku_remote_exists
pre_deploy
sync_heroku
heroku_migrate
rescue
ensure
post_deploy
end
end

def pre_deploy
Expand Down Expand Up @@ -56,7 +61,8 @@ def heroku(command)
end

def cedar?
@cedar ||= heroku("stack").split("\n").grep(/\*/).any? do |line|
return @cedar unless @cedar.nil?
@cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{environment}").run.split("\n").grep(/\*/).any? do |line|
line.include?("cedar")
end
end
Expand Down Expand Up @@ -106,7 +112,7 @@ def package_with_more
end

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

Expand Down
4 changes: 2 additions & 2 deletions lib/kumade/git.rb
Expand Up @@ -2,9 +2,9 @@
module Kumade
class Git < Base
attr_reader :environment
def initialize(pretending, environment)
def initialize(environment = 'staging', pretending = false)
super()
@pretending = pretending
@pretending = pretending
@environment = environment
end

Expand Down
114 changes: 44 additions & 70 deletions spec/kumade/cli_spec.rb
@@ -1,47 +1,56 @@
require 'spec_helper'

describe Kumade::CLI do
subject { Kumade::CLI }
let(:out){ StringIO.new }
let(:environment){ 'my-environment' }

%w(-p --pretend).each do |pretend_arg|
it "sets pretend mode when run with #{pretend_arg}" do
subject.stub(:deploy)

subject.run([environment, pretend_arg], out)
subject.pretending?.should be_true
let(:out) { StringIO.new }
let(:environment) { 'my-environment' }

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

before { Kumade::CLI.deployer = deployer }
after { Kumade::CLI.deployer = nil }

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

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

%w(-v --verbose).each do |verbose_arg|
it "sets verbose mode when run with #{verbose_arg}" do
subject.stub(:deploy)
context "running normally" do
subject { Kumade::CLI.new([environment], out) }

subject.run([environment, verbose_arg], out)
subject.verbose?.should be_true
it "deploys correctly" do
deployer.should_receive(:new).with(environment, false)
deployer_instance.should_receive(:deploy)
subject
end
end
end

it "defaults to staging" do
subject.stub(:deploy)
subject.run([], out)
subject.environment.should == 'staging'
end

it "deploys" do
Kumade::Deployer.any_instance.should_receive(:deploy)
describe Kumade::CLI, ".deployer" do
after { Kumade::CLI.deployer = nil }
it { Kumade::CLI.deployer.should == Kumade::Deployer }

subject.run([environment], out)
it "can override deployer" do
Kumade::CLI.deployer = "deployer!"
Kumade::CLI.deployer.should == "deployer!"
end

end

describe Kumade::CLI do
describe Kumade::CLI, ".swapping_stdout_for" do
let(:stdout) { $stdout }
let(:output) { StringIO.new }

it 'does not let anything get printed' do
stdout = $stdout
stdout.should_not_receive(:print)
output = StringIO.new

Kumade::CLI.swapping_stdout_for(output) do
$stdout.puts "Hello, you can't see me."
Expand All @@ -52,56 +61,21 @@
end

it 'dumps the output stash to real stdout when an error happens' do
stdout = $stdout
stdout.should_receive(:print)
output = StringIO.new

Kumade::CLI.swapping_stdout_for(output) do
$stdout.puts "Hello, you can see me!"
raise Kumade::DeploymentError.new("error")
end
end

it 'prints everything in pretend mode' do
stdout = $stdout
stdout.should_receive(:puts)
output = StringIO.new
Kumade::CLI.should_receive(:pretending?).and_return(true)

Kumade::CLI.swapping_stdout_for(output) do
$stdout.puts "Hello, you can see me!"
end
end

it 'prints everything when verbose is true' do
stdout = $stdout
stdout.should_receive(:puts)
output = StringIO.new
Kumade::CLI.should_receive(:pretending?).and_return(false)
Kumade::CLI.should_receive(:verbose?).and_return(true)
context "in print output mode" do
it 'prints everything' do
stdout.should_receive(:puts)

Kumade::CLI.swapping_stdout_for(output) do
$stdout.puts "Hello, you can see me!"
Kumade::CLI.swapping_stdout_for(output, true) do
$stdout.puts "Hello, you can see me!"
end
end
end
end

describe Kumade::CLI, ".print_output?" do

it "should return true when pretending" do
Kumade::CLI.should_receive(:pretending?).and_return(true)
Kumade::CLI.print_output?.should be_true
end

it "should return true when verbose" do
Kumade::CLI.should_receive(:pretending?).and_return(false)
Kumade::CLI.should_receive(:verbose?).and_return(true)
Kumade::CLI.print_output?.should be_true
end

it "should return false when not verbose and not pretending" do
Kumade::CLI.should_receive(:verbose?).and_return(false)
Kumade::CLI.should_receive(:pretending?).and_return(false)
Kumade::CLI.print_output?.should be_false
end
end
end

0 comments on commit 5a4af13

Please sign in to comment.