Class: DepAnalyzer

Inherits:
Cache show all
Defined in:
lib/dep_analyzer.rb

Overview

Abstract class to analyze dependencies. Intended to be subclassed for a given dependency system (eg rubygems). Subclasses must implement #deps, #installed, and #outdated at the very least.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Cache

#cache

Constructor Details

#initializeDepAnalyzer

:nodoc:



111
112
113
114
# File 'lib/dep_analyzer.rb', line 111

def initialize # :nodoc:
  super ".#{self.class}.cache"
  @g = Graph.new
end

Instance Attribute Details

#gObject

:nodoc:



109
110
111
# File 'lib/dep_analyzer.rb', line 109

def g
  @g
end

Instance Method Details

#decorateObject

Allows your subclass to add extra fancy stuff to the graph when analysis is finished.



120
121
122
# File 'lib/dep_analyzer.rb', line 120

def decorate
  # nothing to do by default
end

#deps(port) ⇒ Object

Return the dependencies for a given port.

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/dep_analyzer.rb', line 127

def deps port
  raise NotImplementedError, "subclass responsibility"
end

#installedObject

Return all installed items on the system.

Raises:

  • (NotImplementedError)


134
135
136
# File 'lib/dep_analyzer.rb', line 134

def installed
  raise NotImplementedError, "subclass responsibility"
end

#outdatedObject

Return all outdated items currently installed.

Raises:

  • (NotImplementedError)


141
142
143
# File 'lib/dep_analyzer.rb', line 141

def outdated
  raise NotImplementedError, "subclass responsibility"
end

#run(argv = ARGV) ⇒ Object

Do the actual work.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dep_analyzer.rb', line 148

def run(argv = ARGV)
  setup

  ports = {}
  installed.each do |port|
    ports[port] = nil
  end

  old = {}
  outdated.each do |port|
    old[port] = nil
  end

  all_ports = ports.keys

  ports.each_key do |port|
    deps = self.deps(port)
    # remove things that don't intersect with installed list
    deps -= (deps - all_ports)
    deps.each do |dep|
      g[port] << dep
    end
    ports[port] = deps
  end

  blue   = g.color "blue"
  purple = g.color "purple4"
  red    = g.color "red"

  indies = ports.keys - ports.minvert.keys
  indies.each do |k|
    blue << g[k]
  end

  old.each do |k,v|
    if indies.include? k then
      purple << g[k]
    else
      red << g[k]
    end
  end

  decorate

  puts "Looks like you can nuke:\n\t#{indies.sort.join("\n\t")}"

  unless argv.empty? then
    argv.each do |pkg|
      hits = ports.transitive[pkg]
      sorted = ports.tsort.reverse
      topo = [pkg] + sorted.select { |o| hits.include? o }
      prune = ports.dup
      topo.each do |k|
        prune.delete(k)
      end
      topo -= prune.values.flatten.uniq

      puts topo.join(' ')
    end
  end

  g
end

#setupObject

Allows subclasses to do any preparation before the run.



215
216
217
# File 'lib/dep_analyzer.rb', line 215

def setup
  # nothing to do by default
end