Class: Ore::Generator

Inherits:
Thor::Group
  • Object
show all
Includes:
Actions, Naming, Options, Template::Helpers, Template::Interpolations, Thor::Actions
Defined in:
lib/ore/generator.rb

Constant Summary

Constants included from Naming

Naming::BIN_DIR, Naming::COMMON_NAMESPACES, Naming::DATA_DIR, Naming::EXT_DIR, Naming::IGNORE_NAMESPACES, Naming::LIB_DIR, Naming::NAMESPACE_ACRONYMS, Naming::PKG_DIR, Naming::SPEC_DIR, Naming::TEST_DIR

Constants included from Options

Options::DEFAULT_DESCRIPTION, Options::DEFAULT_LICENSE, Options::DEFAULT_SUMMARY, Options::DEFAULT_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#interpolate

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

Methods included from Options

defaults, included

Instance Attribute Details

#disabled_templatesObject (readonly)

The disabled templates.



26
27
28
# File 'lib/ore/generator.rb', line 26

def disabled_templates
  @disabled_templates
end

#enabled_templatesObject (readonly)

The enabled templates.



23
24
25
# File 'lib/ore/generator.rb', line 23

def enabled_templates
  @enabled_templates
end

#generated_dirsObject (readonly)

The generated directories.



32
33
34
# File 'lib/ore/generator.rb', line 32

def generated_dirs
  @generated_dirs
end

#generated_filesObject (readonly)

The generated files.



35
36
37
# File 'lib/ore/generator.rb', line 35

def generated_files
  @generated_files
end

#templatesObject (readonly)

The loaded templates.



29
30
31
# File 'lib/ore/generator.rb', line 29

def templates
  @templates
end

Instance Method Details

#disable_template(name) ⇒ Object (protected)

Disables a template in the generator.

Parameters:

  • name (Symbol, String)

    The name of the template.

Since:

  • 0.4.0



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ore/generator.rb', line 155

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.

Parameters:

  • name (Symbol, String)

    The name of the template to add.

Since:

  • 0.4.0



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
145
# File 'lib/ore/generator.rb', line 118

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.



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
202
# File 'lib/ore/generator.rb', line 174

def enable_templates!
  @templates          = []
  @enabled_templates  = Set[]
  @disabled_templates = Set[]
  
  enable_template :base

  # enable the default templates first
  Options.defaults.each_key do |name|
    if (Template.template?(name) && options[name])
      enable_template(name)
    end
  end

  # enable the templates specified by option
  options.each do |name,value|
    if (Template.template?(name) && value)
      enable_template(name)
    end
  end

  # enable any additionally specified templates
  options.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

#generateObject

Generates a new project.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ore/generator.rb', line 70

def generate
  self.destination_root = path

  enable_templates!
  initialize_variables!

  unless options.quiet?
    say "Generating #{self.destination_root}", :green
  end

  generate_directories!
  generate_files!

  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

#generate_directories!Object (protected)

Creates directories listed in the template directories.



297
298
299
300
301
302
303
# File 'lib/ore/generator.rb', line 297

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.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/ore/generator.rb', line 308

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_variables!Object (protected)

Initializes variables for the templates.



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
# File 'lib/ore/generator.rb', line 207

def initialize_variables!
  @root        = destination_root
  @project_dir = File.basename(@root)
  @name        = (options.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 options.hg?                              then :hg
         elsif options.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_of(@name)
  @namespace_dirs = namespace_dirs_of(@name)
  @namespace_path = namespace_path_of(@name)
  @namespace_dir  = @namespace_dirs.last

  @version     = options.version
  @summary     = options.summary
  @description = options.description
  @license     = options.license

  @authors     = (options.authors || [@scm_user || ENV['USER']])
  @author      = @authors.first

  @email       = (options.email || @scm_email)
  @safe_email  = @email.sub('@',' at ') if @email

  @homepage    = if options.homepage
                   options.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, @markup_ext = if    options.markdown? then [:markdown, 'md']
                         elsif options.textile?  then [:textile, 'tt']
                         else                         [:rdoc, 'rdoc']
                         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