The base code generator is bare-bones. It sets up the source and destination paths and tells the logger whether to keep its trap shut.
It’s useful for copying files such as stylesheets, images, or javascripts.
For more comprehensive template-based passive code generation with arguments, you’ll want RubiGen::NamedBase.
Generators create a manifest of the actions they perform then hand the manifest to a command which replays the actions to do the heavy lifting (such as checking for existing files or creating directories if needed). Create, destroy, and list commands are included. Since a single manifest may be used by any command, creating new generators is as simple as writing some code templates and declaring what you’d like to do with them.
The manifest method must be implemented by subclasses, returning a RubiGen::Manifest. The record method is provided as a convenience for manifest creation. Example:
class StylesheetGenerator < RubiGen::Base def manifest record do |m| m.directory('public/stylesheets') m.file('application.css', 'public/stylesheets/application.css') end end end
See RubiGen::Commands::Create for a list of methods available to the manifest.
# File lib/rubigen/base.rb, line 100 100: def initialize(runtime_args, runtime_options = {}) 101: @args = runtime_args 102: parse!(@args, runtime_options) 103: 104: # Derive source and destination paths. 105: @source_root = options[:source] || File.join(spec.path, 'templates') 106: if options[:destination] 107: @destination_root = options[:destination] 108: elsif defined? ::APP_ROOT 109: @destination_root = ::APP_ROOT 110: elsif defined? ::RAILS_ROOT 111: @destination_root = ::RAILS_ROOT 112: end 113: 114: # Silence the logger if requested. 115: logger.quiet = options[:quiet] 116: 117: @stdout = options[:stdout] 118: 119: # Raise usage error if help is requested. 120: usage if options[:help] 121: end
# File lib/rubigen/base.rb, line 173 173: def after_generate 174: end
Return the basename of the destination_root, BUT, if it is trunk, tags, or branches, it continues to the parent path for the name
# File lib/rubigen/base.rb, line 163 163: def base_name 164: name = File.basename(destination_root) 165: root = destination_root 166: while ]trunk branches tags].include? name 167: root = File.expand_path(File.join(root, "..")) 168: name = File.basename(root) 169: end 170: name 171: end
Return the full path from the destination root for the given path. Example for destination_root = ’/dest’:
destination_path('some/path.rb') == '/dest/some/path.rb'
# File lib/rubigen/base.rb, line 156 156: def destination_path(relative_destination) 157: File.expand_path(File.join(destination_root, relative_destination)) 158: end
Generators must provide a manifest. Use the record method to create a new manifest and record your generator’s actions.
# File lib/rubigen/base.rb, line 125 125: def manifest 126: raise NotImplementedError, "No manifest for '#{spec.name}' generator." 127: end
Return the full path from the source root for the given path. Example for source_root = ’/source’:
source_path('some/path.rb') == '/source/some/path.rb'
The given path may include a colon ’:’ character to indicate that the file belongs to another generator. This notation allows any generator to borrow files from another. Example:
source_path('model:fixture.yml') = '/model/source/path/fixture.yml'
# File lib/rubigen/base.rb, line 137 137: def source_path(relative_source) 138: # Check whether we're referring to another generator's file. 139: name, path = relative_source.split(':', 2) 140: 141: # If not, return the full path to our source file. 142: if path.nil? 143: File.join(source_root, name) 144: 145: # Otherwise, ask our referral for the file. 146: else 147: # FIXME: this is broken, though almost always true. Others' 148: # source_root are not necessarily the templates dir. 149: File.join(self.class.lookup(name).path, 'templates', path) 150: end 151: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.