Class: Rocco::Task

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/rocco/tasks.rb

Overview

‘Rocco::Task.new` takes a task name, the destination directory docs should be built under, and a source file pattern or file list.

Instance Method Summary collapse

Constructor Details

#initialize(task_name, dest = 'docs/', sources = 'lib/**/*.rb', options = {}) ⇒ Task

Returns a new instance of Task.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rocco/tasks.rb', line 63

def initialize(task_name, dest='docs/', sources='lib/**/*.rb', options={})
  @name = task_name
  @dest = dest[-1] == ?/ ? dest : "#{dest}/"
  @sources = FileList[sources]
  @options = options

  # Make sure there's a `directory` task defined for our destination.
  define_directory_task @dest

  # Run over the source file list, constructing destination filenames
  # and defining file tasks.
  @sources.each do |source_file|
    dest_file = source_file.sub(Regexp.new("#{File.extname(source_file)}$"), ".html")
    define_file_task source_file, "#{@dest}#{dest_file}"

    # If `rake/clean` was required, add the generated files to the list.
    # That way all Rocco generated are removed when running `rake clean`.
    CLEAN.include "#{@dest}#{dest_file}" if defined? CLEAN
  end
end

Instance Method Details

#define_directory_task(path) ⇒ Object

Define the destination directory task and make the ‘:rocco` task depend on it. This causes the destination directory to be created if it doesn’t already exist.



87
88
89
90
# File 'lib/rocco/tasks.rb', line 87

def define_directory_task(path)
  directory path
  task @name => path
end

#define_file_task(source_file, dest_file) ⇒ Object

Setup a ‘file` task for a single Rocco output file (`dest_file`). It depends on the source file, the destination directory, and all of Rocco’s internal source code, so that the destination file is rebuilt when any of those changes.

You can run these tasks directly with Rake:

rake docs/foo.html docs/bar.html

… would generate the ‘foo.html` and `bar.html` files but only if they don’t already exist or one of their dependencies was changed.



103
104
105
106
107
108
109
110
111
112
# File 'lib/rocco/tasks.rb', line 103

def define_file_task(source_file, dest_file)
  prerequisites = [@dest, source_file] + rocco_source_files
  file dest_file => prerequisites do |f|
    verbose { puts "rocco: #{source_file} -> #{dest_file}" }
    rocco = Rocco.new(source_file, @sources.to_a, @options)
    FileUtils.mkdir_p(File.dirname(dest_file))
    File.open(dest_file, 'wb') { |fd| fd.write(rocco.to_html) }
  end
  task @name => dest_file
end

#rocco_source_filesObject

Return a ‘FileList` that includes all of Roccos source files. This causes output files to be regenerated properly when someone upgrades the Rocco library.



117
118
119
120
# File 'lib/rocco/tasks.rb', line 117

def rocco_source_files
  libdir = File.expand_path('../..', __FILE__)
  FileList["#{libdir}/rocco.rb", "#{libdir}/rocco/**"]
end