Class: Rip::PackageManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ PackageManager

Returns a new instance of PackageManager.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rip/package_manager.rb', line 45

def initialize(env = nil)
  @env = env || Rip::Env.active
  load

  # key is the package name, value is the current
  # installed version
  @versions ||= {}

  # key is the package name, value is an array of
  # libraries it depend on
  @dependents ||= {}

  # key is the package name, value is an array of
  # libraries that depend on it
  @dependencies ||= {}

  # key is the package name, value is the source
  @sources ||= {}

  # key is the package name, value is the installed
  # files
  @files ||= {}
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



43
44
45
# File 'lib/rip/package_manager.rb', line 43

def dependencies
  @dependencies
end

#dependentsObject (readonly)

Returns the value of attribute dependents.



43
44
45
# File 'lib/rip/package_manager.rb', line 43

def dependents
  @dependents
end

#envObject (readonly)

Returns the value of attribute env.



43
44
45
# File 'lib/rip/package_manager.rb', line 43

def env
  @env
end

#sourcesObject (readonly)

Returns the value of attribute sources.



43
44
45
# File 'lib/rip/package_manager.rb', line 43

def sources
  @sources
end

#versionsObject (readonly)

Returns the value of attribute versions.



43
44
45
# File 'lib/rip/package_manager.rb', line 43

def versions
  @versions
end

Instance Method Details

#add_files(name, file_list = []) ⇒ Object



143
144
145
146
147
# File 'lib/rip/package_manager.rb', line 143

def add_files(name, file_list = [])
  @files[name] ||= []
  @files[name].concat file_list
  save
end

#add_package(package, parent = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rip/package_manager.rb', line 111

def add_package(package, parent = nil)
  name = package.name
  version = package.version

  if @versions.has_key?(name) && @versions[name] != version
    raise VersionConflict.new(name, version, parent, @versions[name], @dependents[name].to_a)
  end

  if parent && parent.meta_package? && parent.actual_package != package
    parent = parent.actual_package
  end

  if parent && !parent.meta_package?
    @dependents[name] ||= Set.new
    @dependents[name].add(parent.name)
    @dependencies[parent.name] ||= Set.new
    @dependencies[parent.name].add(name)
  end

  # already installed?
  if @versions.has_key? name
    false
  else
    @versions[name] = version
    @sources[name] = package.source
    @files[name] = package.files
    true
  end
ensure
  save
end

#dependencies_for(name) ⇒ Object



95
96
97
# File 'lib/rip/package_manager.rb', line 95

def dependencies_for(name)
  (@dependencies[name] || []).map { |name| package(name) }
end

#dirObject



166
167
168
# File 'lib/rip/package_manager.rb', line 166

def dir
  File.join(Rip.dir, @env)
end

#files(name) ⇒ Object



99
100
101
# File 'lib/rip/package_manager.rb', line 99

def files(name)
  Array(@files[name])
end

#inspectObject



69
70
71
# File 'lib/rip/package_manager.rb', line 69

def inspect
  "(#{self.class} dependencies=#{dependencies.inspect} dependents=#{dependents.inspect} sources=#{sources.inspect} versions=#{versions.inspect})"
end

#installed?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rip/package_manager.rb', line 103

def installed?(name)
  @versions.has_key? name
end

#loadObject



177
178
179
# File 'lib/rip/package_manager.rb', line 177

def load
  marshal_read unzip(File.read(path)) if File.exists? path
end

#marshal_payloadObject



191
192
193
# File 'lib/rip/package_manager.rb', line 191

def marshal_payload
  Marshal.dump [ @versions, @dependents, @dependencies, @sources, @files ]
end

#marshal_read(data) ⇒ Object



195
196
197
# File 'lib/rip/package_manager.rb', line 195

def marshal_read(data)
  @versions, @dependents, @dependencies, @sources, @files = Marshal.load(data)
end

#package(name) ⇒ Object

If given a package name, finds and returns the package. If given a package, returns it.



83
84
85
86
87
88
89
# File 'lib/rip/package_manager.rb', line 83

def package(name)
  if name.respond_to? :cache_path
    name
  elsif @versions[name]
    Package.for(@sources[name], @versions[name], @files[name])
  end
end

#package_namesObject



77
78
79
# File 'lib/rip/package_manager.rb', line 77

def package_names
  @versions.keys
end

#package_version(name) ⇒ Object



107
108
109
# File 'lib/rip/package_manager.rb', line 107

def package_version(name)
  @versions[name]
end

#packagesObject



73
74
75
# File 'lib/rip/package_manager.rb', line 73

def packages
  @versions.keys.map { |name| package(name) }
end

#packages_that_depend_on(name) ⇒ Object



91
92
93
# File 'lib/rip/package_manager.rb', line 91

def packages_that_depend_on(name)
  (@dependents[name] || []).map { |name| package(name) }
end

#pathObject



162
163
164
# File 'lib/rip/package_manager.rb', line 162

def path
  File.join(dir, "#{@env}.ripenv")
end

#remove_package(package) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rip/package_manager.rb', line 149

def remove_package(package)
  name = package.name

  Array(@dependencies[name]).each do |dep|
    @dependents[dep].delete(name) if @dependents[dep].respond_to? :delete
  end

  @dependents.delete(name)
  @dependencies.delete(name)
  @versions.delete(name)
  save
end

#saveObject



170
171
172
173
174
175
# File 'lib/rip/package_manager.rb', line 170

def save
  File.open(path, 'w') do |f|
    f.puts zip(marshal_payload)
    f.flush
  end
end

#unzip(data) ⇒ Object



185
186
187
188
189
# File 'lib/rip/package_manager.rb', line 185

def unzip(data)
  Zlib::Inflate.inflate(data)
rescue Zlib::BufError
  raise CorruptedRipenv.new("#{path} possibly corrupted")
end

#zip(data) ⇒ Object



181
182
183
# File 'lib/rip/package_manager.rb', line 181

def zip(data)
  Zlib::Deflate.deflate(data)
end