class Crew::Task

Constants

NoMatchingHintsError

Attributes

arguments[R]
context[RW]
default_hints[RW]
desc[RW]
home[R]
name[R]
path[R]

Public Class Methods

new(home, name, load_path = nil, &dsl_blk) click to toggle source
# File lib/crew/task.rb, line 10
def initialize(home, name, load_path = nil, &dsl_blk)
  @home, @name, @load_path, @dsl_blk = home, name, load_path, dsl_blk
  @context = @home.context
  reset!
end

Public Instance Methods

add_run(*hints, &block) click to toggle source
# File lib/crew/task.rb, line 164
def add_run(*hints, &block)
  add_to_list(@runners, hints, block)
end
add_setup(*hints, &block) click to toggle source
# File lib/crew/task.rb, line 156
def add_setup(*hints, &block)
  add_to_list(@setups, hints, block)
end
add_template(name, contents) click to toggle source
# File lib/crew/task.rb, line 143
def add_template(name, contents)
  @templates[name] = contents
end
add_test(*hints, &test) click to toggle source
# File lib/crew/task.rb, line 152
def add_test(*hints, &test)
  @tests << [(@default_hints + hints).map(&:to_s), test]
end
add_verify(*hints, &block) click to toggle source
# File lib/crew/task.rb, line 160
def add_verify(*hints, &block)
  add_to_list(@verifiers, hints, block)
end
args() click to toggle source
# File lib/crew/task.rb, line 41
def args
  @arguments.proxy(self)
end
cd(dir, &blk) click to toggle source
# File lib/crew/task.rb, line 65
def cd(dir, &blk)
  @context.cd(dir, &blk)
end
digest() click to toggle source
# File lib/crew/task.rb, line 125
def digest
  Digest::SHA1.hexdigest(source)
end
hint(h) click to toggle source
# File lib/crew/task.rb, line 89
def hint(h)
  @context.hint(h)
end
hints() click to toggle source
# File lib/crew/task.rb, line 93
def hints
  @context.hints
end
home_dir() click to toggle source
# File lib/crew/task.rb, line 20
def home_dir
  @home.home_path
end
installed?() click to toggle source
# File lib/crew/task.rb, line 16
def installed?
  File.exist?(@load_path) && @load_path.start_with?(home_dir)
end
method_missing(name, *args, &blk) click to toggle source
Calls superclass method
# File lib/crew/task.rb, line 57
def method_missing(name, *args, &blk)
  if t = task(name.to_s)
    t.run!(*args, &blk)
  else
    super(name, *args, &blk)
  end
end
passing?() click to toggle source
# File lib/crew/task.rb, line 49
def passing?
  results = Dir[File.join(all_tests_path_prefix, "**", "*.json")].collect do |result|
    JSON.parse(File.read(result))['status']
  end
  results.uniq!
  results.all? {|r| %w(pass skip).include?(r)}
end
reconnect!() click to toggle source
# File lib/crew/task.rb, line 97
def reconnect!
  @context.reconnect!
end
reset!() click to toggle source
# File lib/crew/task.rb, line 24
def reset!
  @desc = ""
  @templates = {}
  @arguments = Arguments.new
  @setups = {}
  @verifiers = {}
  @runners = {}
  @tests = []
  @default_hints = []
  dsl = DSL.new(self, &@dsl_blk)
  dsl.load(@load_path) if @load_path
end
run!(*incoming_args, &blk) click to toggle source
# File lib/crew/task.rb, line 101
def run!(*incoming_args, &blk)
  reset!
  @context.with_callbacks do
    @arguments.process!(incoming_args, &blk)
    @context.logger.task(@name, args) do
      success = false
      task_finished = false

      @context.home.perform_setup { setup }
      raise "task '#{@name}' has been called from within setup, but, has no verifier" if @context.home.in_setup && !verifiable?
      success = verifiable? && verified?
      unless success
        raise "can't perform `#{@name}' because we're in setup mode, but, we're not actually performing the setup" if @context.home.in_setup && !@context.home.setup_mode
        @out = run
        if verifiable?
          @out = nil
          verified? or raise "validator for '#{@name}' failed after the task completed successfully"
        end
      end
      @out
    end
  end
end
save_data(*args) click to toggle source
# File lib/crew/task.rb, line 81
def save_data(*args)
  @context.save_data(*args)
end
save_file(*args) click to toggle source
# File lib/crew/task.rb, line 85
def save_file(*args)
  @context.save_file(*args)
end
setup() click to toggle source
# File lib/crew/task.rb, line 172
def setup
  eval_for_matching_hints(@setups)
end
sh(cmd, opts = {}) click to toggle source
# File lib/crew/task.rb, line 69
def sh(cmd, opts = {})
  @context.sh(cmd, opts)
end
sh_with_code(cmd, opts = {}) click to toggle source
# File lib/crew/task.rb, line 73
def sh_with_code(cmd, opts = {})
  @context.sh_with_code(cmd, opts)
end
source() click to toggle source
# File lib/crew/task.rb, line 37
def source
  File.read(@load_path)
end
task(name) click to toggle source
# File lib/crew/task.rb, line 77
def task(name)
  @context.task(name)
end
template(name, locals = {}) click to toggle source
# File lib/crew/task.rb, line 147
def template(name, locals = {})
  template = @templates.fetch(name)
  ERB.new(template).result(OpenStruct.new(locals).instance_eval { binding })
end
test!(tester, opts = {}) { |run_test!(opts)| ... } click to toggle source
# File lib/crew/task.rb, line 129
def test!(tester, opts = {})
  @tests.each_with_index do |(hints, test_block), index|
    force = opts[:force]
    skip = !hints.all? {|h| tester.hints.include?(h)}
    test = Test.new(self, test_path_prefix, index, skip, test_block)
    if test.result.status == 'fail' && opts[:failed_only]
      force = true
    end
    test.clear_cached_result! if force
    yield test.run_test!(opts[:fast])
  end
  clear_extra_results!
end
untested?() click to toggle source
# File lib/crew/task.rb, line 45
def untested?
  @tests.empty?
end
verify() click to toggle source
# File lib/crew/task.rb, line 168
def verify
  eval_for_matching_hints(@verifiers) { raise_no_matching_hints }
end

Private Instance Methods

add_to_list(list, hints, block) click to toggle source
# File lib/crew/task.rb, line 181
def add_to_list(list, hints, block)
  # TODO
  raise "task #{@name} tries to add an empty blah" unless block && list
  combined_hints = (@default_hints + hints).map(&:to_s)
  list[combined_hints] = block
end
all_tests_path_prefix() click to toggle source
# File lib/crew/task.rb, line 199
def all_tests_path_prefix
  parts = digest.match(/^(..)(..)(..)(.*)/)
  File.join(@home.test_path, parts[1], parts[2], parts[3], parts[4], "tests")
end
clear_cached_results!() click to toggle source
# File lib/crew/task.rb, line 238
def clear_cached_results!
  FileUtils.rm_rf(test_path_prefix)
end
clear_extra_results!() click to toggle source
# File lib/crew/task.rb, line 229
def clear_extra_results!
  index = @tests.size
  while path = File.join(test_path_prefix, "#{index}.json")
    return unless File.exist?(path)
    File.unlink(path)
    index += 1
  end
end
eval_for_matching_hints(list, *args) { || ... } click to toggle source
# File lib/crew/task.rb, line 188
def eval_for_matching_hints(list, *args)
  matching_hints = list.keys.sort_by {|hints| hints.length}.reverse.find do |hints|
    hints.all? { |h| @context.hints.include?(h) }
  end
  if matching_hints
    instance_exec(*args, &list[matching_hints])
  else
    yield if block_given?
  end
end
raise_no_matching_hints() click to toggle source
# File lib/crew/task.rb, line 177
def raise_no_matching_hints
  raise NoMatchingHintsError, "#{@name} with hints #{@context.hints.to_a}"
end
run() click to toggle source
# File lib/crew/task.rb, line 209
def run
  eval_for_matching_hints(@runners) {
    raise_no_matching_hints
  }
end
test_path_prefix() click to toggle source
# File lib/crew/task.rb, line 204
def test_path_prefix
  raise unless @context
  File.join(all_tests_path_prefix, @context.name)
end
verifiable?() click to toggle source
# File lib/crew/task.rb, line 215
def verifiable?
  !@verifiers.empty?
end
verified?() click to toggle source
# File lib/crew/task.rb, line 219
def verified?
  !verifiable? or begin
    verify
  rescue AssertionError
    false
  else
    true
  end
end