Class: Origen::Utility::FileDiff::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/utility/file_diff.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file_name, target_file_name) ⇒ Processor

Returns a new instance of Processor



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/origen/utility/file_diff.rb', line 62

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

Instance Attribute Details

#sourceObject

Returns the value of attribute source



60
61
62
# File 'lib/origen/utility/file_diff.rb', line 60

def source
  @source
end

#source_outputObject

Returns the value of attribute source_output



61
62
63
# File 'lib/origen/utility/file_diff.rb', line 61

def source_output
  @source_output
end

#targetObject

Returns the value of attribute target



60
61
62
# File 'lib/origen/utility/file_diff.rb', line 60

def target
  @target
end

#target_outputObject

Returns the value of attribute target_output



61
62
63
# File 'lib/origen/utility/file_diff.rb', line 61

def target_output
  @target_output
end

Instance Method Details

#handle_block_added(size) ⇒ Object



85
86
87
88
89
90
# File 'lib/origen/utility/file_diff.rb', line 85

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) ⇒ Object



92
93
94
95
96
97
# File 'lib/origen/utility/file_diff.rb', line 92

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_matchedObject



75
76
77
78
# File 'lib/origen/utility/file_diff.rb', line 75

def handle_exactly_matched
  source_output.add_line(:unchanged, source)
  target_output.add_line(:unchanged, target)
end

#handle_line_changedObject



80
81
82
83
# File 'lib/origen/utility/file_diff.rb', line 80

def handle_line_changed
  source_output.add_line(:changed, source)
  target_output.add_line(:changed, target)
end

#process!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/origen/utility/file_diff.rb', line 99

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