Generates diffs for every output file written
@see Listener#start
# File lib/nanoc/cli/commands/compile.rb, line 73 def start require 'tempfile' self.setup_diffs old_contents = {} Nanoc::NotificationCenter.on(:will_write_rep) do |rep, snapshot| path = rep.raw_path(:snapshot => snapshot) old_contents[rep] = File.file?(path) ? File.read(path) : nil end Nanoc::NotificationCenter.on(:rep_written) do |rep, path, is_created, is_modified| if !rep.binary? new_contents = File.file?(path) ? File.read(path) : nil if old_contents[rep] && new_contents generate_diff_for(rep, old_contents[rep], new_contents) end old_contents.delete(rep) end end end
@see Listener#stop
# File lib/nanoc/cli/commands/compile.rb, line 93 def stop super self.teardown_diffs end
# File lib/nanoc/cli/commands/compile.rb, line 126 def diff_strings(a, b) require 'open3' # Create files Tempfile.open('old') do |old_file| Tempfile.open('new') do |new_file| # Write files old_file.write(a) old_file.flush new_file.write(b) new_file.flush # Diff cmd = [ 'diff', '-u', old_file.path, new_file.path ] Open3.popen3(*cmd) do |stdin, stdout, stderr| result = stdout.read return (result == '' ? nil : result) end end end rescue Errno::ENOENT warn 'Failed to run `diff`, so no diff with the previously compiled ' 'content will be available.' nil end
# File lib/nanoc/cli/commands/compile.rb, line 110 def generate_diff_for(rep, old_content, new_content) return if old_content == new_content @diff_threads << Thread.new do # Generate diff diff = diff_strings(old_content, new_content) diff.sub!(/^--- .*/, '--- ' + rep.raw_path) diff.sub!(/^\+\+\+ .*/, '+++ ' + rep.raw_path) # Write diff @diff_lock.synchronize do File.open('output.diff', 'a') { |io| io.write(diff) } end end end
Generated with the Darkfish Rdoc Generator 2.