Class: Cape::Rake

Inherits:
Object
  • Object
show all
Defined in:
lib/cape/rake.rb

Overview

An abstraction of the Rake installation and available tasks.

Constant Summary

DEFAULT_EXECUTABLE =

The default command used to run Rake. We use `bundle check` to detect the presence of Bundler and a Bundler configuration. If Bundler is installed on the computer and configured, we prepend `rake` with `bundle exec`.

(
 '/usr/bin/env '                                +
 '`'                                            +
  '/usr/bin/env bundle check >/dev/null 2>&1; ' +
  'case $? in '                                 +
     # Exit code 0: bundle is defined and installed
     # Exit code 1: bundle is defined but not installed
    '0|1 ) '                                    +
      'echo bundle exec '                       +
      ';; '                                     +
  'esac'                                        +
 '` '                                           +
 'rake'
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Rake

Constructs a new Rake object with the specified attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    attribute values



34
35
36
37
38
# File 'lib/cape/rake.rb', line 34

def initialize(attributes={})
  attributes.each do |name, value|
    send "#{name}=", value
  end
end

Instance Attribute Details

#remote_executableString

The command used to run Rake on remote computers.

Returns:

  • (String)

    the command used to run Rake on remote computers

See Also:



125
126
127
# File 'lib/cape/rake.rb', line 125

def remote_executable
  @remote_executable ||= DEFAULT_EXECUTABLE
end

Instance Method Details

#==(other) ⇒ true, false

Compares the Rake object to another.

Parameters:

  • other (Object)

    another object

Returns:

  • (true)

    the Cape::Rake object is equal to other

  • (false)

    the Cape::Rake object is unequal to other



46
47
48
49
50
# File 'lib/cape/rake.rb', line 46

def ==(other)
  other.kind_of?(Rake)                          &&
  (other.local_executable  == local_executable) &&
  (other.remote_executable == remote_executable)
end

#each_task(task_expression = nil) {|task| ... } ⇒ Rake

Enumerates Rake tasks.

Parameters:

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

    the full name of a task or namespace to filter

Yields:

  • (task)

    a block that processes tasks

Yield Parameters:

  • task (Hash)

    metadata on a task

Returns:

  • (Rake)

    the object



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
# File 'lib/cape/rake.rb', line 61

def each_task(task_expression=nil)
  previous_task, this_task = nil, nil
  task_expression = task_expression                       ?
                    ::Regexp.escape(task_expression.to_s) :
                    '.+?'
  regexp = /^rake (#{task_expression}(?::.+?)?)(?:\[(.+?)\])?\s+#\s*(.*)/
  each_output_line do |l|
    unless (matches = l.chomp.match(regexp))
      next
    end

    previous_task = this_task
    this_task = {}.tap do |t|
      t[:name]        = matches[1].strip
      t[:parameters]  = matches[2].split(',') if matches[2]
      t[:description] = matches[3]
    end
    if previous_task
      all_but_last_segment = this_task[:name].split(':')[0...-1].join(':')
      previous_task[:default] = all_but_last_segment ==
                                previous_task[:name]
      yield previous_task
    end
  end
  yield this_task if this_task
  self
end

#expire_cache!Object

Forces cached Rake task metadata (if any) to be discarded.



90
91
92
93
# File 'lib/cape/rake.rb', line 90

def expire_cache!
  @output_lines = nil
  self
end

#local_executableString

The command used to run Rake on the local computer.

Returns:

  • (String)

    the command used to run Rake on the local computer

See Also:



100
101
102
# File 'lib/cape/rake.rb', line 100

def local_executable
  @local_executable ||= DEFAULT_EXECUTABLE
end

#local_executable=(value) ⇒ String

Sets the command used to run Rake on the local computer and discards any cached Rake task metadata.

Parameters:

  • value (String)

    the command used to run Rake on the local computer

Returns:

  • (String)

    value

See Also:



112
113
114
115
116
117
118
# File 'lib/cape/rake.rb', line 112

def local_executable=(value)
  unless @local_executable == value
    @local_executable = value
    expire_cache!
  end
  value
end