Class: Sake::TasksFile

Inherits:
Object
  • Object
show all
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.



316
317
318
319
320
# File 'lib/sake.rb', line 316

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

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



300
301
302
# File 'lib/sake.rb', line 300

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.



308
309
310
311
312
313
314
# File 'lib/sake.rb', line 308

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



385
386
387
# File 'lib/sake.rb', line 385

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.



377
378
379
380
381
# File 'lib/sake.rb', line 377

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)


391
392
393
# File 'lib/sake.rb', line 391

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.



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

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.



370
371
372
# File 'lib/sake.rb', line 370

def to_ruby
  @tasks.to_ruby
end