10 # Here's a convenient way to get a handle on generator commands.
11 # Command.instance('destroy', my_generator) instantiates a Destroy
12 # delegate of my_generator ready to do your dirty work.
13 def self.instance(command, generator)
14 const_get(command.to_s.camelize).new(generator)
17 # Even more convenient access to commands. Include Commands in
18 # the generator Base class to get a nice #command instance method
19 # which returns a delegate for the requested command.
20 def self.included(base)
21 base.send(:define_method, :command) do |command|
22 Commands.instance(command, self)
27 # Generator commands delegate Rails::Generator::Base and implement
28 # a standard set of actions. Their behavior is defined by the way
29 # they respond to these actions: Create brings life; Destroy brings
30 # death; List passively observes.
32 # Commands are invoked by replaying (or rewinding) the generator's
33 # manifest of actions. See Rails::Generator::Manifest and
34 # Rails::Generator::Base#manifest method that generator subclasses
35 # are required to override.
37 # Commands allows generators to "plug in" invocation behavior, which
38 # corresponds to the GoF Strategy pattern.
39 class Base < DelegateClass(Rails::Generator::Base)
40 # Replay action manifest. RewindBase subclass rewinds manifest.
45 def dependency(generator_name, args, runtime_options = {})
46 logger.dependency(generator_name) do
47 self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke!
51 # Does nothing for all commands except Create.
52 def class_collisions(*class_names)
55 # Does nothing for all commands except Create.
60 def migration_directory(relative_path)
61 directory(@migration_directory = relative_path)
64 def existing_migrations(file_name)
65 Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/)
68 def migration_exists?(file_name)
69 not existing_migrations(file_name).empty?
72 def current_migration_number
73 Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path|
74 n = File.basename(file_path).split('_', 2).first.to_i
75 if n > max then n else max end
79 def next_migration_number
80 current_migration_number + 1
83 def next_migration_string(padding = 3)
84 "%.#{padding}d" % next_migration_number
87 def gsub_file(relative_destination, regexp, *args, &block)
88 path = destination_path(relative_destination)
89 content = File.read(path).gsub(regexp, *args, &block)
90 File.open(path, 'wb') { |file| file.write(content) }
94 # Ask the user interactively whether to force collision.
95 def force_file_collision?(destination, src, dst, file_options = {}, &block)
96 $stdout.print "overwrite #{destination}? (enter \"h\" for help) [Ynaqdh] "
97 case $stdin.gets.chomp
99 Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp|
100 temp.write render_file(src, file_options, &block)
102 $stdout.puts `#{diff_cmd} #{dst} #{temp.path}`
107 $stdout.puts "forcing #{spec.name}"
108 options[:collision] = :force
110 $stdout.puts "aborting #{spec.name}"
112 when /\An\z/i then :skip
113 when /\Ay\z/i then :force
117 n - no, do not overwrite
118 a - all, overwrite this and all others
120 d - diff, show the differences between the old and the new
121 h - help, show this help
130 ENV['RAILS_DIFF'] || 'diff -u'
133 def render_template_part(template_options)
134 # Getting Sandbox to evaluate part template in it
135 part_binding = template_options[:sandbox].call.sandbox_binding
136 part_rel_path = template_options[:insert]
137 part_path = source_path(part_rel_path)
139 # Render inner template within Sandbox binding
140 rendered_part = ERB.new(File.readlines(part_path).join, nil, '-').result(part_binding)
141 begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id])
142 end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id])
143 begin_mark + rendered_part + end_mark
146 def template_part_mark(name, id)
147 "<!--[#{name}:#{id}]-->\n"
151 # Base class for commands which handle generator actions in reverse, such as Destroy.
152 class RewindBase < Base
153 # Rewind action manifest.
155 manifest.rewind(self)
160 # Create is the premier generator command. It copies files, creates
161 # directories, renders templates, and more.
164 # Check whether the given class names are already taken by
165 # Ruby or Rails. In the future, expand to check other namespaces
166 # such as the rest of the user's app.
167 def class_collisions(*class_names)
168 class_names.flatten.each do |class_name|
169 # Convert to string to allow symbol arguments.
170 class_name = class_name.to_s
172 # Skip empty strings.
173 next if class_name.strip.empty?
175 # Split the class from its module nesting.
176 nesting = class_name.split('::')
179 # Extract the last Module in the nesting.
180 last = nesting.inject(Object) { |last, nest|
181 break unless last.const_defined?(nest)
185 # If the last Module exists, check whether the given
186 # class exists and raise a collision if so.
187 if last and last.const_defined?(name.camelize)
188 raise_class_collision(class_name)
193 # Copy a file from source to destination with collision checking.
195 # The file_options hash accepts :chmod and :shebang and :collision options.
196 # :chmod sets the permissions of the destination file:
197 # file 'config/empty.log', 'log/test.log', :chmod => 0664
198 # :shebang sets the #!/usr/bin/ruby line for scripts
199 # file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby'
200 # :collision sets the collision option only for the destination file:
201 # file 'settings/server.yml', 'config/server.yml', :collision => :skip
203 # Collisions are handled by checking whether the destination file
204 # exists and either skipping the file, forcing overwrite, or asking
205 # the user what to do.
206 def file(relative_source, relative_destination, file_options = {}, &block)
207 # Determine full paths for source and destination files.
208 source = source_path(relative_source)
209 destination = destination_path(relative_destination)
210 destination_exists = File.exists?(destination)
212 # If source and destination are identical then we're done.
213 if destination_exists and identical?(source, destination, &block)
214 return logger.identical(relative_destination)
217 # Check for and resolve file collisions.
218 if destination_exists
220 # Make a choice whether to overwrite the file. :force and
221 # :skip already have their mind made up, but give :ask a shot.
222 choice = case (file_options[:collision] || options[:collision]).to_sym #|| :ask
223 when :ask then force_file_collision?(relative_destination, source, destination, file_options, &block)
224 when :force then :force
225 when :skip then :skip
226 else raise "Invalid collision option: #{options[:collision].inspect}"
229 # Take action based on our choice. Bail out if we chose to
230 # skip the file; otherwise, log our transgression and continue.
232 when :force then logger.force(relative_destination)
233 when :skip then return(logger.skip(relative_destination))
234 else raise "Invalid collision choice: #{choice}.inspect"
237 # File doesn't exist so log its unbesmirched creation.
239 logger.create relative_destination
242 # If we're pretending, back off now.
243 return if options[:pretend]
245 # Write destination file with optional shebang. Yield for content
246 # if block given so templaters may render the source file. If a
247 # shebang is requested, replace the existing shebang or insert a
249 File.open(destination, 'wb') do |dest|
250 dest.write render_file(source, file_options, &block)
253 # Optionally change permissions.
254 if file_options[:chmod]
255 FileUtils.chmod(file_options[:chmod], destination)
258 # Optionally add file to subversion
259 system("svn add #{destination}") if options[:svn]
262 # Checks if the source and the destination file are identical. If
263 # passed a block then the source file is a template that needs to first
264 # be evaluated before being compared to the destination.
265 def identical?(source, destination, &block)
266 return false if File.directory? destination
267 source = block_given? ? File.open(source) {|sf| yield(sf)} : IO.read(source)
268 destination = IO.read(destination)
269 source == destination
272 # Generate a file for a Rails application using an ERuby template.
273 # Looks up and evaluates a template by name and writes the result.
275 # The ERB template uses explicit trim mode to best control the
276 # proliferation of whitespace in generated code. <%- trims leading
277 # whitespace; -%> trims trailing whitespace including one newline.
279 # A hash of template options may be passed as the last argument.
280 # The options accepted by the file are accepted as well as :assigns,
281 # a hash of variable bindings. Example:
282 # template 'foo', 'bar', :assigns => { :action => 'view' }
284 # Template is implemented in terms of file. It calls file with a
285 # block which takes a file handle and returns its rendered contents.
286 def template(relative_source, relative_destination, template_options = {})
287 file(relative_source, relative_destination, template_options) do |file|
288 # Evaluate any assignments in a temporary, throwaway binding.
289 vars = template_options[:assigns] || {}
291 vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
293 # Render the source file with the temporary binding.
294 ERB.new(file.read, nil, '-').result(b)
298 def complex_template(relative_source, relative_destination, template_options = {})
299 options = template_options.dup
300 options[:assigns] ||= {}
301 options[:assigns]['template_for_inclusion'] = render_template_part(template_options)
302 template(relative_source, relative_destination, options)
305 # Create a directory including any missing parent directories.
306 # Always directories which exist.
307 def directory(relative_path)
308 path = destination_path(relative_path)
309 if File.exists?(path)
310 logger.exists relative_path
312 logger.create relative_path
313 unless options[:pretend]
314 FileUtils.mkdir_p(path)
316 # Subversion doesn't do path adds, so we need to add
317 # each directory individually.
318 # So stack up the directory tree and add the paths to
319 # subversion in order without recursion.
321 stack=[relative_path]
322 until File.dirname(stack.last) == stack.last # dirname('.') == '.'
323 stack.push File.dirname(stack.last)
325 stack.reverse_each do |rel_path|
326 svn_path = destination_path(rel_path)
327 system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn'))
335 def readme(*relative_sources)
336 relative_sources.flatten.each do |relative_source|
337 logger.readme relative_source
338 puts File.read(source_path(relative_source)) unless options[:pretend]
342 # When creating a migration, it knows to find the first available file in db/migrate and use the migration.rb template.
343 def migration_template(relative_source, relative_destination, template_options = {})
344 migration_directory relative_destination
345 migration_file_name = template_options[:migration_file_name] || file_name
346 raise "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}" if migration_exists?(migration_file_name)
347 template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
350 def route_resources(*resources)
351 resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
352 sentinel = 'ActionController::Routing::Routes.draw do |map|'
354 logger.route "map.resources #{resource_list}"
355 unless options[:pretend]
356 gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
357 "#{match}\n map.resources #{resource_list}\n"
363 def render_file(path, options = {})
364 File.open(path, 'rb') do |file|
369 if shebang = options[:shebang]
370 content << "#!#{shebang}\n"
372 content << "line\n" if line !~ /^#!/
380 # Raise a usage error with an informative WordNet suggestion.
381 # Thanks to Florian Gross (flgr).
382 def raise_class_collision(class_name)
383 message = <<end_message
384 The name '#{class_name}' is reserved by Ruby on Rails.
385 Please choose an alternative and run this generator again.
387 if suggest = find_synonyms(class_name)
388 message << "\n Suggestions: \n\n"
389 message << suggest.join("\n")
391 raise UsageError, message
394 SYNONYM_LOOKUP_URI = "http://wordnet.princeton.edu/cgi-bin/webwn2.0?stage=2&word=%s&posnumber=1&searchtypenumber=2&senses=&showglosses=1"
396 # Look up synonyms on WordNet. Thanks to Florian Gross (flgr).
397 def find_synonyms(word)
401 open(SYNONYM_LOOKUP_URI % word) do |stream|
402 data = stream.read.gsub(" ", " ").gsub("<BR>", "")
403 data.scan(/^Sense \d+\n.+?\n\n/m)
412 # Undo the actions performed by a generator. Rewind the action
413 # manifest and attempt to completely erase the results of each action.
414 class Destroy < RewindBase
415 # Remove a file if it exists and is a file.
416 def file(relative_source, relative_destination, file_options = {})
417 destination = destination_path(relative_destination)
418 if File.exists?(destination)
419 logger.rm relative_destination
420 unless options[:pretend]
422 # If the file has been marked to be added
423 # but has not yet been checked in, revert and delete
424 if options[:svn][relative_destination]
425 system("svn revert #{destination}")
426 FileUtils.rm(destination)
428 # If the directory is not in the status list, it
429 # has no modifications so we can simply remove it
430 system("svn rm #{destination}")
433 FileUtils.rm(destination)
437 logger.missing relative_destination
442 # Templates are deleted just like files and the actions take the
443 # same parameters, so simply alias the file method.
444 alias_method :template, :file
446 # Remove each directory in the given path from right to left.
447 # Remove each subdirectory if it exists and is a directory.
448 def directory(relative_path)
449 parts = relative_path.split('/')
451 partial = File.join(parts)
452 path = destination_path(partial)
453 if File.exists?(path)
454 if Dir[File.join(path, '*')].empty?
456 unless options[:pretend]
458 # If the directory has been marked to be added
459 # but has not yet been checked in, revert and delete
460 if options[:svn][relative_path]
461 system("svn revert #{path}")
462 FileUtils.rmdir(path)
464 # If the directory is not in the status list, it
465 # has no modifications so we can simply remove it
466 system("svn rm #{path}")
469 FileUtils.rmdir(path)
473 logger.notempty partial
476 logger.missing partial
482 def complex_template(*args)
483 # nothing should be done here
486 # When deleting a migration, it knows to delete every file named "[0-9]*_#{file_name}".
487 def migration_template(relative_source, relative_destination, template_options = {})
488 migration_directory relative_destination
490 migration_file_name = template_options[:migration_file_name] || file_name
491 unless migration_exists?(migration_file_name)
492 puts "There is no migration named #{migration_file_name}"
497 existing_migrations(migration_file_name).each do |file_path|
498 file(relative_source, file_path, template_options)
502 def route_resources(*resources)
503 resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
504 look_for = "\n map.resources #{resource_list}\n"
505 logger.route "map.resources #{resource_list}"
506 gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
511 # List a generator's action manifest.
513 def dependency(generator_name, args, options = {})
514 logger.dependency "#{generator_name}(#{args.join(', ')}, #{options.inspect})"
517 def class_collisions(*class_names)
518 logger.class_collisions class_names.join(', ')
521 def file(relative_source, relative_destination, options = {})
522 logger.file relative_destination
525 def template(relative_source, relative_destination, options = {})
526 logger.template relative_destination
529 def complex_template(relative_source, relative_destination, options = {})
530 logger.template "#{options[:insert]} inside #{relative_destination}"
533 def directory(relative_path)
534 logger.directory "#{destination_path(relative_path)}/"
538 logger.readme args.join(', ')
541 def migration_template(relative_source, relative_destination, options = {})
542 migration_directory relative_destination
543 logger.migration_template file_name
546 def route_resources(*resources)
547 resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
548 logger.route "map.resources #{resource_list}"
552 # Update generator's action manifest.
553 class Update < Create
554 def file(relative_source, relative_destination, options = {})
555 # logger.file relative_destination
558 def template(relative_source, relative_destination, options = {})
559 # logger.template relative_destination
562 def complex_template(relative_source, relative_destination, template_options = {})
565 dest_file = destination_path(relative_destination)
566 source_to_update = File.readlines(dest_file).join
568 logger.missing relative_destination
572 logger.refreshing "#{template_options[:insert].gsub(/\.erb/,'')} inside #{relative_destination}"
574 begin_mark = Regexp.quote(template_part_mark(template_options[:begin_mark], template_options[:mark_id]))
575 end_mark = Regexp.quote(template_part_mark(template_options[:end_mark], template_options[:mark_id]))
577 # Refreshing inner part of the template with freshly rendered part.
578 rendered_part = render_template_part(template_options)
579 source_to_update.gsub!(/#{begin_mark}.*?#{end_mark}/m, rendered_part)
581 File.open(dest_file, 'w') { |file| file.write(source_to_update) }
584 def directory(relative_path)
585 # logger.directory "#{destination_path(relative_path)}/"