Method: Buildr::Project#task

Defined in:
lib/buildr/core/project.rb

#task(*args, &block) ⇒ Object

:call-seq:

task(name) => Task
task(name=>prereqs) => Task
task(name) { |task| ... } => Task

Creates and returns a new task in the project. Similar to calling Rake’s task method, but prefixes the task name with the project name and executes the task in the project’s base directory.

For example:

define 'foo' do
  task 'doda'
end

puts project('foo').task('doda').name
=> 'foo:doda'

When called from within the project definition, creates a new task if the task does not already exist. If called from outside the project definition, returns the named task and raises an exception if the task is not defined.

As with Rake’s task method, calling this method enhances the task with the prerequisites and optional block.



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/buildr/core/project.rb', line 516

def task(*args, &block)
  task_name, arg_names, deps = Buildr.application.resolve_args(args)
  if task_name =~ /^:/
    task = Buildr.application.switch_to_namespace [] do
      Rake::Task.define_task(task_name[1..-1])
    end
  elsif Buildr.application.current_scope == name.split(':')
    task = Rake::Task.define_task(task_name)
  else
    unless task = Buildr.application.lookup(task_name, name.split(':'))
      raise "You cannot define a project task outside the project definition, and no task #{name}:#{task_name} defined in the project"
    end
  end
  task.set_arg_names(arg_names) unless arg_names.empty?
  task.enhance Array(deps), &block
end