提交 868d9f5a 编写于 作者: J Jeremy Kemper

Generator can show diff on file collision to help you decide whether to skip...

Generator can show diff on file collision to help you decide whether to skip or overwrite. Closes #6364.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5397 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 4bd64363
*SVN* *SVN*
* Generator can show diff on file collision to help you decide whether to skip or overwrite. #6364 [jeffw, Jeremy Kemper]
* Generated directories are recursively svn added, like mkdir -p. #6416 [NeilW] * Generated directories are recursively svn added, like mkdir -p. #6416 [NeilW]
* resource and scaffold_resource generators add a restful route to config/routes.rb [Jeremy Kemper] * resource and scaffold_resource generators add a restful route to config/routes.rb [Jeremy Kemper]
......
require 'delegate' require 'delegate'
require 'optparse' require 'optparse'
require 'fileutils' require 'fileutils'
require 'tempfile'
require 'erb' require 'erb'
module Rails module Rails
...@@ -91,9 +92,17 @@ def gsub_file(relative_destination, regexp, *args, &block) ...@@ -91,9 +92,17 @@ def gsub_file(relative_destination, regexp, *args, &block)
private private
# Ask the user interactively whether to force collision. # Ask the user interactively whether to force collision.
def force_file_collision?(destination) def force_file_collision?(destination, src, dst, file_options = {}, &block)
$stdout.print "overwrite #{destination}? [Ynaq] " $stdout.print "overwrite #{destination}? [Ynaqd] "
case $stdin.gets case $stdin.gets
when /d/i
Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
temp.write render_file(src, file_options, &block)
temp.rewind
$stdout.puts `#{diff_cmd} #{dst} #{temp.path}`
end
puts "retrying"
raise 'retry diff'
when /a/i when /a/i
$stdout.puts "forcing #{spec.name}" $stdout.puts "forcing #{spec.name}"
options[:collision] = :force options[:collision] = :force
...@@ -107,6 +116,10 @@ def force_file_collision?(destination) ...@@ -107,6 +116,10 @@ def force_file_collision?(destination)
retry retry
end end
def diff_cmd
ENV['RAILS_DIFF'] || 'diff -u'
end
def render_template_part(template_options) def render_template_part(template_options)
# Getting Sandbox to evaluate part template in it # Getting Sandbox to evaluate part template in it
part_binding = template_options[:sandbox].call.sandbox_binding part_binding = template_options[:sandbox].call.sandbox_binding
...@@ -197,7 +210,7 @@ def file(relative_source, relative_destination, file_options = {}, &block) ...@@ -197,7 +210,7 @@ def file(relative_source, relative_destination, file_options = {}, &block)
# Make a choice whether to overwrite the file. :force and # Make a choice whether to overwrite the file. :force and
# :skip already have their mind made up, but give :ask a shot. # :skip already have their mind made up, but give :ask a shot.
choice = case (file_options[:collision] || options[:collision]).to_sym #|| :ask choice = case (file_options[:collision] || options[:collision]).to_sym #|| :ask
when :ask then force_file_collision?(relative_destination) when :ask then force_file_collision?(relative_destination, source, destination, file_options, &block)
when :force then :force when :force then :force
when :skip then :skip when :skip then :skip
else raise "Invalid collision option: #{options[:collision].inspect}" else raise "Invalid collision option: #{options[:collision].inspect}"
...@@ -223,20 +236,8 @@ def file(relative_source, relative_destination, file_options = {}, &block) ...@@ -223,20 +236,8 @@ def file(relative_source, relative_destination, file_options = {}, &block)
# if block given so templaters may render the source file. If a # if block given so templaters may render the source file. If a
# shebang is requested, replace the existing shebang or insert a # shebang is requested, replace the existing shebang or insert a
# new one. # new one.
File.open(destination, 'wb') do |df| File.open(destination, 'wb') do |dest|
File.open(source, 'rb') do |sf| dest.write render_file(source, file_options, &block)
if block_given?
df.write(yield(sf))
else
if file_options[:shebang]
df.puts("#!#{file_options[:shebang]}")
if line = sf.gets
df.puts(line) if line !~ /^#!/
end
end
df.write(sf.read)
end
end
end end
# Optionally change permissions. # Optionally change permissions.
...@@ -347,6 +348,23 @@ def route_resources(*resources) ...@@ -347,6 +348,23 @@ def route_resources(*resources)
end end
private private
def render_file(path, options = {})
File.open(path, 'rb') do |file|
if block_given?
yield file
else
content = ''
if shebang = options[:shebang]
content << "#!#{shebang}\n"
if line = file.gets
content << "line\n" if line !~ /^#!/
end
end
content << file.read
end
end
end
# Raise a usage error with an informative WordNet suggestion. # Raise a usage error with an informative WordNet suggestion.
# Thanks to Florian Gross (flgr). # Thanks to Florian Gross (flgr).
def raise_class_collision(class_name) def raise_class_collision(class_name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册