Ticket #8439: generator_merging.patch
| File generator_merging.patch, 4.5 kB (added by nicwilliams, 1 year ago) |
|---|
-
rails/railties/lib/rails_generator/commands.rb
old new 93 93 private 94 94 # Ask the user interactively whether to force collision. 95 95 def force_file_collision?(destination, src, dst, file_options = {}, &block) 96 $stdout.print "overwrite #{destination}? [Ynaqd ] "96 $stdout.print "overwrite #{destination}? [Ynaqdm] " 97 97 case $stdin.gets 98 98 when /d/i 99 99 Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp| 100 temp.write render_file(src, file_options, &block)100 temp.write tmp_file = render_file(src, file_options, &block) 101 101 temp.rewind 102 102 $stdout.puts `#{diff_cmd} #{dst} #{temp.path}` 103 103 end 104 puts "retrying"104 $stdout.puts "retrying" 105 105 raise 'retry diff' 106 when /m/i 107 Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp| 108 temp.write tmp_file = render_file(src, file_options, &block) 109 temp.rewind 110 options[:generated] = tmp_file 111 options[:merge] = `#{merge_cmd} #{temp.path} #{dst} #{temp.path}` # means the label is the original file path 112 $stdout.puts options[:merge] 113 end 114 $stdout.puts "merging #{spec.name}" 115 :merge 106 116 when /a/i 107 117 $stdout.puts "forcing #{spec.name}" 108 118 options[:collision] = :force … … 120 130 ENV['RAILS_DIFF'] || 'diff -u' 121 131 end 122 132 133 def merge_cmd 134 ENV['RAILS_MERGE'] || 'diff3 -m' 135 end 136 123 137 def render_template_part(template_options) 124 138 # Getting Sandbox to evaluate part template in it 125 139 part_binding = template_options[:sandbox].call.sandbox_binding … … 191 205 # file 'settings/server.yml', 'config/server.yml', :collision => :skip 192 206 # 193 207 # Collisions are handled by checking whether the destination file 194 # exists and either skipping the file, forcing overwrite, or asking208 # exists and either skipping the file, forcing overwrite, merging, or asking 195 209 # the user what to do. 196 210 def file(relative_source, relative_destination, file_options = {}, &block) 197 211 # Determine full paths for source and destination files. … … 220 234 # skip the file; otherwise, log our transgression and continue. 221 235 case choice 222 236 when :force then logger.force(relative_destination) 237 when :merge then logger.merge(relative_destination) 223 238 when :skip then return(logger.skip(relative_destination)) 224 239 else raise "Invalid collision choice: #{choice}.inspect" 225 240 end … … 232 247 # If we're pretending, back off now. 233 248 return if options[:pretend] 234 249 250 FileUtils.cp(destination, destination + ".original") if options[:merge] 251 235 252 # Write destination file with optional shebang. Yield for content 236 253 # if block given so templaters may render the source file. If a 237 254 # shebang is requested, replace the existing shebang or insert a 238 255 # new one. 239 256 File.open(destination, 'wb') do |dest| 240 dest.write render_file(source, file_options, &block)257 dest.write options[:merge] || render_file(source, file_options, &block) 241 258 end 259 260 if options[:merge] 261 File.open(destination + ".generated", 'wb') do |dest| 262 dest.write options[:generated] 263 end 264 else 265 # Optionally change permissions. 266 if file_options[:chmod] 267 FileUtils.chmod(file_options[:chmod], destination) 268 end 242 269 243 # Optionally change permissions. 244 if file_options[:chmod] 245 FileUtils.chmod(file_options[:chmod], destination) 270 # Optionally add file to subversion 271 system("svn add #{destination}") if options[:svn] 246 272 end 247 248 # Optionally add file to subversion249 system("svn add #{destination}") if options[:svn]250 273 end 251 274 252 275 # Checks if the source and the destination file are identical. If