Class: Ore::Generator
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Ore::Generator
- Includes:
- Actions, Naming, Template::Helpers, Template::Interpolations, Thor::Actions
- Defined in:
- lib/ore/generator.rb
Constant Summary
Constants included from Template::Helpers
Constants included from Naming
Naming::BIN_DIR, Naming::COMMON_ABBREVIATIONS, Naming::COMMON_NAMESPACES, Naming::DATA_DIR, Naming::EXT_DIR, Naming::IGNORE_NAMESPACES, Naming::LIB_DIR, Naming::PKG_DIR, Naming::SPEC_DIR, Naming::TEST_DIR
Instance Attribute Summary collapse
-
#disabled_templates ⇒ Object
readonly
The disabled templates.
-
#enabled_templates ⇒ Object
readonly
The enabled templates.
-
#generated_dirs ⇒ Object
readonly
The generated directories.
-
#generated_files ⇒ Object
readonly
The generated files.
-
#templates ⇒ Object
readonly
The loaded templates.
Class Method Summary collapse
-
.generator_option(name, options = {}) ⇒ Object
Defines a generator option.
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
Generates a new project.
-
#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_scm! ⇒ Object
protected
Initializes the project repository and commits all files.
-
#initialize_variables! ⇒ Object
protected
Initializes variables for the templates.
Methods included from Template::Helpers
#bin?, #bundler?, #bundler_tasks?, #enabled?, #gem_package_task?, #git?, #hg?, #includes, #indent, #jeweler_tasks?, #rdoc?, #rspec?, #rubygems_tasks?, #svn?, #test_unit?, #yaml_escape, #yard?
Methods included from Template::Interpolations
Methods included from Naming
#module_of, #modules_of, #names_in, #namespace_dirs_of, #namespace_of, #namespace_path_of, #underscore
Methods included from Actions
#generate_dir, #generate_file, #run
Instance Attribute Details
#disabled_templates ⇒ Object (readonly)
The disabled templates.
76 77 78 |
# File 'lib/ore/generator.rb', line 76 def disabled_templates @disabled_templates end |
#enabled_templates ⇒ Object (readonly)
The enabled templates.
73 74 75 |
# File 'lib/ore/generator.rb', line 73 def enabled_templates @enabled_templates end |
#generated_dirs ⇒ Object (readonly)
The generated directories.
82 83 84 |
# File 'lib/ore/generator.rb', line 82 def generated_dirs @generated_dirs end |
#generated_files ⇒ Object (readonly)
The generated files.
85 86 87 |
# File 'lib/ore/generator.rb', line 85 def generated_files @generated_files end |
#templates ⇒ Object (readonly)
The loaded templates.
79 80 81 |
# File 'lib/ore/generator.rb', line 79 def templates @templates end |
Class Method Details
.generator_option(name, options = {}) ⇒ Object
Defines a generator option.
27 28 29 30 31 |
# File 'lib/ore/generator.rb', line 27 def self.generator_option(name,={}) default = .fetch(:default,Config.[name]) class_option(name,.merge(default: default)) end |
Instance Method Details
#disable_template(name) ⇒ Object (protected)
Disables a template in the generator.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ore/generator.rb', line 154 def disable_template(name) name = name.to_sym return false if @disabled_templates.include?(name) if (template_dir = Template.templates[name]) source_paths.delete(template_dir) @templates.delete_if { |template| template.path == template_dir } @enabled_templates.delete(name) end @disabled_templates << name return true end |
#enable_template(name) ⇒ Object (protected)
Enables a template, adding it to the generator.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ore/generator.rb', line 117 def enable_template(name) name = name.to_sym return false if @enabled_templates.include?(name) unless (template_dir = Template.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.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ore/generator.rb', line 173 def enable_templates! @templates = [] @enabled_templates = Set[] @disabled_templates = Set[] enable_template :gem # enable the default templates first Options::DEFAULT_TEMPLATES.each do |name| if (Template.template?(name) && [name]) enable_template(name) end end # enable the templates specified by option .each do |name,value| if (Template.template?(name) && value) enable_template(name) end end # 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
Generates a new project.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ore/generator.rb', line 90 def generate self.destination_root = path enable_templates! initialize_variables! extend Template::Helpers::MARKUP.fetch(@markup) unless .quiet? say "Generating #{self.destination_root}", :green end generate_directories! generate_files! initialize_scm! end |
#generate_directories! ⇒ Object (protected)
Creates directories listed in the template directories.
304 305 306 307 308 309 310 |
# File 'lib/ore/generator.rb', line 304 def generate_directories! @templates.each do |template| template.each_directory do |dir| generate_dir dir end end end |
#generate_files! ⇒ Object (protected)
Copies static files and renders templates in the template directories.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/ore/generator.rb', line 315 def generate_files! # 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| generate_file dest, file end # then render the templates template.each_template(@markup) do |dest,file| generate_file dest, file, template: true end end @generated_files.each_value do |path| dir = path.split(File::SEPARATOR,2).first if dir == 'bin' chmod path, 0755 end end end |
#initialize_scm! ⇒ Object (protected)
Initializes the project repository and commits all files.
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/ore/generator.rb', line 344 def initialize_scm! in_root do case @scm when :git unless File.directory?('.git') run 'git init' run 'git add .' run 'git commit -m "Initial commit."' end when :hg unless File.directory?('.hg') run 'hg init' run 'hg add .' run 'hg commit -m "Initial commit."' end when :svn @ignore.each do |pattern| run "svn propset svn:ignore #{pattern.dump}" end run 'svn add .' run 'svn commit -m "Initial commit."' end end end |
#initialize_variables! ⇒ Object (protected)
Initializes variables for the templates.
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/ore/generator.rb', line 206 def initialize_variables! @root = destination_root @project_dir = File.basename(@root) @name = (.name || @project_dir) @scm = if File.directory?(File.join(@root,'.git')) then :git elsif File.directory?(File.join(@root,'.hg')) then :hg elsif File.directory?(File.join(@root,'.svn')) then :svn elsif .hg? then :hg elsif .git? then :git end case @scm when :git @scm_user = `git config user.name`.chomp @scm_email = `git config user.email`.chomp @github_user = `git config github.user`.chomp when :hg user_email = `hg showconfig ui.username`.chomp user_email.scan(/([^<]+)\s+<([^>]+)>/) do |(user,email)| @scm_user, @scm_email = user, email end end @modules = modules_of(@name) @module_depth = @modules.length @module = @modules.last @namespace = .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 @authors = if . || . [*., *.] else [@scm_user || ENV['USERNAME'] || ENV['USER'].capitalize] end @author = @authors.first @email = (.email || @scm_email) @safe_email = @email.sub('@',' at ') if @email @homepage = if .homepage .homepage elsif !(@github_user.nil? || @github_user.empty?) "https://github.com/#{@github_user}/#{@name}#readme" else "https://rubygems.org/gems/#{@name}" end @uri = URI(@homepage) @bug_tracker = case @uri.host when 'github.com' "https://#{@uri.host}#{@uri.path}/issues" end @markup = if .markdown? then :markdown elsif .textile? then :textile elsif .markup? then .markup.to_sym end @markup_ext = Template::Markup::EXT.fetch(@markup) do say "Unknown markup: #{@markup}", :red exit -1 end @date = Date.today @year = @date.year @month = @date.month @day = @date.day @ignore = SortedSet[] @dependencies = {} @development_dependencies = {} @templates.each do |template| @ignore.merge(template.ignore) @dependencies.merge!(template.dependencies) @development_dependencies.merge!(template.development_dependencies) template.variables.each do |name,value| instance_variable_set("@#{name}",value) end end @generated_dirs = {} @generated_files = {} end |