Class: Sake::TasksFile

Inherits:
Object
  • Object
show all
Includes:
Rake::TaskManager
Defined in:
lib/sake.rb

Overview

This class represents a Rake task file, in the traditional sense. It takes on parameter: the path to a Rakefile. When instantiated, it will read the file and parse out the rake tasks, storing them in a ‘tasks’ array. This array can be accessed directly:

file = Sake::TasksFile.parse('Rakefile')
puts file.tasks.inspect

The parse method also works with remote files, as its implementation uses open-uri’s open().

Sake::TasksFile.parse('Rakefile')
Sake::TasksFile.parse('http://errtheblog.com/code/errake')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTasksFile

Returns a new instance of TasksFile.



339
340
341
342
343
# File 'lib/sake.rb', line 339

def initialize
  @namespace = []
  @tasks     = TasksArray.new
  @comment   = nil
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



323
324
325
# File 'lib/sake.rb', line 323

def tasks
  @tasks
end

Class Method Details

.parse(file) ⇒ Object

The idea here is that we may be sucking in Rakefiles from an untrusted source. While we’re happy to let the user audit the code of any Rake task before running it, we’d rather not be responsible for executing a ‘rm -rf` in the Rakefile itself. To ensure this, we need to set a safelevel before parsing the Rakefile in question.



331
332
333
334
335
336
337
# File 'lib/sake.rb', line 331

def self.parse(file)
  body = open(file).read

  instance = new
  Thread.new { instance.instance_eval "$SAFE = 3\n#{body}" }.join
  instance
end

Instance Method Details

#add_task(task) ⇒ Object

Single task version of add_tasks



404
405
406
# File 'lib/sake.rb', line 404

def add_task(task)
  @tasks << task
end

#add_tasks(tasks) ⇒ Object

Add tasks to this TasksFile. Can accept another TasksFile object or an array of Task objects.



396
397
398
399
400
# File 'lib/sake.rb', line 396

def add_tasks(tasks)
  Array(tasks.is_a?(TasksFile) ? tasks.tasks : tasks).each do |task|
    add_task task
  end
end

#has_task?(task) ⇒ Boolean

Does this task exist?

Returns:

  • (Boolean)


410
411
412
# File 'lib/sake.rb', line 410

def has_task?(task)
  @tasks.map { |t| t.to_s }.include? task.to_s
end

#remove_task(target_task) ⇒ Object

Hunt for and remove a particular task.



416
417
418
# File 'lib/sake.rb', line 416

def remove_task(target_task)
  @tasks.reject! { |task| task.name == target_task.name }
end

#to_rubyObject

Call to_ruby on all our tasks and return a concat’d string of them.



389
390
391
# File 'lib/sake.rb', line 389

def to_ruby
  @tasks.to_ruby
end