2022-01-09 22:30:07 +01:00
|
|
|
module repo
|
|
|
|
|
|
|
|
import os
|
2022-01-19 18:54:33 +01:00
|
|
|
import package
|
2022-01-09 22:30:07 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// subpath where the uncompressed version of the files archive is stored
|
|
|
|
const files_subpath = 'files'
|
|
|
|
|
|
|
|
// subpath where the uncompressed version of the repo archive is stored
|
|
|
|
const repo_subpath = 'repo'
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-11 18:19:41 +01:00
|
|
|
// Dummy struct to work around the fact that you can only share structs, maps &
|
|
|
|
// arrays
|
2022-01-11 21:01:09 +01:00
|
|
|
pub struct Dummy {
|
|
|
|
x int
|
|
|
|
}
|
2022-01-11 18:19:41 +01:00
|
|
|
|
2022-01-13 21:47:14 +01:00
|
|
|
// This struct manages a single repository.
|
2022-01-11 14:40:25 +01:00
|
|
|
pub struct Repo {
|
2022-01-11 16:37:49 +01:00
|
|
|
mut:
|
2022-01-11 18:19:41 +01:00
|
|
|
mutex shared Dummy
|
2022-01-11 16:08:11 +01:00
|
|
|
pub:
|
2022-01-13 21:47:14 +01:00
|
|
|
// Where to store repository files; should exist
|
|
|
|
repo_dir string [required]
|
|
|
|
// Where to find packages; packages are expected to all be in the same directory
|
|
|
|
pkg_dir string [required]
|
2022-01-11 16:08:11 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 21:24:46 +01:00
|
|
|
// new creates a new Repo & creates the directories as needed
|
2022-01-19 18:54:33 +01:00
|
|
|
pub fn new(repo_dir string, pkg_dir string) ?Repo {
|
|
|
|
if !os.is_dir(repo_dir) {
|
|
|
|
os.mkdir_all(repo_dir) or { return error('Failed to create repo directory.') }
|
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
if !os.is_dir(pkg_dir) {
|
|
|
|
os.mkdir_all(pkg_dir) or { return error('Failed to create package directory.') }
|
|
|
|
}
|
2022-01-13 21:47:14 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
return Repo{
|
|
|
|
repo_dir: repo_dir
|
|
|
|
pkg_dir: pkg_dir
|
|
|
|
}
|
2022-01-11 18:19:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// add_from_path adds a package from an arbitrary path & moves it into the pkgs
|
|
|
|
// directory if necessary.
|
|
|
|
pub fn (r &Repo) add_from_path(pkg_path string) ?bool {
|
2022-01-20 19:39:38 +01:00
|
|
|
pkg := package.read_pkg(pkg_path) or { return error('Failed to read package file: $err.msg') }
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
added := r.add(pkg) ?
|
|
|
|
|
|
|
|
// If the add was successful, we move the file to the packages directory
|
|
|
|
if added {
|
|
|
|
dest_path := os.real_path(os.join_path_single(r.pkg_dir, pkg.filename()))
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// 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) ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:11:03 +01:00
|
|
|
return added
|
2022-01-11 14:40:25 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// add adds a given Pkg to the repository
|
|
|
|
fn (r &Repo) add(pkg &package.Pkg) ?bool {
|
|
|
|
pkg_dir := r.pkg_path(pkg)
|
2022-01-11 16:08:11 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
// We can't add the same package twice
|
|
|
|
if os.exists(pkg_dir) {
|
|
|
|
return false
|
2022-01-11 16:08:11 +01:00
|
|
|
}
|
2022-01-09 22:30:07 +01:00
|
|
|
|
2022-01-19 18:54:33 +01:00
|
|
|
os.mkdir(pkg_dir) or { return error('Failed to create package directory.') }
|
|
|
|
|
|
|
|
os.write_file(os.join_path_single(pkg_dir, 'desc'), pkg.to_desc()) or {
|
|
|
|
os.rmdir_all(pkg_dir) ?
|
|
|
|
|
|
|
|
return error('Failed to write desc file.')
|
2022-01-09 22:30:07 +01:00
|
|
|
}
|
2022-01-19 18:54:33 +01:00
|
|
|
os.write_file(os.join_path_single(pkg_dir, 'files'), pkg.to_files()) or {
|
|
|
|
os.rmdir_all(pkg_dir) ?
|
|
|
|
|
|
|
|
return error('Failed to write files file.')
|
|
|
|
}
|
2022-01-20 17:11:03 +01:00
|
|
|
|
|
|
|
r.sync() ?
|
2022-01-19 18:54:33 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the path where the given package's desc & files files are stored
|
|
|
|
fn (r &Repo) pkg_path(pkg &package.Pkg) string {
|
|
|
|
return os.join_path(r.repo_dir, '$pkg.info.name-$pkg.info.version')
|
2022-01-09 22:30:07 +01:00
|
|
|
}
|