Class: SmartDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_one, file_two) ⇒ SmartDiff

Create a SmartDiff object which will create diff of two source files based on AST generated by JRubyParser.

Parameters:

  • file_one (String)

    The path to the first source file.

  • file_two (String)

    The path to the second source file.



19
20
21
22
23
# File 'lib/smart_diff.rb', line 19

def initialize(file_one, file_two)
  @file_one = file_one
  @file_two = file_two
  @node_diff = diff()
end

Instance Attribute Details

#code_oneObject

Returns the value of attribute code_one.



8
9
10
# File 'lib/smart_diff.rb', line 8

def code_one
  @code_one
end

#code_twoObject

Returns the value of attribute code_two.



8
9
10
# File 'lib/smart_diff.rb', line 8

def code_two
  @code_two
end

#file_oneObject

Returns the value of attribute file_one.



8
9
10
# File 'lib/smart_diff.rb', line 8

def file_one
  @file_one
end

#file_twoObject

Returns the value of attribute file_two.



8
9
10
# File 'lib/smart_diff.rb', line 8

def file_two
  @file_two
end

#node_diffObject

Returns the value of attribute node_diff.



8
9
10
# File 'lib/smart_diff.rb', line 8

def node_diff
  @node_diff
end

Instance Method Details

#diffObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/smart_diff.rb', line 38

def diff()
  @code_one = read(@file_one)
  @code_two = read(@file_two)
  if @code_one && @code_two
    nodeA = parse(@code_one, @file_one)
    nodeB = parse(@code_two, @file_two)
    nd = org.jrubyparser.util.diff.NodeDiff.new(nodeA, @code_one, nodeB, @code_two)
    nd.deep_diff
  end
end

#parse(code_to_parse, file_name) ⇒ Object



34
35
36
# File 'lib/smart_diff.rb', line 34

def parse(code_to_parse, file_name)
  JRubyParser.parse(code_to_parse, { :filename => file_name })
end

#read(file_name) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/smart_diff.rb', line 25

def read(file_name)
  path = Pathname.new(file_name).expand_path
  if path.exist?
    File.read path
  else
    raise "#{path} not found. Check the path."
  end
end