Class: Ore::Generator

Inherits:
Thor::Group
  • Object
show all
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

Instance Method Summary collapse

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

#interpolate

Class Method Details

.register_template(path) ⇒ Object

Registers a template with the generator.

Parameters:

  • path (String)

    The path to the template.

Raises:

  • (StandardError)

    The given path was not a directory.



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

.templatesObject

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.

Parameters:

  • name (Symbol, String)

    The name of the template.

Since:

  • 0.4.0



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.

Parameters:

  • name (Symbol, String)

    The name of the template to add.

Since:

  • 0.4.0



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 options.bundler?
  enable_template(:jeweler_tasks) if options.jeweler_tasks?
  enable_template(:ore_tasks) if options.ore_tasks?
  
  enable_template(:rspec) if options.rspec?
  enable_template(:test_unit) if options.test_unit?

  enable_template(:yard) if options.yard?
  enable_template(:rdoc) if options.rdoc?

  # 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



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 options.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 = (options.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 = options.version
  @summary = options.summary
  @description = options.description
  @license = options.license
  @email = options.email
  @safe_email = @email.sub('@',' at ') if @email
  @homepage = (options.homepage || "http://rubygems.org/gems/#{@name}")
  @authors = options.authors
  @author = options.authors.first

  @markup = if options.markdown?
              :markdown
            elsif options.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