Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 329ae587ac
Fetching contributors…

Cannot retrieve contributors at this time

file 242 lines (224 sloc) 7.946 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
require 'cape/capistrano'
require 'cape/rake'

module Cape

  # Provides methods for integrating Capistrano and Rake.
  module DSL

    # Enumerates Rake tasks.
    #
    # @param [String, Symbol] task_expression the full name of a task or
    # namespace to filter; optional
    # @param [Proc] block a block that processes tasks
    #
    # @yield [task] a block that processes tasks
    # @yieldparam [Hash] task metadata on a task
    #
    # @return [DSL] the object
    #
    # @example Enumerating all Rake tasks
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # Cape do
    # each_rake_task do |t|
    # # Do something interesting with this hash:
    # # * t[:name] -- the full name of the task
    # # * t[:parameters] -- the names of task arguments
    # # * t[:description] -- documentation on the task, including parameters
    # end
    # end
    #
    # @example Enumerating some Rake tasks
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # Cape do
    # each_rake_task :foo do |t|
    # # Do something interesting with this hash:
    # # * t[:name] -- the full name of the task
    # # * t[:parameters] -- the names of task arguments
    # # * t[:description] -- documentation on the task, including parameters
    # end
    # end
    def each_rake_task(task_expression=nil, &block)
      rake.each_task(task_expression, &block)
      self
    end

    # The command used to run Rake on the local computer.
    #
    # @return [String] the command used to run Rake on the local computer
    #
    # @see Rake::DEFAULT_EXECUTABLE
    def local_rake_executable
      rake.local_executable
    end

    # Sets the command used to run Rake on the local computer.
    #
    # @param [String] value the command used to run Rake on the local computer
    # @return [String] _value_
    #
    # @example Changing the local Rake executable
    # require 'cape'
    #
    # Cape do
    # self.local_rake_executable = '/path/to/rake'
    # $stdout.puts "We changed the local Rake executable to #{self.local_rake_executable.inspect}."
    # end
    def local_rake_executable=(value)
      rake.local_executable = value
    end

    # Makes the use of a Cape block parameter optional by forwarding non-Cape
    # method calls to the containing binding.
    #
    # @param [Symbol, String] method the method called
    # @param [Array] args the arguments passed to _method_
    # @param [Proc] block the block passed to _method_
    # @return the result of the forwarded method call
    def method_missing(method, *args, &block)
      @outer_self.send(method, *args, &block)
    end

    # Defines Rake tasks as Capistrano recipes.
    #
    # @overload mirror_rake_tasks(task_expression=nil)
    # Defines Rake tasks as Capistrano recipes.
    #
    # @param [String, Symbol] task_expression the full name of a Rake task or
    # namespace to filter; optional
    #
    # @yield [env] a block that defines environment variables for the Rake
    # task; optional
    # @yieldparam [Hash] env the environment variables to set before executing
    # the Rake task
    #
    # @return [DSL] the object
    #
    # @overload mirror_rake_tasks(capistrano_task_options={})
    # Defines all Rake tasks as Capistrano recipes with options.
    #
    # @param [Hash] capistrano_task_options options to pass to the Capistrano
    # +task+ method; optional
    #
    # @yield [env] a block that defines environment variables for the Rake
    # task; optional
    # @yieldparam [Hash] env the environment variables to set before executing
    # the Rake task
    #
    # @return [DSL] the object
    #
    # @overload mirror_rake_tasks(task_expression, capistrano_task_options={})
    # Defines specific Rake tasks as Capistrano recipes with options.
    #
    # @param [String, Symbol] task_expression the full name of a Rake
    # task or namespace to
    # filter
    # @param [Hash] capistrano_task_options options to pass to the
    # Capistrano +task+ method;
    # optional
    #
    # @yield [env] a block that defines environment variables for the Rake
    # task; optional
    # @yieldparam [Hash] env the environment variables to set before executing
    # the Rake task
    #
    # @return [DSL] the object
    #
    # @note Any parameters that the Rake tasks have are integrated via environment variables, since Capistrano does not support recipe parameters per se.
    #
    # @see http://github.com/capistrano/capistrano/blob/master/lib/capistrano/task_definition.rb#L15-L17 Valid Capistrano ‘task’ method options
    #
    # @example Mirroring all Rake tasks
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # Cape do
    # mirror_rake_tasks
    # end
    #
    # @example Mirroring specific Rake tasks
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # Cape do
    # mirror_rake_tasks 'log:clear'
    # end
    #
    # @example Mirroring specific Rake tasks with Capistrano recipe options and/or environment variables
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # Cape do
    # mirror_rake_tasks :db, :roles => :app do |env|
    # env['RAILS_ENV'] = rails_env
    # end
    # end
    #
    # @example Mirroring Rake tasks into a Capistrano namespace
    # # config/deploy.rb
    #
    # require 'cape'
    #
    # namespace :rake_tasks do
    # Cape do |cape|
    # cape.mirror_rake_tasks
    # end
    # end
    def mirror_rake_tasks(*arguments, &block)
      arguments_count = arguments.length
      options = arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
      unless arguments.length <= 1
        raise ::ArgumentError,
              "wrong number of arguments (#{arguments_count} for 0 or 1, plus " +
              'an options hash)'
      end

      task_expression = arguments.first
      options[:binding] = binding
      rake.each_task task_expression do |t|
        deployment_library.define_rake_wrapper(t, options, &block)
      end
      self
    end

    # The command used to run Rake on remote computers.
    #
    # @return [String] the command used to run Rake on remote computers
    #
    # @see Rake::DEFAULT_EXECUTABLE
    def remote_rake_executable
      rake.remote_executable
    end

    # Sets the command used to run Rake on remote computers.
    #
    # @param [String] value the command used to run Rake on remote computers
    # @return [String] _value_
    #
    # @example Changing the remote Rake executable
    # require 'cape'
    #
    # Cape do
    # self.remote_rake_executable = '/path/to/rake'
    # $stdout.puts "We changed the remote Rake executable to #{self.remote_rake_executable.inspect}."
    # end
    def remote_rake_executable=(value)
      rake.remote_executable = value
    end

  protected

    # Returns an abstraction of the Rake installation and available tasks.
    def rake
      @rake ||= Rake.new
    end

  private

    def deployment_library
      return @deployment_library if @deployment_library

      raise_unless_capistrano
      @deployment_library = Capistrano.new(:rake => rake)
    end

    def raise_unless_capistrano
      if @outer_self.method(:task).owner.name !~ /^Capistrano::/
        raise 'Use this in the context of Capistrano recipes'
      end
    end

  end

end
Something went wrong with that request. Please try again.