Class: Mattock::TaskLib

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Calibrate::Configurable::DirectoryStructure, CascadingDefinition
Defined in:
lib/mattock/tasklib.rb

Overview

TaskLib provides a base class to build tasklibs on so that you can get to what you care about, and get option validation as well.

The convention that’s added in Mattock is that Tasklibs are passed to each other as arguments, so that behavior can be composed out of modular components.

To define a new task lib: subclass TaskLib, add some ::setting calls, and override #define to add some tasks.

To use your tasklib, instantiate with a block, optionally passing other task libs to copy configuration from.

The configuration handling is provided by CascadingDefinition, and configuration options are built using Configurable

Examples:

class CoolTask < Mattock::TaskLib
  settings :option_one, :option_two

  default_namespace :be

  def define
    task :cool do
      puts "I am so #{option_one} #{option_two}"
    end
  end
end

CoolTask.new(:args) do |t|
  t.option_one = "cool"
  t.option_two = "very"
end
> rake be:cool
I am so very cool

Composition

transport = HTTPTasks.new do |t|
  t.server = http://mycoolserver.com
end

UploadTasks.new(transport) do |t|
  t.dir = "./source_dir"
end

Direct Known Subclasses

CommandTaskLib

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CascadingDefinition

#confirm_configuration, #confirm_step, #confirm_steps, #default_configuration, #resolve_configuration, #setup_cascade

Constructor Details

#initialize(*toolkits, &block) ⇒ TaskLib

Returns a new instance of TaskLib.



65
66
67
68
# File 'lib/mattock/tasklib.rb', line 65

def initialize(*toolkits, &block)
  @tasks = []
  setup_cascade(*toolkits, &block)
end

Instance Attribute Details

#namespace_name=(value) ⇒ Object (writeonly)

Sets the attribute namespace_name

Parameters:

  • value

    the value to set the attribute namespace_name to.



55
56
57
# File 'lib/mattock/tasklib.rb', line 55

def namespace_name=(value)
  @namespace_name = value
end

#tasksObject (readonly)

Returns the value of attribute tasks.



63
64
65
# File 'lib/mattock/tasklib.rb', line 63

def tasks
  @tasks
end

Class Method Details

.default_namespace(name) ⇒ Object

The namespace this lib’s tasks will created within. Changeable at instantiation



59
60
61
# File 'lib/mattock/tasklib.rb', line 59

def self.default_namespace(name)
  setting(:namespace_name, name).isnt(:copiable)
end

Instance Method Details

#[](taskname) ⇒ Object

Wraps a single task in lib’s namespace



147
148
149
# File 'lib/mattock/tasklib.rb', line 147

def [](taskname)
  in_namespace(taskname).first
end

#bracket_task(before, name, after, &block) ⇒ Object

Shorthand for

task name => before
task after => name

Which ensures that if “after” is ever invoked, the execution will be before, name, then after



82
83
84
85
# File 'lib/mattock/tasklib.rb', line 82

def bracket_task(before, name, after, &block)
  task self[name] => before, &block
  task after => self[name]
end

#debug_settings_taskObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mattock/tasklib.rb', line 134

def debug_settings_task
  in_namespace do
    task :debug_settings do
      require 'pp'
      puts self.class.name
      pp self.to_hash
    end
  end

  task :debug_settings => self[:debug_settings]
end

#default_namespaceObject



123
124
125
# File 'lib/mattock/tasklib.rb', line 123

def default_namespace
  nil
end

#defineObject

Default define defines some tasks related to debugging Rakefiles - subclasses can get these just by remembering to call ‘super’ in their define



130
131
132
# File 'lib/mattock/tasklib.rb', line 130

def define
  debug_settings_task
end

#in_namespace(args) ⇒ Object #in_namespace(&block) ⇒ Object

Overloads:

  • #in_namespace(args) ⇒ Object

    maps the arguments to namespace-prefixed names, for use in Rake dependency declaration

  • #in_namespace(&block) ⇒ Object

    wraps the block in a Rake namespace call



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mattock/tasklib.rb', line 101

def in_namespace(*tasknames)
  if tasknames.empty?
    if block_given?
      if @namespace_name.nil?
        yield
      else
        namespace @namespace_name do
          yield
        end
      end
    end
  else
    tasknames.map do |taskname|
      [@namespace_name, taskname].compact.join(":")
    end
  end
end

#root_taskObject



119
120
121
# File 'lib/mattock/tasklib.rb', line 119

def root_task
  @namespace_name || :default
end

#task(*args) ⇒ Object

Records tasks as they are created



71
72
73
74
75
# File 'lib/mattock/tasklib.rb', line 71

def task(*args)
  a_task = super
  @tasks << a_task
  return a_task
end

#task_spine(*list) ⇒ Object

Builds a series of tasks in a sequence - the idea is that dependant tasklibs can depend on stages of a larger process



89
90
91
92
93
94
# File 'lib/mattock/tasklib.rb', line 89

def task_spine(*list)
  task list.first
  list.each_cons(2) do |first, second|
    task second => first
  end
end