diff --git a/lib/capistrano/gitflow.rb b/lib/capistrano/gitflow.rb index 4abec99..32403b5 100644 --- a/lib/capistrano/gitflow.rb +++ b/lib/capistrano/gitflow.rb @@ -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 diff --git a/lib/capistrano/gitflow/base.rb b/lib/capistrano/gitflow/base.rb new file mode 100644 index 0000000..df73abe --- /dev/null +++ b/lib/capistrano/gitflow/base.rb @@ -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 diff --git a/lib/capistrano/gitflow/helpers/base_helper.rb b/lib/capistrano/gitflow/helpers/base_helper.rb new file mode 100644 index 0000000..cefc463 --- /dev/null +++ b/lib/capistrano/gitflow/helpers/base_helper.rb @@ -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 diff --git a/lib/capistrano/gitflow/helpers/legacy_helper.rb b/lib/capistrano/gitflow/helpers/legacy_helper.rb new file mode 100644 index 0000000..ae8515f --- /dev/null +++ b/lib/capistrano/gitflow/helpers/legacy_helper.rb @@ -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 diff --git a/lib/capistrano/gitflow/helpers/modern_helper.rb b/lib/capistrano/gitflow/helpers/modern_helper.rb new file mode 100644 index 0000000..ea52fa8 --- /dev/null +++ b/lib/capistrano/gitflow/helpers/modern_helper.rb @@ -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 diff --git a/lib/capistrano/gitflow/helpers/helper.rb b/lib/capistrano/gitflow/helpers/shared_helper.rb similarity index 89% rename from lib/capistrano/gitflow/helpers/helper.rb rename to lib/capistrano/gitflow/helpers/shared_helper.rb index 2eed961..96aa1ee 100644 --- a/lib/capistrano/gitflow/helpers/helper.rb +++ b/lib/capistrano/gitflow/helpers/shared_helper.rb @@ -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 @@ -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 diff --git a/lib/capistrano/gitflow/legacy/gitflow.rb b/lib/capistrano/gitflow/legacy/gitflow.rb index 63d1a46..f81aa88 100644 --- a/lib/capistrano/gitflow/legacy/gitflow.rb +++ b/lib/capistrano/gitflow/legacy/gitflow.rb @@ -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 diff --git a/lib/capistrano/tasks/gitflow.rb b/lib/capistrano/tasks/gitflow.rb index 858e9f2..d3be848 100644 --- a/lib/capistrano/tasks/gitflow.rb +++ b/lib/capistrano/tasks/gitflow.rb @@ -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