use super::{BloatDir, EnforcePolicy, PackageManager, dir_size};
use anyhow::{Result, anyhow};
use std::fs;
use std::path::Path;
const MANIFEST: &str = "project.godot";
const CACHE_DIRS: &[&str] = &[".godot", ".import"];
pub struct Godot;
impl PackageManager for Godot {
fn name(&self) -> &'static str {
"godot"
}
fn detect(&self, path: &Path) -> bool {
path.join(MANIFEST).is_file()
}
fn bloat_dirs(&self, path: &Path) -> Vec<BloatDir> {
CACHE_DIRS
.iter()
.map(|name| path.join(name))
.filter(|dir| dir.is_dir())
.map(|dir| BloatDir {
name: dir
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned(),
size_bytes: dir_size(&dir),
path: dir,
shared_bytes: 0,
})
.collect()
}
fn enforce_lockfile(&self, path: &Path, _policy: EnforcePolicy) -> Result<()> {
let manifest = path.join(MANIFEST);
let content = fs::read_to_string(&manifest).map_err(|e| {
anyhow!("`{MANIFEST}` could not be read ({e}): nothing to re-import the assets from.")
})?;
if !content.contains("config_version=") {
return Err(anyhow!(
"`{MANIFEST}` has no `config_version=` line: refusing to treat the imported-\
resource cache as regenerable from it."
));
}
Ok(())
}
fn restore(&self, _path: &Path, _timeout: std::time::Duration) -> Result<()> {
println!(
"Godot's imported-resource cache regenerates when the editor next opens the \
project; `godot --headless --import` does it without the window on 4.x"
);
Ok(())
}
fn lockfiles(&self) -> &'static [&'static str] {
&[MANIFEST]
}
fn opt_in(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use tempfile::tempdir;
fn project(dir: &Path) -> PathBuf {
fs::write(
dir.join(MANIFEST),
"; Engine configuration file.\nconfig_version=5\n\n[application]\n\nconfig/name=\"Demo\"\n",
)
.unwrap();
dir.to_path_buf()
}
fn claimed(project: &Path) -> Vec<String> {
Godot
.bloat_dirs(project)
.into_iter()
.map(|b| b.name)
.collect()
}
#[test]
fn detects_on_the_project_manifest() {
let dir = tempdir().unwrap();
assert!(!Godot.detect(dir.path()));
project(dir.path());
assert!(Godot.detect(dir.path()));
}
#[test]
fn both_engine_versions_caches_are_claimed() {
let dir = tempdir().unwrap();
let root = project(dir.path());
fs::create_dir(root.join(".godot")).unwrap();
fs::write(root.join(".godot").join("uid_cache.bin"), "cache").unwrap();
fs::create_dir(root.join(".import")).unwrap();
assert_eq!(claimed(&root), vec![".godot", ".import"]);
}
#[test]
fn nothing_else_beside_the_manifest_is_claimed() {
let dir = tempdir().unwrap();
let root = project(dir.path());
fs::create_dir(root.join("assets")).unwrap();
assert!(claimed(&root).is_empty());
}
#[test]
fn a_missing_or_bogus_manifest_is_refused() {
let dir = tempdir().unwrap();
let policy = EnforcePolicy::default();
assert!(Godot.enforce_lockfile(dir.path(), policy).is_err());
fs::write(dir.path().join(MANIFEST), "hello there").unwrap();
assert!(Godot.enforce_lockfile(dir.path(), policy).is_err());
project(dir.path());
assert!(Godot.enforce_lockfile(dir.path(), policy).is_ok());
}
#[test]
fn godot_is_opt_in() {
assert!(Godot.opt_in());
}
}