feat: complete M2 content workspace and inspector

This commit is contained in:
Rbanh committed 2026-07-17 23:40:49 -04:00
1 parent 931f561e7f
commit cf6eaba534
438 files changed
+59749 -19043

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
# xtask subtree rules
- Packaging, migration, validation, and headless commands must be deterministic and scriptable.
- Packaging is a slice or candidate gate, not an inner-loop command.
- Package tests use compact fixtures and must not require the editor crate.
- Repeated package builds must be idempotent.
- Use the isolated package lane; do not invalidate the fast editor lane.
- Run package-focused tests before any full-workspace gate when packaging changes.
+8 -1
View File
@@ -33,10 +33,16 @@ name = "upgrade-project"
path = "src/upgrade_project.rs"
required-features = ["validate-levels"]
[[bin]]
name = "process-assets"
path = "src/process_assets.rs"
required-features = ["content-pipeline"]
[dependencies]
walkdir = "2.5"
scene = { path = "../crates/scene", optional = true }
shared = { path = "../crates/shared", optional = true }
content_pipeline = { path = "../crates/content_pipeline", optional = true }
settings = { workspace = true, optional = true }
serde_json = "1"
serde = { version = "1", features = ["derive"] }
@@ -48,5 +54,6 @@ libc = "0.2"
[features]
default = []
validate-levels = ["dep:scene", "dep:settings", "dep:shared"]
validate-levels = ["dep:scene", "dep:settings", "dep:shared", "dep:content_pipeline"]
validate-samples = ["validate-levels"]
content-pipeline = ["dep:content_pipeline", "dep:shared"]
+10 -407
View File
@@ -1,410 +1,13 @@
use std::{
env, fs, io,
path::{Path, PathBuf},
process::ExitCode,
time::{Duration, SystemTime},
};
//! Compatibility entry point for the retired age-based target cleaner.
//!
//! Build artifacts are now managed as whole, sentinel-marked lanes by
//! `scripts/codex/build_storage.py`. Deleting selected entries from Cargo's
//! internal directories is intentionally unsupported.
use walkdir::WalkDir;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Profile {
Debug,
Release,
}
impl Profile {
fn as_str(self) -> &'static str {
match self {
Profile::Debug => "debug",
Profile::Release => "release",
}
}
}
#[derive(Debug)]
struct Config {
apply: bool,
days: u64,
target_dir: PathBuf,
profiles: Vec<Profile>,
include_artifacts: bool,
keep_incremental: bool,
verbose: bool,
}
#[derive(Debug)]
struct Candidate {
path: PathBuf,
kind: &'static str,
reason: &'static str,
bytes: u64,
}
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("error: {error}");
ExitCode::FAILURE
}
}
}
fn run() -> Result<(), String> {
let config = parse_args(env::args().skip(1))?;
let cutoff = SystemTime::now()
.checked_sub(Duration::from_secs(config.days * 24 * 60 * 60))
.ok_or("invalid --days value")?;
let candidates = collect_candidates(&config, cutoff)?;
print_report(&config, &candidates);
if config.apply {
apply_candidates(&candidates)?;
}
Ok(())
}
fn parse_args(args: impl IntoIterator<Item = String>) -> Result<Config, String> {
let mut config = Config {
apply: false,
days: 7,
target_dir: env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("target")),
profiles: vec![Profile::Debug],
include_artifacts: false,
keep_incremental: false,
verbose: false,
};
let mut args = args.into_iter();
while let Some(arg) = args.next() {
match arg.as_str() {
"--apply" => config.apply = true,
"--include-artifacts" => config.include_artifacts = true,
"--keep-incremental" => config.keep_incremental = true,
"--verbose" => config.verbose = true,
"--days" => {
let value = args
.next()
.ok_or_else(|| "--days requires a number".to_string())?;
config.days = value
.parse()
.map_err(|_| format!("invalid --days value: {value}"))?;
}
"--profile" => {
let value = args
.next()
.ok_or_else(|| "--profile requires debug, release, or all".to_string())?;
config.profiles = match value.as_str() {
"debug" | "dev" => vec![Profile::Debug],
"release" => vec![Profile::Release],
"all" => vec![Profile::Debug, Profile::Release],
_ => return Err(format!("invalid --profile value: {value}")),
};
}
"--target-dir" => {
config.target_dir = args
.next()
.map(PathBuf::from)
.ok_or_else(|| "--target-dir requires a path".to_string())?;
}
"-h" | "--help" => {
print_help();
std::process::exit(0);
}
_ => return Err(format!("unknown argument: {arg}")),
}
}
Ok(config)
}
fn collect_candidates(config: &Config, cutoff: SystemTime) -> Result<Vec<Candidate>, String> {
let mut candidates = Vec::new();
if !config.target_dir.exists() {
return Ok(candidates);
}
collect_flycheck_dirs(config, cutoff, &mut candidates)?;
for profile in &config.profiles {
let profile_dir = config.target_dir.join(profile.as_str());
if !profile_dir.exists() {
continue;
}
if !config.keep_incremental {
let incremental = profile_dir.join("incremental");
if incremental.exists() {
push_candidate(
&mut candidates,
incremental,
"incremental",
"safe Cargo incremental cache",
)?;
}
}
if config.include_artifacts {
collect_stale_children(
&profile_dir.join("deps"),
cutoff,
"deps",
"stale hashed dependency artifact",
&mut candidates,
)?;
collect_stale_children(
&profile_dir.join("build"),
cutoff,
"build",
"stale build-script output",
&mut candidates,
)?;
collect_stale_children(
&profile_dir.join(".fingerprint"),
cutoff,
"fingerprint",
"stale Cargo fingerprint",
&mut candidates,
)?;
collect_stale_children(
&profile_dir.join("examples"),
cutoff,
"examples",
"stale example artifact",
&mut candidates,
)?;
}
}
candidates.sort_by(|a, b| a.path.cmp(&b.path));
Ok(candidates)
}
fn collect_flycheck_dirs(
config: &Config,
cutoff: SystemTime,
candidates: &mut Vec<Candidate>,
) -> Result<(), String> {
let entries = fs::read_dir(&config.target_dir)
.map_err(|error| format!("failed to read {}: {error}", config.target_dir.display()))?;
for entry in entries {
let entry = entry.map_err(|error| format!("failed to read target entry: {error}"))?;
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
if file_name.starts_with("flycheck") && is_stale(&entry.path(), cutoff).unwrap_or(false) {
push_candidate(
candidates,
entry.path(),
"flycheck",
"stale rust-analyzer flycheck target dir",
)?;
}
}
Ok(())
}
fn collect_stale_children(
dir: &Path,
cutoff: SystemTime,
kind: &'static str,
reason: &'static str,
candidates: &mut Vec<Candidate>,
) -> Result<(), String> {
if !dir.exists() {
return Ok(());
}
let entries =
fs::read_dir(dir).map_err(|error| format!("failed to read {}: {error}", dir.display()))?;
for entry in entries {
let entry =
entry.map_err(|error| format!("failed to read {} entry: {error}", dir.display()))?;
let path = entry.path();
if is_stale(&path, cutoff).unwrap_or(false) {
push_candidate(candidates, path, kind, reason)?;
}
}
Ok(())
}
fn push_candidate(
candidates: &mut Vec<Candidate>,
path: PathBuf,
kind: &'static str,
reason: &'static str,
) -> Result<(), String> {
let bytes =
path_size(&path).map_err(|error| format!("failed to size {}: {error}", path.display()))?;
candidates.push(Candidate {
path,
kind,
reason,
bytes,
});
Ok(())
}
fn is_stale(path: &Path, cutoff: SystemTime) -> io::Result<bool> {
let modified = fs::metadata(path)?.modified()?;
Ok(modified < cutoff)
}
fn path_size(path: &Path) -> io::Result<u64> {
let metadata = fs::symlink_metadata(path)?;
if metadata.is_file() {
return Ok(metadata.len());
}
let mut bytes = 0;
for entry in WalkDir::new(path).follow_links(false) {
let entry = entry?;
let metadata = entry.metadata()?;
if metadata.is_file() {
bytes += metadata.len();
}
}
Ok(bytes)
}
fn print_report(config: &Config, candidates: &[Candidate]) {
let total: u64 = candidates.iter().map(|candidate| candidate.bytes).sum();
let mode = if config.apply { "APPLY" } else { "DRY RUN" };
println!(
"{mode}: {} candidate(s), {} reclaimable in {}",
candidates.len(),
format_bytes(total),
config.target_dir.display()
);
println!(
"profiles={}, days={}, include_artifacts={}, incremental={}",
config
.profiles
.iter()
.map(|profile| profile.as_str())
.collect::<Vec<_>>()
.join(","),
config.days,
config.include_artifacts,
!config.keep_incremental
);
print_kind_summary(candidates);
let mut sorted: Vec<_> = candidates.iter().collect();
sorted.sort_by(|a, b| b.bytes.cmp(&a.bytes).then_with(|| a.path.cmp(&b.path)));
let limit = if config.verbose { sorted.len() } else { 50 };
for candidate in sorted.iter().take(limit) {
println!(
"{:>10} {:<12} {} ({})",
format_bytes(candidate.bytes),
candidate.kind,
candidate.path.display(),
candidate.reason
);
}
if sorted.len() > limit {
println!(
"... {} more candidate(s) omitted; rerun with --verbose to list all.",
sorted.len() - limit
);
}
if !config.apply {
println!();
println!("No files were deleted. Add --apply to delete these candidates.");
if !config.include_artifacts {
println!(
"Add --include-artifacts to also prune stale deps/build/.fingerprint entries."
);
}
}
}
fn print_kind_summary(candidates: &[Candidate]) {
let mut rows: Vec<(&str, usize, u64)> = Vec::new();
for candidate in candidates {
if let Some((_, count, bytes)) =
rows.iter_mut().find(|(kind, _, _)| *kind == candidate.kind)
{
*count += 1;
*bytes += candidate.bytes;
} else {
rows.push((candidate.kind, 1, candidate.bytes));
}
}
rows.sort_by_key(|row| std::cmp::Reverse(row.2));
for (kind, count, bytes) in rows {
println!(
"summary: {:<12} {:>5} item(s), {}",
kind,
count,
format_bytes(bytes)
);
}
}
fn apply_candidates(candidates: &[Candidate]) -> Result<(), String> {
for candidate in candidates {
let metadata = fs::symlink_metadata(&candidate.path)
.map_err(|error| format!("failed to inspect {}: {error}", candidate.path.display()))?;
if metadata.is_dir() {
fs::remove_dir_all(&candidate.path).map_err(|error| {
format!("failed to remove {}: {error}", candidate.path.display())
})?;
} else {
fs::remove_file(&candidate.path).map_err(|error| {
format!("failed to remove {}: {error}", candidate.path.display())
})?;
}
}
Ok(())
}
fn format_bytes(bytes: u64) -> String {
const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
let mut value = bytes as f64;
let mut unit = UNITS[0];
for next in &UNITS[1..] {
if value < 1024.0 {
break;
}
value /= 1024.0;
unit = next;
}
if unit == "B" {
format!("{bytes} B")
} else {
format!("{value:.1} {unit}")
}
}
fn print_help() {
println!(
"\
Prune Cargo target cache for this workspace.
USAGE:
cargo run -p xtask --bin clean-target -- [OPTIONS]
OPTIONS:
--apply Delete candidates. Without this, only prints a dry run.
--days N Staleness cutoff for artifact caches [default: 7].
--profile PROFILE debug, release, or all [default: debug].
--target-dir PATH Target directory [default: CARGO_TARGET_DIR or target].
--include-artifacts Also prune stale deps/build/.fingerprint/example artifacts.
--keep-incremental Do not remove incremental cache.
--verbose List every candidate instead of the largest 50.
-h, --help Print help.
EXAMPLES:
cargo run -p xtask --bin clean-target --
cargo run -p xtask --bin clean-target -- --apply
cargo run -p xtask --bin clean-target -- --include-artifacts --days 3 --apply
"
fn main() {
eprintln!(
"cargo clean-target is retired. Use `python scripts/codex/build_storage.py status`, \
`prune --dry-run`, or `prune --apply`."
);
std::process::exit(2);
}
+186 -3
View File
@@ -319,6 +319,7 @@ fn run(options: Options) -> Result<PathBuf, String> {
}
fn validate_release_ready(project_root: &Path, phase: &str) -> Result<(), String> {
validate_generated_content(project_root, phase)?;
let validation = scene::validate_project(project_root);
if validation.is_release_ready() {
return Ok(());
@@ -342,6 +343,99 @@ fn validate_release_ready(project_root: &Path, phase: &str) -> Result<(), String
))
}
fn validate_generated_content(project_root: &Path, phase: &str) -> Result<(), String> {
let registry_path = project_root.join(content_pipeline::REGISTRY_PATH);
let source = fs::read_to_string(&registry_path)
.map_err(|error| format!("could not read {}: {error}", registry_path.display()))?;
let loaded = shared::parse_asset_registry(&source)?;
if loaded.migration_required {
return Err(format!(
"asset registry requires explicit migration {phase}; run `cargo upgrade-project --project . --apply`"
));
}
let mut processed = content_pipeline::scan_project(project_root, &loaded.document)?;
let mut model_plans = Vec::new();
for record in processed
.registry
.records
.iter_mut()
.filter(|record| record.kind == shared::AssetKind::Model)
{
let plan = content_pipeline::plan_model_artifacts_at(project_root, record)?;
*record = plan.record.clone();
model_plans.push(plan);
}
let expected_registry = content_pipeline::serialize_registry(&processed.registry)?;
if source.as_bytes() != expected_registry {
return Err(format!(
"asset registry is stale {phase}; run `cargo process-assets --project .`"
));
}
if model_plans.iter().any(|plan| {
fs::read(&plan.static_path).ok().as_deref() != Some(plan.static_bytes.as_slice())
|| fs::read(&plan.animation_path).ok().as_deref()
!= Some(plan.animation_bytes.as_slice())
}) {
return Err(format!(
"model artifacts are stale {phase}; run `cargo process-assets --project .`"
));
}
processed.runtime_catalog = shared::RuntimeContentCatalog::from(&processed.registry);
for record in processed
.registry
.records
.iter()
.filter(|record| record.kind == shared::AssetKind::Texture)
{
let plan = content_pipeline::plan_texture_artifact(project_root, record)?;
if !plan.reused
&& fs::read(&plan.output_path).ok().as_deref() != Some(plan.output_bytes.as_slice())
{
return Err(format!(
"texture artifacts are stale {phase}; run `cargo process-assets --project .`"
));
}
content_pipeline::apply_texture_plan_to_catalog(
&mut processed.runtime_catalog,
&record.id,
&plan,
)?;
}
for record in processed.registry.records.iter().filter(|record| {
matches!(
record.kind,
shared::AssetKind::Material | shared::AssetKind::MaterialInstance
)
}) {
let Some(plan) =
content_pipeline::plan_material_artifact(project_root, record, &processed.registry)?
else {
continue;
};
if !plan.reused
&& fs::read(&plan.output_path).ok().as_deref() != Some(plan.output_bytes.as_slice())
{
return Err(format!(
"material artifacts are stale {phase}; run `cargo process-assets --project .`"
));
}
content_pipeline::apply_material_plan_to_catalog(
&mut processed.runtime_catalog,
&record.id,
&plan,
)?;
}
let catalog_path = project_root.join(content_pipeline::RUNTIME_CATALOG_PATH);
let expected_catalog = content_pipeline::serialize_runtime_catalog(&processed.runtime_catalog)?;
if fs::read(&catalog_path).ok().as_deref() != Some(expected_catalog.as_slice()) {
return Err(format!(
"runtime content catalog is stale {phase}; run `cargo process-assets --project .`"
));
}
Ok(())
}
fn load_profile(project_root: &Path, profile_name: &str) -> Result<BuildProfile, String> {
if profile_name.is_empty()
|| !profile_name
@@ -674,7 +768,14 @@ fn resolve_output(project_root: &Path, output: &str) -> Result<PathBuf, String>
let suffix = lexical_output
.strip_prefix(existing)
.map_err(|error| format!("could not resolve build output suffix: {error}"))?;
let resolved_output = resolved_ancestor.join(suffix);
// Joining an empty suffix can preserve a trailing separator on some platforms. The package
// lock stores this path as its identity, so first publication (output absent) and subsequent
// publication (output present) must resolve to byte-identical strings.
let resolved_output = if suffix.as_os_str().is_empty() {
resolved_ancestor
} else {
resolved_ancestor.join(suffix)
};
if resolved_output == resolved_dist || !resolved_output.starts_with(&resolved_dist) {
return Err("build output must resolve inside the project dist directory".into());
}
@@ -771,7 +872,7 @@ impl PackageLock {
})?;
if record.magic != PACKAGE_LOCK_MAGIC
|| record.schema_version != PACKAGE_SCHEMA_VERSION
|| record.output != output_identity
|| Path::new(&record.output) != output
{
return Err(format!(
"refusing to overwrite unrecognized package lock {}",
@@ -1478,7 +1579,50 @@ fn runtime_asset_sources(project_root: &Path) -> Result<Vec<(PathBuf, PathBuf)>,
sources.push((relative.to_path_buf(), entry.path().to_path_buf()));
}
}
let catalog_path = assets.join("content.catalog.ron");
let catalog_source = fs::read_to_string(&catalog_path)
.map_err(|error| format!("could not read {}: {error}", catalog_path.display()))?;
let catalog: shared::RuntimeContentCatalog = ron::from_str(&catalog_source)
.map_err(|error| format!("invalid runtime content catalog: {error}"))?;
for runtime_path in catalog.records.iter().flat_map(|record| {
record
.texture
.as_ref()
.and_then(|texture| texture.processed_path.as_deref())
.into_iter()
.chain(
record
.material
.as_ref()
.and_then(|material| material.packed_arm_path.as_deref()),
)
}) {
let relative = runtime_path
.strip_prefix("assets/")
.ok_or_else(|| format!("runtime artifact is outside assets/: {runtime_path}"))?;
let relative = PathBuf::from(relative);
if !relative.starts_with(".import-cache/runtime") {
return Err(format!(
"runtime artifact is outside managed runtime storage: {runtime_path}"
));
}
let source = assets.join(&relative);
let metadata = fs::symlink_metadata(&source).map_err(|error| {
format!(
"runtime artifact is missing at {}: {error}",
source.display()
)
})?;
if metadata.file_type().is_symlink() || !metadata.is_file() {
return Err(format!(
"runtime artifact must be a regular file: {}",
source.display()
));
}
sources.push((relative, source));
}
sources.sort_by(|left, right| left.0.cmp(&right.0));
sources.dedup_by(|left, right| left.0 == right.0);
Ok(sources)
}
@@ -1755,7 +1899,7 @@ mod tests {
fs::write(root.join("assets/.index/registry.ron"), "[]").unwrap();
fs::write(
root.join("assets/project.ron"),
"(version:1,template_version:1,default_level:\"assets/levels/main.scn.ron\",asset_roots:[\"assets/levels\"],capabilities:[])",
"(version:2,template_version:2,default_level:\"assets/levels/main.scn.ron\",asset_roots:[\"assets\"],capabilities:[])",
)
.unwrap();
fs::write(
@@ -1884,6 +2028,10 @@ mod tests {
})})"#,
)
.unwrap();
let processed =
content_pipeline::scan_project(&root, &shared::AssetRegistryDocument::default())
.unwrap();
content_pipeline::publish_content_documents(&root, &processed.registry).unwrap();
let error = run(Options {
project_root: root.clone(),
profile_name: "release".into(),
@@ -1949,6 +2097,19 @@ mod tests {
fs::remove_dir_all(root).unwrap();
}
#[test]
fn package_output_identity_is_stable_after_output_creation() {
let root = fixture_root();
fs::create_dir_all(root.join("dist")).unwrap();
let before = resolve_output(&root, "dist/qa").unwrap();
fs::create_dir_all(&before).unwrap();
let after = resolve_output(&root, "dist/qa").unwrap();
assert_eq!(before, after);
assert_eq!(before.to_string_lossy(), after.to_string_lossy());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn build_profile_rejects_unsafe_target_triple() {
let root = fixture_root();
@@ -2153,6 +2314,28 @@ mod tests {
fs::remove_dir_all(root).unwrap();
}
#[test]
fn package_lock_accepts_an_equivalent_output_with_a_trailing_separator() {
let root = fixture_root();
let output = root.join("dist/release");
fs::create_dir_all(root.join("dist")).unwrap();
let lock_path = root.join("dist/.blacksite-release.lock");
let record = PackageLockRecord {
magic: PACKAGE_LOCK_MAGIC.into(),
schema_version: PACKAGE_SCHEMA_VERSION,
output: format!("{}/", output.display()),
pid: 1,
};
fs::write(&lock_path, serde_json::to_vec(&record).unwrap()).unwrap();
PackageLock::acquire(&output).unwrap();
let refreshed: PackageLockRecord =
serde_json::from_slice(&fs::read(&lock_path).unwrap()).unwrap();
assert_eq!(refreshed.output, output.to_string_lossy());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn unrecognized_lock_file_is_preserved_and_rejected() {
let root = fixture_root();
+239
View File
@@ -0,0 +1,239 @@
//! Deterministic, GPU-free content registry and runtime-catalog processor.
use content_pipeline::{
scan_project, serialize_registry, serialize_runtime_catalog, write_if_changed, REGISTRY_PATH,
RUNTIME_CATALOG_PATH,
};
use serde::Serialize;
use shared::{parse_asset_registry, AssetRegistryDocument};
use std::path::{Path, PathBuf};
#[derive(Serialize)]
struct CommandReport {
project: String,
check: bool,
registry_changed: bool,
runtime_catalog_changed: bool,
model_artifacts_changed: usize,
texture_artifacts_changed: usize,
material_artifacts_changed: usize,
processing: content_pipeline::ProcessingReport,
}
fn main() {
if let Err(error) = run() {
eprintln!("process-assets failed: {error}");
std::process::exit(1);
}
}
fn run() -> Result<(), String> {
let mut project = PathBuf::from(".");
let mut check = false;
let mut json = false;
let mut arguments = std::env::args().skip(1);
while let Some(argument) = arguments.next() {
match argument.as_str() {
"--project" => {
project = PathBuf::from(
arguments
.next()
.ok_or_else(|| "--project requires a path".to_string())?,
);
}
"--check" => check = true,
"--json" => json = true,
other => return Err(format!("unknown argument `{other}`")),
}
}
let project_display = project.to_string_lossy().into_owned();
let project = std::fs::canonicalize(&project)
.map_err(|error| format!("could not resolve project {project_display}: {error}"))?;
std::env::set_current_dir(&project)
.map_err(|error| format!("could not enter project {}: {error}", project.display()))?;
let project = PathBuf::from(".");
let registry_path = project.join(REGISTRY_PATH);
let previous = load_registry(&registry_path)?;
let mut processed = scan_project(&project, &previous)?;
let mut model_plans = Vec::new();
for record in processed
.registry
.records
.iter_mut()
.filter(|record| record.kind == shared::AssetKind::Model)
{
let plan = content_pipeline::plan_model_artifacts(record)
.map_err(|error| format!("could not process model {}: {error}", record.path))?;
*record = plan.record.clone();
model_plans.push(plan);
}
processed.runtime_catalog = shared::RuntimeContentCatalog::from(&processed.registry);
let mut texture_plans = Vec::new();
for record in processed
.registry
.records
.iter()
.filter(|record| record.kind == shared::AssetKind::Texture)
{
let plan = content_pipeline::plan_texture_artifact(&project, record)
.map_err(|error| format!("could not process Texture {}: {error}", record.path))?;
content_pipeline::apply_texture_plan_to_catalog(
&mut processed.runtime_catalog,
&record.id,
&plan,
)?;
texture_plans.push(plan);
}
let mut material_plans = Vec::new();
for record in processed.registry.records.iter().filter(|record| {
matches!(
record.kind,
shared::AssetKind::Material | shared::AssetKind::MaterialInstance
)
}) {
if let Some(plan) =
content_pipeline::plan_material_artifact(&project, record, &processed.registry)?
{
content_pipeline::apply_material_plan_to_catalog(
&mut processed.runtime_catalog,
&record.id,
&plan,
)?;
material_plans.push(plan);
}
}
processed.report.changed = processed.registry != previous;
let model_artifacts_changed = model_plans
.iter()
.filter(|plan| {
file_differs(&plan.static_path, &plan.static_bytes)
|| file_differs(&plan.animation_path, &plan.animation_bytes)
})
.count();
let texture_artifacts_changed = texture_plans
.iter()
.filter(|plan| !plan.reused && file_differs(&plan.output_path, &plan.output_bytes))
.count();
let material_artifacts_changed = material_plans
.iter()
.filter(|plan| !plan.reused && file_differs(&plan.output_path, &plan.output_bytes))
.count();
let registry_bytes = serialize_registry(&processed.registry)?;
let runtime_bytes = serialize_runtime_catalog(&processed.runtime_catalog)?;
let runtime_path = project.join(RUNTIME_CATALOG_PATH);
let registry_changed = file_differs(&registry_path, &registry_bytes);
let runtime_catalog_changed = file_differs(&runtime_path, &runtime_bytes);
let report = CommandReport {
project: project_display,
check,
registry_changed,
runtime_catalog_changed,
model_artifacts_changed,
texture_artifacts_changed,
material_artifacts_changed,
processing: processed.report,
};
if !check {
let mut publication_paths = model_plans
.iter()
.flat_map(|plan| [plan.static_path.clone(), plan.animation_path.clone()])
.collect::<Vec<_>>();
publication_paths.extend(texture_plans.iter().map(|plan| plan.output_path.clone()));
publication_paths.extend(material_plans.iter().map(|plan| plan.output_path.clone()));
publication_paths.push(registry_path.clone());
publication_paths.push(runtime_path.clone());
publication_paths.sort();
publication_paths.dedup();
let backups = publication_paths
.iter()
.map(|path| (path.clone(), std::fs::read(path).ok()))
.collect::<Vec<_>>();
let publication = (|| {
for plan in &model_plans {
content_pipeline::publish_model_artifacts(plan)?;
}
for plan in &texture_plans {
content_pipeline::publish_texture_artifact(plan)?;
}
for plan in &material_plans {
content_pipeline::publish_material_artifact(plan)?;
}
write_if_changed(&registry_path, &registry_bytes)?;
write_if_changed(&runtime_path, &runtime_bytes)?;
Ok::<(), String>(())
})();
if let Err(error) = publication {
for (path, bytes) in &backups {
restore_file(path, bytes.as_deref());
}
return Err(format!("headless content publication rolled back: {error}"));
}
}
if json {
println!(
"{}",
serde_json::to_string_pretty(&report)
.map_err(|error| format!("could not serialize report: {error}"))?
);
} else {
println!(
"process-assets: discovered={}, added={}, moved={}, removed={}, registry_changed={}, runtime_catalog_changed={}, model_artifacts_changed={}, texture_artifacts_changed={}, material_artifacts_changed={}, mode={}",
report.processing.discovered,
report.processing.added,
report.processing.moved,
report.processing.removed,
registry_changed,
runtime_catalog_changed,
model_artifacts_changed,
texture_artifacts_changed,
material_artifacts_changed,
if check { "check" } else { "write" }
);
for diagnostic in &report.processing.diagnostics {
eprintln!(" {}: {}", diagnostic.path, diagnostic.message);
}
}
if check
&& (registry_changed
|| runtime_catalog_changed
|| model_artifacts_changed > 0
|| texture_artifacts_changed > 0
|| material_artifacts_changed > 0)
{
return Err(
"generated content state is stale; run `cargo process-assets --project <root>`".into(),
);
}
Ok(())
}
fn load_registry(path: &Path) -> Result<AssetRegistryDocument, String> {
let source = match std::fs::read_to_string(path) {
Ok(source) => source,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(AssetRegistryDocument::default());
}
Err(error) => return Err(format!("could not read {}: {error}", path.display())),
};
let loaded = parse_asset_registry(&source)?;
if loaded.migration_required {
return Err(
"asset registry v1 requires `cargo upgrade-project --project <root> --apply`".into(),
);
}
Ok(loaded.document)
}
fn file_differs(path: &Path, expected: &[u8]) -> bool {
std::fs::read(path).map_or(true, |current| current != expected)
}
fn restore_file(path: &Path, bytes: Option<&[u8]>) {
if let Some(bytes) = bytes {
let _ = std::fs::write(path, bytes);
} else {
let _ = std::fs::remove_file(path);
}
}