Glyph - Defining Custom Tasks

Glyph relies on Rake to perform most of its core operations. Typically, Rake tasks are used do define the high level logic that is used by Glyph commands to, for example, compile a project or load configuration files.

Furthermore, Rake provides an easy mechanism to create dependencies among tasks: for example, to make sure that Glyph’s configuration files are loaded before everything else happens.

Creating a 'custom:generate' task

A custom task has been defined for the Glyph project used to produce this document. This custom task is used to compile a few of the documents files into standalone files, deployed in Glyph’s root folder:

  • book/text/introduction.glyphREADME.textile
  • book/text/changelog.glyphCHANGELOG.textile
  • book/text/license.glyphLICENSE.textile
  • book/text/acknowledgement.glyphAUTHORS.textile

First of all, create a lib/tasks folder in your project directory. Then, create a .rake file within it, e.g. tasks.rake.

Finally, here’s the source of the task:

 1namespace :custom do
 2  task :generate, [:file] do |t, args|
 3    generate = lambda do |source, destination|
 4 "Generating #{destination}..."
 5      Glyph.compile Glyph::PROJECT/"text/#{source}.glyph", 
 6        Glyph::PROJECT/"../#{destination}.textile"
 7    end
 8    files = {
 9      :AUTHORS => :acknowledgements, 
10      :CHANGELOG => :changelog, 
11      :LICENSE => :license, 
12      :README => :introduction
13    }
14    arg = args[:file].upcase.to_sym
15    raise RuntimeError, "Unknown file '#{arg}.glyph'" 
16      unless files.keys.include? arg
17 files[arg], arg
18 "Done."
19  end

That’s it. Note that this task is pretty useless without a command that calls it, and it won’t even show up if you run rake -T within your project directory. for more information on creating custom commands, see Defining Custom Commands.