forked from vieter-v/vieter
69 lines
1.6 KiB
V
69 lines
1.6 KiB
V
module repo
|
|
|
|
import os
|
|
import package
|
|
|
|
// Represents a collection of Repos
|
|
pub struct MultiArchRepo {
|
|
mut:
|
|
repos shared map[string]ArchRepo [required]
|
|
pub:
|
|
pkg_dir string [required]
|
|
repo_dir string [required]
|
|
}
|
|
|
|
// Initializes a new multi-repository.
|
|
pub fn new_multi(repo_dir string, pkg_dir string) ?MultiArchRepo {
|
|
mut repos := map[string]ArchRepo{}
|
|
|
|
// We initialize a repo for each directory inside the repos dir
|
|
if !os.is_dir(repo_dir) {
|
|
os.mkdir_all(repo_dir) or {
|
|
return error("Failed to create directory '$repo_dir': $err.msg")
|
|
}
|
|
} else {
|
|
for name in os.ls(repo_dir) ? {
|
|
d := os.join_path_single(repo_dir, name)
|
|
|
|
if os.is_dir(d) {
|
|
repos[name] = new_arch(d, pkg_dir) ?
|
|
}
|
|
}
|
|
}
|
|
|
|
return MultiArchRepo{
|
|
pkg_dir: pkg_dir
|
|
repo_dir: repo_dir
|
|
repos: repos
|
|
}
|
|
}
|
|
|
|
pub fn (r &MultiArchRepo) add_from_path(repo_name string, pkg_path string) ?(bool, &package.Pkg) {
|
|
// First, we create the repo if it isn't present yet
|
|
repo := lock r.repos {
|
|
if repo_name !in r.repos {
|
|
r.repos[repo_name] = new_arch(os.join_path_single(r.repo_dir, repo_name),
|
|
r.pkg_dir) ?
|
|
}
|
|
|
|
r.repos[repo_name] ?
|
|
}
|
|
|
|
// We read in the package file
|
|
pkg := package.read_pkg(pkg_path) or { return error('Failed to read package file: $err.msg') }
|
|
|
|
added := repo.add(pkg) ?
|
|
|
|
// Move over package to pkg dir if it was successfully added
|
|
if added {
|
|
dest_path := os.real_path(os.join_path_single(r.pkg_dir, pkg.filename()))
|
|
|
|
// Only move the file if it's not already in the package directory
|
|
if dest_path != os.real_path(pkg_path) {
|
|
os.mv(pkg_path, dest_path) ?
|
|
}
|
|
}
|
|
|
|
return added, &pkg
|
|
}
|