Class: LicenseFinder::Diff

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

Class Method Summary collapse

Class Method Details

.compare(file1, file2) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/license_finder/diff.rb', line 6

def compare(file1, file2)
  p1 = Set.new(build_packages(file1))
  p2 = Set.new(build_packages(file2))

  added = p2.difference(p1).to_a
  removed = p1.difference(p2).to_a
  unchanged = p1.intersection(p2).to_a

  [].tap do |packages|
    unchanged.each do |package|
      package_previous = find_package(p1, package)
      package_current = find_package(p2, package)

      if package_current.licenses == package_previous.licenses
        packages << PackageDelta.unchanged(package_current, package_previous)
      else
        packages << PackageDelta.removed(package_previous)
        packages << PackageDelta.added(package_current)
      end
    end

    added.each    { |package| packages << PackageDelta.added(package) }
    removed.each  { |package| packages << PackageDelta.removed(package) }
  end
end