Class: Ore::Generator
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Ore::Generator
- Includes:
- Naming, Template::Helpers, Template::Interpolations, Thor::Actions
- Defined in:
- lib/ore/generator.rb
Constant Summary collapse
- @@base_template =
The base template for all RubyGems
:base
Class Method Summary collapse
-
.register_template(path) ⇒ Object
Registers a template with the generator.
-
.templates ⇒ Object
The templates registered with the generator.
Instance Method Summary collapse
-
#disable_template(name) ⇒ Object
protected
Disables a template in the generator.
-
#enable_template(name) ⇒ Object
protected
Enables a template, adding it to the generator.
-
#enable_templates! ⇒ Object
protected
Enables templates.
- #generate ⇒ Object
-
#generate_directories! ⇒ Object
protected
Creates directories listed in the template directories.
-
#generate_files! ⇒ Object
protected
Copies static files and renders templates in the template directories.
-
#initialize_variables! ⇒ Object
protected
Initializes variables for the templates.
Methods included from Template::Helpers
#bundler?, #enabled?, #includes, #indent, #jeweler_tasks?, #ore_tasks?, #rdoc?, #rspec?, #test_unit?, #yaml_escape, #yard?
Methods included from Template::Interpolations
Class Method Details
.register_template(path) ⇒ Object
Registers a template with the generator.
38 39 40 41 42 43 44 45 |
# File 'lib/ore/generator.rb', line 38 def Generator.register_template(path) unless File.directory?(path) raise(StandardError,"#{path.dump} is must be a directory") end name = File.basename(path).to_sym Generator.templates[name] = path end |
.templates ⇒ Object
The templates registered with the generator.
25 26 27 |
# File 'lib/ore/generator.rb', line 25 def Generator.templates @templates ||= {} end |
Instance Method Details
#disable_template(name) ⇒ Object (protected)
Disables a template in the generator.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ore/generator.rb', line 149 def disable_template(name) name = name.to_sym unless (template_dir = Generator.templates[name]) say "Unknown template #{name}", :red exit -1 end self.source_paths.delete(template_dir) @templates.delete_if { |template| template.path == template_dir } @enabled_templates.delete(name) return true end |
#enable_template(name) ⇒ Object (protected)
Enables a template, adding it to the generator.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ore/generator.rb', line 112 def enable_template(name) name = name.to_sym return false if @enabled_templates.include?(name) unless (template_dir = Generator.templates[name]) say "Unknown template #{name}", :red exit -1 end new_template = Template::Directory.new(template_dir) # mark the template as enabled @enabled_templates << name # enable any other templates new_template.enable.each do |sub_template| enable_template(sub_template) end # append the new template to the end of the list, # to override previously loaded templates @templates << new_template # add the template directory to the source-paths self.source_paths << new_template.path return true end |
#enable_templates! ⇒ Object (protected)
Enables templates.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ore/generator.rb', line 167 def enable_templates! @templates = [] @enabled_templates = [] enable_template(@@base_template) enable_template(:bundler) if .bundler? enable_template(:jeweler_tasks) if .jeweler_tasks? enable_template(:ore_tasks) if .ore_tasks? enable_template(:rspec) if .rspec? enable_template(:test_unit) if .test_unit? enable_template(:yard) if .yard? enable_template(:rdoc) if .rdoc? # enable any additionally specified templates .templates.each { |name| enable_template(name) } # disable any previously enabled templates @templates.reverse_each do |template| template.disable.each { |name| disable_template(name) } end end |
#generate ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ore/generator.rb', line 80 def generate self.destination_root = path enable_templates! initialize_variables! say "Generating #{self.destination_root}", :green generate_directories! generate_files! if .git? in_root do unless File.directory?('.git') run 'git init' run 'git add .' run 'git commit -m "Initial commit."' end end end end |
#generate_directories! ⇒ Object (protected)
Creates directories listed in the template directories.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/ore/generator.rb', line 241 def generate_directories! generated = Set[] @templates.each do |template| template.each_directory do |dir| unless generated.include?(dir) path = interpolate(dir) empty_directory path generated << dir end end end end |
#generate_files! ⇒ Object (protected)
Copies static files and renders templates in the template directories.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/ore/generator.rb', line 259 def generate_files! generated = Set[] # iterate through the templates in reverse, so files in the templates # loaded last override the previously templates. @templates.reverse_each do |template| # copy in the static files first template.each_file(@markup) do |dest,file| unless generated.include?(dest) path = interpolate(dest) copy_file file, path generated << dest end end # then render the templates template.each_template(@markup) do |dest,file| unless generated.include?(dest) path = interpolate(dest) @current_template_dir = File.dirname(dest) template file, path @current_template_dir = nil generated << dest end end end end |
#initialize_variables! ⇒ Object (protected)
Initializes variables for the templates.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/ore/generator.rb', line 195 def initialize_variables! @project_dir = File.basename(destination_root) @name = (.name || @project_dir) @modules = modules_of(@name) @module_depth = @modules.length @module = @modules.last @namespace = namespace_of(@name) @namespace_dirs = namespace_dirs_of(@name) @namespace_path = namespace_path_of(@name) @namespace_dir = @namespace_dirs.last @version = .version @summary = .summary @description = .description @license = .license @email = .email @safe_email = @email.sub('@',' at ') if @email @homepage = (.homepage || "http://rubygems.org/gems/#{@name}") @authors = . @author = ..first @markup = if .markdown? :markdown elsif .textile? :textile else :rdoc end @date = Date.today @year = @date.year @month = @date.month @day = @date.day @templates.each do |template| template.variables.each do |name,value| instance_variable_set("@#{name}",value) end end end |