use super::{BloatDir, EnforcePolicy, PackageManager, dir_size};
use anyhow::{Result, anyhow};
use std::fs;
use std::path::Path;
const MANIFEST: &str = "package.json";
const CACHE_DIRS: &[&str] = &["library", "temp"];
fn is_creator_manifest(content: &str) -> bool {
content.contains("\"creator\"") || content.contains("cocos-creator")
}
pub struct Cocos;
impl PackageManager for Cocos {
fn name(&self) -> &'static str {
"cocos"
}
fn detect(&self, path: &Path) -> bool {
let manifest = path.join(MANIFEST);
manifest.is_file()
&& fs::read_to_string(&manifest).is_ok_and(|content| is_creator_manifest(&content))
}
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 !is_creator_manifest(&content) {
return Err(anyhow!(
"`{MANIFEST}` no longer names Cocos Creator: refusing to treat the \
imported-asset cache as regenerable from it."
));
}
Ok(())
}
fn restore(&self, _path: &Path, _timeout: std::time::Duration) -> Result<()> {
println!(
"Cocos Creator's imported-asset cache regenerates when the editor next \
opens the project"
);
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),
"{\n \"name\": \"demo\",\n \"creator\": {\n \"version\": \"3.8.0\"\n }\n}\n",
)
.unwrap();
dir.to_path_buf()
}
fn claimed(project: &Path) -> Vec<String> {
Cocos
.bloat_dirs(project)
.into_iter()
.map(|b| b.name)
.collect()
}
#[test]
fn detects_on_a_creator_manifest_only() {
let dir = tempdir().unwrap();
assert!(!Cocos.detect(dir.path()));
fs::write(
dir.path().join(MANIFEST),
"{\n \"name\": \"demo\",\n \"dependencies\": {}\n}\n",
)
.unwrap();
assert!(!Cocos.detect(dir.path()));
project(dir.path());
assert!(Cocos.detect(dir.path()));
}
#[test]
fn a_creator_2x_engine_line_also_detects() {
let dir = tempdir().unwrap();
fs::write(
dir.path().join(MANIFEST),
"{\n \"name\": \"demo\",\n \"engine\": \"cocos-creator-js\",\n \"version\": \"2.4.13\"\n}\n",
)
.unwrap();
assert!(Cocos.detect(dir.path()));
}
#[test]
fn the_caches_are_claimed_and_the_exported_build_is_not() {
let dir = tempdir().unwrap();
let root = project(dir.path());
fs::create_dir(root.join("library")).unwrap();
fs::write(root.join("library").join("imports.json"), "cache").unwrap();
fs::create_dir(root.join("temp")).unwrap();
fs::create_dir(root.join("build")).unwrap();
fs::create_dir(root.join("assets")).unwrap();
assert_eq!(claimed(&root), vec!["library", "temp"]);
}
#[test]
fn a_missing_or_bogus_manifest_is_refused() {
let dir = tempdir().unwrap();
let policy = EnforcePolicy::default();
assert!(Cocos.enforce_lockfile(dir.path(), policy).is_err());
fs::write(dir.path().join(MANIFEST), "{\"name\": \"demo\"}").unwrap();
assert!(Cocos.enforce_lockfile(dir.path(), policy).is_err());
project(dir.path());
assert!(Cocos.enforce_lockfile(dir.path(), policy).is_ok());
}
#[test]
fn cocos_is_opt_in() {
assert!(Cocos.opt_in());
}
}