Class: Librarian::SpecChangeSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, spec, lock) ⇒ SpecChangeSet

Returns a new instance of SpecChangeSet.

Raises:

  • (TypeError)


14
15
16
17
18
19
# File 'lib/librarian/spec_change_set.rb', line 14

def initialize(environment, spec, lock)
  self.environment = environment
  raise TypeError, "can't convert #{spec.class} into #{Spec}" unless Spec === spec
  raise TypeError, "can't convert #{lock.class} into #{Resolution}" unless Resolution === lock
  @spec, @lock = spec, lock
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



10
11
12
# File 'lib/librarian/spec_change_set.rb', line 10

def environment
  @environment
end

#lockObject (readonly)

Returns the value of attribute lock.



12
13
14
# File 'lib/librarian/spec_change_set.rb', line 12

def lock
  @lock
end

#specObject (readonly)

Returns the value of attribute spec.



12
13
14
# File 'lib/librarian/spec_change_set.rb', line 12

def spec
  @spec
end

Instance Method Details

#added_dependency_namesObject



75
76
77
# File 'lib/librarian/spec_change_set.rb', line 75

def added_dependency_names
  @added_dependency_names ||= spec_dependency_names - lock_dependency_names
end

#analyzeObject

Returns an array of those manifests from the previous spec which should be kept,

based on inspecting the new spec against the locked resolution from the previous spec.


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/librarian/spec_change_set.rb', line 142

def analyze
  @analyze ||= begin
    debug { "Analyzing spec and lock:" }

    if same?
      debug { "  Same!" }
      return lock.manifests
    end

    debug { "  Removed:" } ; removed_dependency_names.each { |name| debug { "    #{name}" } }
    debug { "  ExplicitRemoved:" } ; explicit_removed_dependency_names.each { |name| debug { "    #{name}" } }
    debug { "  Added:" } ; added_dependency_names.each { |name| debug { "    #{name}" } }
    debug { "  NonMatchingAdded:" } ; nonmatching_added_dependency_names.each { |name| debug { "    #{name}" } }
    debug { "  Changed:" } ; changed_dependency_names.each { |name| debug { "    #{name}" } }
    debug { "  DeepKeep:" } ; deep_keep_manifest_names.each { |name| debug { "    #{name}" } }
    debug { "  ShallowStrip:" } ; shallow_strip_manifest_names.each { |name| debug { "    #{name}" } }

    manifests = ManifestSet.new(lock_manifests)
    manifests.deep_keep!(deep_keep_manifest_names)
    manifests.shallow_strip!(shallow_strip_manifest_names)
    manifests.to_a
  end
end

#changed?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/librarian/spec_change_set.rb', line 25

def changed?
  !same?
end

#changed_dependency_namesObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/librarian/spec_change_set.rb', line 98

def changed_dependency_names
  @changed_dependency_names ||= common_dependency_names.reject do |name|
    spec_dependency = spec_dependency_index[name]
    lock_dependency = lock_dependency_index[name]
    lock_manifest = lock_manifests_index[name]
    same = true
    same &&= spec_dependency.satisfied_by?(lock_manifest)
    same &&= spec_dependency.source == lock_dependency.source
    same
  end.to_set
end

#common_dependency_namesObject



94
95
96
# File 'lib/librarian/spec_change_set.rb', line 94

def common_dependency_names
  @common_dependency_names ||= lock_dependency_names & spec_dependency_names
end

#deep_keep_manifest_namesObject



110
111
112
113
114
115
116
117
118
# File 'lib/librarian/spec_change_set.rb', line 110

def deep_keep_manifest_names
  @deep_keep_manifest_names ||= begin
    lock_dependency_names - (
      removed_dependency_names +
      changed_dependency_names +
      nonmatching_added_dependency_names
    )
  end
end

#explicit_removed_dependency_namesObject

A dependency which is deleted from the specfile will, in the general case,

be removed conservatively. This means it might not actually be removed.
But if the dependency originally declared a source which is now non-
default, it must be removed, even if another dependency has a transitive
dependency on the one that was removed (which is the scenario in which
a conservative removal would not remove it). In this case, we must also
remove it explicitly so that it can be re-resolved from the default
source.


68
69
70
71
72
73
# File 'lib/librarian/spec_change_set.rb', line 68

def explicit_removed_dependency_names
  @explicit_removed_dependency_names ||= removed_dependency_names.reject do |name|
    lock_manifest = lock_manifests_index[name]
    spec.sources.include?(lock_manifest.source)
  end.to_set
end

#inspectObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/librarian/spec_change_set.rb', line 126

def inspect
  Helpers.strip_heredoc(<<-INSPECT)
    <##{self.class.name}:
      Removed: #{removed_dependency_names.to_a.join(", ")}
      ExplicitRemoved: #{explicit_removed_dependency_names.to_a.join(", ")}
      Added: #{added_dependency_names.to_a.join(", ")}
      NonMatchingAdded: #{nonmatching_added_dependency_names.to_a.join(", ")}
      Changed: #{changed_dependency_names.to_a.join(", ")}
      DeepKeep: #{deep_keep_manifest_names.to_a.join(", ")}
      ShallowStrip: #{shallow_strip_manifest_names.to_a.join(", ")}
    >
  INSPECT
end

#lock_dependenciesObject



39
40
41
# File 'lib/librarian/spec_change_set.rb', line 39

def lock_dependencies
  @lock_dependencies ||= lock.dependencies
end

#lock_dependency_indexObject



45
46
47
# File 'lib/librarian/spec_change_set.rb', line 45

def lock_dependency_index
  @lock_dependency_index ||= Hash[lock_dependencies.map{|d| [d.name, d]}]
end

#lock_dependency_namesObject



42
43
44
# File 'lib/librarian/spec_change_set.rb', line 42

def lock_dependency_names
  @lock_dependency_names ||= Set.new(lock_dependencies.map{|d| d.name})
end

#lock_manifestsObject



49
50
51
# File 'lib/librarian/spec_change_set.rb', line 49

def lock_manifests
  @lock_manifests ||= lock.manifests
end

#lock_manifests_indexObject



52
53
54
# File 'lib/librarian/spec_change_set.rb', line 52

def lock_manifests_index
  @lock_manifests_index ||= ManifestSet.new(lock_manifests).to_hash
end

#nonmatching_added_dependency_namesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/librarian/spec_change_set.rb', line 79

def nonmatching_added_dependency_names
  @nonmatching_added_dependency_names ||= added_dependency_names.reject do |name|
    spec_dependency = spec_dependency_index[name]
    lock_manifest = lock_manifests_index[name]
    if lock_manifest
      matching = true
      matching &&= spec_dependency.satisfied_by?(lock_manifest)
      matching &&= spec_dependency.source == lock_manifest.source
      matching
    else
      false
    end
  end.to_set
end

#removed_dependency_namesObject



56
57
58
# File 'lib/librarian/spec_change_set.rb', line 56

def removed_dependency_names
  @removed_dependency_names ||= lock_dependency_names - spec_dependency_names
end

#same?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/librarian/spec_change_set.rb', line 21

def same?
  @same ||= spec.dependencies.sort_by{|d| d.name} == lock.dependencies.sort_by{|d| d.name}
end

#shallow_strip_manifest_namesObject



120
121
122
123
124
# File 'lib/librarian/spec_change_set.rb', line 120

def shallow_strip_manifest_names
  @shallow_strip_manifest_names ||= begin
    explicit_removed_dependency_names + changed_dependency_names
  end
end

#spec_dependenciesObject



29
30
31
# File 'lib/librarian/spec_change_set.rb', line 29

def spec_dependencies
  @spec_dependencies ||= spec.dependencies
end

#spec_dependency_indexObject



35
36
37
# File 'lib/librarian/spec_change_set.rb', line 35

def spec_dependency_index
  @spec_dependency_index ||= Hash[spec_dependencies.map{|d| [d.name, d]}]
end

#spec_dependency_namesObject



32
33
34
# File 'lib/librarian/spec_change_set.rb', line 32

def spec_dependency_names
  @spec_dependency_names ||= Set.new(spec_dependencies.map{|d| d.name})
end