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.



65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#source_outputObject

Returns the value of attribute source_output.



63
64
65
# File 'lib/origen/utility/file_diff.rb', line 63

def source_output
  @source_output
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

#target_outputObject

Returns the value of attribute target_output.



63
64
65
# File 'lib/origen/utility/file_diff.rb', line 63

def target_output
  @target_output
end

Instance Method Details

#handle_block_added(size) ⇒ Object



88
89
90
91
92
93
# 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) ⇒ Object



95
96
97
98
99
100
# 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_matchedObject



78
79
80
81
# 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_changedObject



83
84
85
86
# 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!Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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