79 lines
1.5 KiB
Crystal
79 lines
1.5 KiB
Crystal
require "file_utils"
|
|
|
|
require "./package.cr"
|
|
require "./manifest.cr"
|
|
|
|
class Package::Database
|
|
PATH_FROM_ROOT = "/var/lib/package"
|
|
|
|
property root : String
|
|
|
|
def initialize
|
|
@root = Database::PATH_FROM_ROOT
|
|
end
|
|
|
|
def <<(tuple : Tuple(Package, Manifest))
|
|
package, manifest = tuple
|
|
|
|
entry_path = get_entry_path(package)
|
|
|
|
FileUtils.mkdir_p entry_path
|
|
|
|
FileUtils.cp package.file_path, "#{entry_path}/package.spec"
|
|
FileUtils.cp manifest.file_path, "#{entry_path}/manifest"
|
|
end
|
|
|
|
def remove(package : Package)
|
|
FileUtils.rm_r get_entry_path package.to_atom
|
|
end
|
|
|
|
def is_installed?(atom : Atom)
|
|
Dir.exists? get_entry_path(atom)
|
|
end
|
|
|
|
def get_package(atom : Atom)
|
|
if atom.slot.nil?
|
|
atom = Atom.new atom.name,
|
|
slot: get_sanitized_slots(atom.name)[0]
|
|
end
|
|
|
|
entry_path = get_entry_path(atom)
|
|
|
|
Package.new "#{entry_path}/package.spec"
|
|
end
|
|
|
|
def get_all_packages
|
|
packages = [] of Package
|
|
|
|
Dir.children(@root).each do |name|
|
|
path = "#{@root}/#{name}"
|
|
|
|
Dir.children(path).each do |slot|
|
|
packages << Package.new "#{path}/#{slot}/package.spec"
|
|
end
|
|
end
|
|
|
|
packages
|
|
end
|
|
|
|
def get_manifest(package : Package)
|
|
Manifest.new "#{get_entry_path(package)}/manifest"
|
|
end
|
|
|
|
private def get_sanitized_slots(name : String)
|
|
Dir.children "#{@root}/#{name}"
|
|
end
|
|
|
|
private def get_entry_path(package : Package)
|
|
get_entry_path package.to_atom
|
|
end
|
|
|
|
private def get_entry_path(atom : Atom)
|
|
name = atom.name
|
|
slot = atom.sanitized_slot
|
|
|
|
"#{@root}/#{name}/#{slot}"
|
|
end
|
|
end
|
|
|