Sha256: 08d74ef94ec65c284bf0d1b3e0e3754c9f47674b2638745bc4a9cd8e47d7dadd

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

module CapistranoRecipes
  module Multistage
    def self.load_into(configuration)
      configuration.load do
        set :multistage_config, Config.new(configuration)

        def stage(name, options={}, &block)
          set :default_stage, name.to_sym if options.delete(:default)
          multistage_config.stage(name, options)
          callbacks[:start].detect { |c| c.source == 'multistage:ensure' }.except << name.to_s

          task(name) do
            set :current_stage, name.to_s
            set :rails_env, name.to_s
            options.each do |k, v|
              set k, v if v
            end
            block.call if block
          end
        end

        namespace :multistage do
          task :ensure do
            unless exists?(:current_stage)
              if stage = multistage_config.inferred_stage
                find_and_execute_task(stage.name)
              elsif exists?(:default_stage)
                find_and_execute_task(default_stage)
              else
                stage_names = multistage_config.stages.map(&:name)
                abort "No stage specified. Please specify one of: #{stage_names.join(', ')} (e.g. `cap #{stage_names.first} #{ARGV.last}')"
              end
            end
          end
        end

        on :start, 'multistage:ensure'
      end
    end

    # Handles multistage configuration
    class Config
      attr_reader :config
      attr_reader :stages

      def initialize(config)
        @config = config
        @stages = []
      end

      def stage(name, options={})
        stages << Stage.new(name, options)
      end

      def inferred_stage
        if config.using_recipe?(:git)
          branch = config.local_branch
          stages.find { |stage| stage.options[:branch].to_s == branch }
        end
      end
    end

    class Stage
      attr_reader :name
      attr_reader :options

      def initialize(name, options={})
        @name    = name
        @options = options
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
capistrano_recipes-1.4.2 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.4.1 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.4.0 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.3.4 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.3.3 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.3.2 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.3.1 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.3.0 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.2.0 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.1.0 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.5 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.4 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.3 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.2 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.1 lib/capistrano/recipes/multistage.rb
capistrano_recipes-1.0.0 lib/capistrano/recipes/multistage.rb