Class: ReaPack::Index::ConflictDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/reapack/index/cdetector.rb

Defined Under Namespace

Classes: Entry, Selector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConflictDetector

Returns a new instance of ConflictDetector.



29
30
31
# File 'lib/reapack/index/cdetector.rb', line 29

def initialize
  @buckets = {}
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



43
44
45
# File 'lib/reapack/index/cdetector.rb', line 43

def buckets
  @buckets
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
# File 'lib/reapack/index/cdetector.rb', line 39

def [](key)
  Selector.new key, self
end

#bucket(name) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
# File 'lib/reapack/index/cdetector.rb', line 45

def bucket(name)
  raise ArgumentError, 'bucket name is not a symbol' unless name.is_a? Symbol
  @buckets[name] ||= []
end

#clearObject



50
51
52
# File 'lib/reapack/index/cdetector.rb', line 50

def clear
  @buckets.clear
end

#initialize_clone(other) ⇒ Object



33
34
35
36
37
# File 'lib/reapack/index/cdetector.rb', line 33

def initialize_clone(other)
  super
  other.instance_variable_set :@buckets,
    Hash[@buckets.map {|k, v| [k, v.clone] }]
end

#load_xml(node) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/reapack/index/cdetector.rb', line 89

def load_xml(node)
  Category.find_all(node).each {|cat|
    Package.find_all(cat.node).each {|pkg|
      pkgroot = File.join(cat.name, pkg.name)

      pkg.versions.last&.children(Source::TAG)&.each {|src|
        type = src[:type] || pkg.type
        platform = src[:platform] || :all
        entry = Entry.new pkgroot, platform.to_sym

        if src[:file]
          entry.file = ReaPack::Index.expand(src[:file], cat.name)
        else
          entry.file = pkgroot
        end

        bucket(type.to_sym) << entry
      }
    }
  }
end

#resolve(bname, key = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/reapack/index/cdetector.rb', line 54

def resolve(bname, key = nil)
  dups = bucket(bname).group_by {|e| e.file }.values.select {|a| a.size > 1 }

  errors = dups.map {|a|
    packages = a.map {|e| e.key }.uniq
    next if key && !packages.include?(key)

    if packages.size == 1 || !key
      original = sort_platforms(a)[1]
      msg = "duplicate file '#{original.file}'"
    else
      original = sort_platforms(a.select {|e| e.key != key }).first
      msg = "'#{original.file}' conflicts with '#{original.key}'"
    end

    platforms = a.map {|e| e.platform }.uniq

    if platforms.size > 1
      # check platform inheritance
      platforms.any? {|p|
        break true unless Source::PLATFORMS[p]
        loop do
          p = Source::PLATFORMS[p] or break false
          break true if platforms.include? p
        end
      } or next
    end

    platform = original.platform
    platform == :all ? msg : "#{msg} on #{platform}"
  }.compact

  errors unless errors.empty?
end