Module: Cape::DSL

Included in:
Cape
Defined in:
lib/cape/dsl.rb

Overview

Provides methods for integrating Capistrano and Rake.

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block)

Makes the use of a Cape block parameter optional by forwarding non-Cape method calls to the containing binding.

Parameters:

  • method (Symbol, String)

    the method called

  • args (Array)

    the arguments passed to method

  • block (Proc)

    the block passed to method

Returns:

  • the result of the forwarded method call



89
90
91
# File 'lib/cape/dsl.rb', line 89

def method_missing(method, *args, &block)
  @outer_self.send(method, *args, &block)
end

Instance Method Details

- (DSL) each_rake_task(task_expression = nil, &block) {|task| ... }

Enumerates Rake tasks.

Examples:

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

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

Parameters:

  • task_expression (String, Symbol) (defaults to: nil)

    the full name of a task or namespace to filter

  • block (Proc)

    a block that processes tasks

Yields:

  • (task)

    a block that processes tasks

Yield Parameters:

  • task (Hash)

    metadata on a task

Returns:

  • (DSL)

    the object



49
50
51
52
# File 'lib/cape/dsl.rb', line 49

def each_rake_task(task_expression=nil, &block)
  rake.each_task(task_expression, &block)
  self
end

- (String) local_rake_executable

The command used to run Rake on the local computer.

Returns:

  • (String)

    the command used to run Rake on the local computer

See Also:



59
60
61
# File 'lib/cape/dsl.rb', line 59

def local_rake_executable
  rake.local_executable
end

- (String) local_rake_executable=(value)

Sets the command used to run Rake on the local computer.

Examples:

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 ' +
               "#{local_rake_executable.inspect}."
end

Parameters:

  • value (String)

    the command used to run Rake on the local computer

Returns:

  • (String)

    value



77
78
79
# File 'lib/cape/dsl.rb', line 77

def local_rake_executable=(value)
  rake.local_executable = value
end

- (DSL) mirror_rake_tasks(task_expression = nil) {|recipes| ... }

Note:

Any parameters that the Rake tasks have are integrated via environment variables, since Capistrano does not support recipe parameters per se.

Defines Rake tasks as Capistrano recipes.

Examples:

Mirroring all Rake tasks

# config/deploy.rb

require 'cape'

Cape do
  # Create Capistrano recipes for all Rake tasks.
  mirror_rake_tasks
end

Mirroring some Rake tasks, but not others

# config/deploy.rb

require 'cape'

Cape do
  # Create Capistrano recipes for the Rake task 'foo' and/or for the
  # tasks in the 'foo' namespace.
  mirror_rake_tasks :foo
end

Mirroring Rake tasks that require renaming, Capistrano recipe options, path switching, and/or environment variables

# config/deploy.rb

require 'cape'

Cape do
  # Display defined Rails routes on application server remote machines
  # only.
  mirror_rake_tasks :routes do |recipes|
    recipes.options[:roles] = :app
  end

  # Execute database migration on application server remote machines
  # only, and set the 'RAILS_ENV' environment variable to the value of
  # the Capistrano variable 'rails_env'.
  mirror_rake_tasks 'db:migrate' do |recipes|
    recipes.options[:roles] = :app
    recipes.env['RAILS_ENV'] = lambda { rails_env }
  end

  # Support a Rake task that must be run on application server remote
  # machines only, and in the remote directory 'release_path' instead of
  # the default, 'current_path'.
  before 'deploy:symlink', :spec
  mirror_rake_tasks :spec do |recipes|
    recipes.cd { release_path }
    recipes.options[:roles] = :app
  end

  # Avoid collisions with the existing Ruby method #test, run tests on
  # application server remote machines only, and set the 'RAILS_ENV'
  # environment variable to the value of the Capistrano variable
  # 'rails_env'.
  mirror_rake_tasks :test do |recipes|
    recipes.rename do |rake_task_name|
      "#{rake_task_name}_task"
    end
    recipes.options[:roles] = :app
    recipes.env['RAILS_ENV'] = lambda { rails_env }
  end
end

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

Parameters:

  • task_expression (String, Symbol) (defaults to: nil)

    the full name of a Rake task or namespace to filter

Yields:

  • (recipes)

    a block that customizes the Capistrano recipe(s) generated for the Rake task(s); optional

Yield Parameters:

  • recipes (RecipeDefinition)

    an interface for customizing the Capistrano recipe(s) generated for the Rake task(s)

Returns:

  • (DSL)

    the object



181
182
183
184
185
186
# File 'lib/cape/dsl.rb', line 181

def mirror_rake_tasks(task_expression=nil, &block)
  rake.each_task task_expression do |t|
    deployment_library.define_rake_wrapper(t, :binding => binding, &block)
  end
  self
end

- (Object) rake (protected)

Returns an abstraction of the Rake installation and available tasks.



218
219
220
# File 'lib/cape/dsl.rb', line 218

def rake
  @rake ||= new_rake
end

- (String) remote_rake_executable

The command used to run Rake on remote computers.

Returns:

  • (String)

    the command used to run Rake on remote computers

See Also:



193
194
195
# File 'lib/cape/dsl.rb', line 193

def remote_rake_executable
  rake.remote_executable
end

- (String) remote_rake_executable=(value)

Sets the command used to run Rake on remote computers.

Examples:

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 ' +
               "#{remote_rake_executable.inspect}."
end

Parameters:

  • value (String)

    the command used to run Rake on remote computers

Returns:

  • (String)

    value



211
212
213
# File 'lib/cape/dsl.rb', line 211

def remote_rake_executable=(value)
  rake.remote_executable = value
end