Class/Module Index [+]

Quicksearch

Nanoc::CLI::Commands::Compile::DiffGenerator

Generates diffs for every output file written

Public Class Methods

enable_for?(command_runner) click to toggle source

@see Listener#enable_for?

# File lib/nanoc/cli/commands/compile.rb, line 68
def self.enable_for?(command_runner)
  command_runner.site.config[:enable_output_diff]
end

Public Instance Methods

start() click to toggle source

@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
stop() click to toggle source

@see Listener#stop

# File lib/nanoc/cli/commands/compile.rb, line 93
def stop
  super
  self.teardown_diffs
end

Protected Instance Methods

diff_strings(a, b) click to toggle source
# 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
generate_diff_for(rep, old_content, new_content) click to toggle source
# 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
setup_diffs() click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 100
def setup_diffs
  @diff_lock    = Mutex.new
  @diff_threads = []
  FileUtils.rm('output.diff') if File.file?('output.diff')
end
teardown_diffs() click to toggle source
# File lib/nanoc/cli/commands/compile.rb, line 106
def teardown_diffs
  @diff_threads.each { |t| t.join }
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.