forked from vieter-v/vieter
63 lines
1.4 KiB
V
63 lines
1.4 KiB
V
module repo
|
|
|
|
import os
|
|
import package
|
|
|
|
// A architecture repo acts as a single repo, but manages multiple repos
|
|
// internally that each manage a repo for a specific architecture
|
|
pub struct ArchRepo {
|
|
mut:
|
|
// Each key is the name of an architecture, e.g. x86_64
|
|
repos shared map[string]Repo [required]
|
|
pub:
|
|
repo_dir string [required]
|
|
pkg_dir string [required]
|
|
}
|
|
|
|
pub fn new_arch(repo_dir string, pkg_dir string) ?ArchRepo {
|
|
if !os.is_dir(pkg_dir) {
|
|
os.mkdir(pkg_dir) or { return error('Failed to create package directory: $err.msg') }
|
|
}
|
|
|
|
mut repos := map[string]Repo{}
|
|
|
|
if !os.is_dir(repo_dir) {
|
|
os.mkdir(repo_dir) or { return error('Failed to create repo directory: $err.msg') }
|
|
}
|
|
// If the directory already exists, we can check which repos already exist
|
|
else {
|
|
for name in os.ls(repo_dir) ? {
|
|
d := os.join_path_single(repo_dir, name)
|
|
|
|
if os.is_dir(d) {
|
|
// The name is expected to be a correct architecture name
|
|
repos[name] = new(d, pkg_dir) ?
|
|
}
|
|
}
|
|
}
|
|
|
|
return ArchRepo{
|
|
repo_dir: repo_dir
|
|
pkg_dir: pkg_dir
|
|
repos: repos
|
|
}
|
|
}
|
|
|
|
fn (r &ArchRepo) add(pkg &package.Pkg) ?bool {
|
|
// TODO add 'any' architecture to every repo
|
|
arch := pkg.info.arch
|
|
|
|
lock r.repos {
|
|
// If a repo for this architecture doesn't exist yet, create it
|
|
if arch !in r.repos {
|
|
r.repos[arch] = new(os.join_path_single(r.repo_dir, arch), r.pkg_dir) ?
|
|
}
|
|
}
|
|
|
|
repo := rlock r.repos {
|
|
r.repos[arch]
|
|
}
|
|
|
|
return repo.add(pkg)
|
|
}
|