Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactoring] Splitting responsabilities between classes and helpers #11

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/capistrano/gitflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
Gem.find_files('capistrano/gitflow/helpers/**/*.rb').each { |path| require path }
require 'capistrano/version'

self.extend CapistranoGitFlow::Helper
include CapistranoGitFlow::Helper

unless defined?(Sinatra)
if gitflow_using_cap3?
if CapistranoGitFlow::Base.is_using_cap3?
require 'capistrano/all'
require File.join(File.dirname(__FILE__), 'tasks', 'gitflow')
else
Expand Down
16 changes: 16 additions & 0 deletions lib/capistrano/gitflow/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module CapistranoGitFlow
module Base
class << self
def cap_major_version
defined?(Capistrano::VERSION) ? Capistrano::VERSION.to_s.split('.').first.to_i : nil
end

# Description of method
#
# @return [Type] description of returned object
def is_using_cap3?
cap_major_version.to_i > 0 && cap_major_version >= 3
end
end
end
end
18 changes: 18 additions & 0 deletions lib/capistrano/gitflow/helpers/base_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative '../base'
require_relative './shared_helper'
require_relative './modern_helper'
require_relative './legacy_helper'
module CapistranoGitFlow
module BaseHelper

def self.included(base)
base.send(:include, CapistranoGitFlow::SharedHelper)
if CapistranoGitFlow::Base.is_using_cap3?
base.send(:include, CapistranoGitFlow::ModernHelper)
else
base.send(:include, CapistranoGitFlow::LegacyHelper)
end
end

end
end
30 changes: 30 additions & 0 deletions lib/capistrano/gitflow/helpers/legacy_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative './shared_helper'
module CapistranoGitFlow
module LegacyHelper
include CapistranoGitFlow::SharedHelper

def gitflow_callbacks
before "deploy:update_code", "gitflow:verify_up_to_date"
super
end

def gitflow_find_task(name)
exists(name)
rescue
nil
end

def gitflow_execute_task(name)
find_and_execute_task(name)
end

def gitflow_capistrano_tag
capistrano_configuration[:tag]
end

def gitflow_ask_confirm(message)
Capistrano::CLI.ui.ask("#{message}")
end

end
end
32 changes: 32 additions & 0 deletions lib/capistrano/gitflow/helpers/modern_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative './shared_helper'
module CapistranoGitFlow
module ModernHelper
include CapistranoGitFlow::SharedHelper

def gitflow_callbacks
before "deploy", "gitflow:verify_up_to_date"
super
end

def gitflow_find_task(name)
::Rake::Task[name]
rescue
nil
end


def gitflow_execute_task(name)
gitflow_find_task(name).invoke
end

def gitflow_capistrano_tag
ENV['TAG']
end

def gitflow_ask_confirm(message)
$stdout.print "#{message}"
$stdin.gets.to_s.chomp
end

end
end
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
module CapistranoGitFlow
module Helper


module SharedHelper
def gitflow_stage
original_stage = fetch(:stage)
original_stage.to_s.include?(":") ? original_stage.split(':').reverse[0] : original_stage
end

def gitflow_using_cap3?
defined?(Capistrano::VERSION) && Capistrano::VERSION.to_s.split('.').first.to_i >= 3
end

def gitflow_callbacks
if gitflow_using_cap3?
before "deploy", "gitflow:verify_up_to_date"
else
before "deploy:update_code", "gitflow:verify_up_to_date"
end
after "gitflow:verify_up_to_date", "gitflow:calculate_tag"
end

def gitflow_find_task(name)
defined?(::Rake) ? ::Rake::Task[name] : exists?(name)
rescue
nil
end

def gitflow_execute_task(name)
defined?(::Rake) ? gitflow_find_task(name).invoke : find_and_execute_task(name)
end

def gitflow_capistrano_tag
defined?(capistrano_configuration) ? capistrano_configuration[:tag] : ENV['TAG']
end

def gitflow_last_tag_matching(pattern)
# search for most recent (chronologically) tag matching the passed pattern, then get the name of that tag.
last_tag = `git describe --exact-match --tags --match='#{pattern}' $(git log --tags='#{pattern}*' -n1 --pretty='%h')`.chomp
Expand All @@ -44,15 +19,6 @@ def gitflow_last_staging_tag
gitflow_last_tag_matching('staging-*')
end

def gitflow_ask_confirm(message)
if gitflow_using_cap3?
$stdout.print "#{message}"
$stdin.gets.to_s.chomp
else
Capistrano::CLI.ui.ask("#{message}")
end
end

def gitflow_next_staging_tag
hwhen = Date.today.to_s
who = `whoami`.chomp.to_url
Expand Down
3 changes: 3 additions & 0 deletions lib/capistrano/gitflow/legacy/gitflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
module Capistrano
class Gitflow
def self.load_into(capistrano_configuration)
capistrano_configuration.class.class_eval do
include CapistranoGitFlow::BaseHelper
end
capistrano_configuration.load File.join(File.dirname(File.dirname(File.dirname(__FILE__))), 'tasks', 'gitflow.rb')
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/capistrano/tasks/gitflow.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

namespace :gitflow do
include CapistranoGitFlow::BaseHelper if CapistranoGitFlow::Base.is_using_cap3?

task :verify_up_to_date do
gitflow_verify_up_to_date
Expand Down