class Origen::Utility::FileDiff::Processor
Diff
Processor
(Origen::Utility::Processor) provides an easy way to diff the contents of two files and display the differences as an HTML file or a TXT file. Very basic functionality, but can be expanded to add more features in the future. Comments are not ignored for now (maybe a future enhancement) Each difference is displayed in a different color in the HTML page Legend:
- New: Light Green - Modified: Light Gray - Deleted: Pink
Usage:
processor = Origen::Utility::FileDiff::Processor.new("#{Origen.root}/left.txt", "#{Origen.root}/right.txt")
To Generate a HTML file (diff.html) showing the differences
Origen::Utility::FileDiff::Formatter::Html.new(processor.process!, "#{Origen.root}/diff.html").format
To Generate a TXT file (diff.txt) showing the differences
Origen::Utility::FileDiff::Formatter::Text.new(processor.process!, "#{Origen.root}/diff.txt").format
Attributes
source[RW]
source_output[RW]
target[RW]
target_output[RW]
Public Class Methods
new(source_file_name, target_file_name)
click to toggle source
# File lib/origen/utility/file_diff.rb, line 65 def initialize(source_file_name, target_file_name) self.source = InputFile.new self.target = InputFile.new self.source_output = OutputFile.new self.target_output = OutputFile.new IO.readlines(source_file_name).each do |line| source << line end IO.readlines(target_file_name).each do |line| target << line end end
Public Instance Methods
handle_block_added(size)
click to toggle source
# File lib/origen/utility/file_diff.rb, line 88 def handle_block_added(size) size.times do source_output.add_line(:added) # Empty line in the left side of the diff target_output.add_line(:added, target) end end
handle_block_deleted(size)
click to toggle source
# File lib/origen/utility/file_diff.rb, line 95 def handle_block_deleted(size) size.times do source_output.add_line(:deleted, source) target_output.add_line(:deleted) # Empty line in the right side of the diff end end
handle_exactly_matched()
click to toggle source
# File lib/origen/utility/file_diff.rb, line 78 def handle_exactly_matched source_output.add_line(:unchanged, source) target_output.add_line(:unchanged, target) end
handle_line_changed()
click to toggle source
# File lib/origen/utility/file_diff.rb, line 83 def handle_line_changed source_output.add_line(:changed, source) target_output.add_line(:changed, target) end
process!()
click to toggle source
# File lib/origen/utility/file_diff.rb, line 102 def process! while source.pointer < source.size && target.pointer < target.size matched = source.find_current_line_in(target) if matched if matched > target.pointer deleted = target.find_current_line_in(source) handle_block_deleted(deleted - source.pointer) if deleted end handle_block_added(matched - target.pointer) handle_exactly_matched else found = target.find_current_line_in(source) if found handle_block_deleted(found - source.pointer) else handle_line_changed end end end handle_block_deleted(source.size - source.pointer) handle_block_added(target.size - target.pointer) self end