//! Every file the generator owns, keyed by its path RELATIVE to the repo root //! (POSIX separators, so the map is the same on any host) — and what drift //! means. //! //! The two destinations live in two Swift packages. The palette and the logo //! are the LOOK and render into Loom's `CottonPaper/Generated` //! (`apps/loom`, the package with no Rust in it), which the phone's `CottonUI` re-exports and the Mac //! imports without linking a screen (architect, 20 Oct 2026); the vocabulary, //! the emotions and the secret payload are the phone's and render into //! `CottonUI/Generated`. Each is rendered once, so a second copy can no longer //! drift out from under the first. //! The Mac half has no generated directory of its own since H3: its one file //! was the browser window's `Sections.swift`, and it went with the window. The //! kind-coverage guard behind it — every record kind placed or excused in //! writing — stays in Rust, in `cotton_core::design::sections` and its tests. //! //! Android receives its own Kotlin palette and mark from the same declarations. //! Both generated directories participate in drift detection. //! //! The phone's catalogue is a directory TREE rather than a flat name list, so //! the drift check works over relative paths and also names a file that is //! THERE and should not be: a stale colorset for a role somebody deleted would //! otherwise sit in the catalogue for ever, compiled into every build and named //! by nothing. use std::collections::BTreeMap; use std::path::Path; use cotton_core::design::palette::colorsets; use crate::emotions::render_emotions; use crate::payload::{render_kotlin_payload, render_payload}; use crate::retired::render_retired_terms; use crate::{ios, kotlin, mark}; /// Where the generated sources live, relative to the repo root: the phone's /// module, which the Mac half imports rather than carrying a second copy of. /// ONE directory since H3 — the Mac's own generated file was the browser /// window's section list, and it went with the window. pub const IOS_DIR: &str = "apps/cotton/Sources/CottonUI/Generated"; /// The look's generated sources: the palette, its catalogue and the logo — /// what the Mac draws with and the phone re-exports. pub const IOS_PAPER_DIR: &str = "apps/loom/Sources/CottonPaper/Generated"; pub const ANDROID_DIR: &str = "apps/cotton-android/app/src/main/kotlin/com/cottondb/android/generated"; pub const ANDROID_CORE_DIR: &str = "apps/cotton-android/core/src/main/kotlin/com/cottondb/android/generated"; /// Every generated file, in path order. pub fn generate() -> Result, String> { let mut files: BTreeMap = BTreeMap::new(); files.insert(format!("{IOS_PAPER_DIR}/Palette.swift"), ios::render_palette()); files.insert( format!("{IOS_DIR}/RetiredTerms.swift"), render_retired_terms()?, ); files.insert(format!("{IOS_DIR}/Emotions.swift"), render_emotions()); files.insert(format!("{IOS_PAPER_DIR}/CottonMark.swift"), mark::render()); files.insert(format!("{IOS_DIR}/SecretPayload.swift"), render_payload()); files.insert( format!("{IOS_PAPER_DIR}/Palette.xcassets/Contents.json"), ios::render_catalogue_root(), ); for (asset, hex) in colorsets() { files.insert( format!("{IOS_PAPER_DIR}/Palette.xcassets/{asset}.colorset/Contents.json"), ios::render_colorset(hex)?, ); } files.insert( format!("{ANDROID_DIR}/CottonPalette.kt"), kotlin::palette()?, ); files.insert(format!("{ANDROID_DIR}/CottonMark.kt"), kotlin::mark()); files.insert( format!("{ANDROID_CORE_DIR}/SecretPayload.kt"), render_kotlin_payload(), ); Ok(files) } /// Write every generated file under `root`, creating directories as needed. pub fn write(root: &Path) -> Result, String> { let files = generate()?; for (name, contents) in &files { let target = root.join(name); if let Some(parent) = target.parent() { std::fs::create_dir_all(parent) .map_err(|error| format!("{}: {error}", parent.display()))?; } std::fs::write(&target, contents) .map_err(|error| format!("{}: {error}", target.display()))?; } Ok(files) } /// Every file under `dir`, as paths relative to it with POSIX separators. fn walk(dir: &Path, prefix: &str, out: &mut Vec) { let Ok(entries) = std::fs::read_dir(dir) else { return; }; for entry in entries.flatten() { let path = entry.path(); let Some(name) = path.file_name().and_then(|name| name.to_str()) else { continue; }; let relative = if prefix.is_empty() { name.to_string() } else { format!("{prefix}/{name}") }; if path.is_dir() { walk(&path, &relative, out); } else { out.push(relative); } } } /// The generated paths under `root` that differ from a fresh generation — /// including a file that is THERE and should not be. pub fn drift(root: &Path) -> Result, String> { let files = generate()?; let mut differing: Vec = Vec::new(); for (name, wanted) in &files { let found = std::fs::read_to_string(root.join(name)).ok(); if found.as_deref() != Some(wanted.as_str()) { differing.push(name.clone()); } } for directory in [IOS_DIR, IOS_PAPER_DIR, ANDROID_DIR, ANDROID_CORE_DIR] { let mut found = Vec::new(); walk(&root.join(directory), "", &mut found); for name in found { let full = format!("{directory}/{name}"); if !files.contains_key(&full) { differing.push(format!("{full} (not generated — delete it)")); } } } differing.sort(); Ok(differing) } #[cfg(test)] #[path = "files_tests.rs"] mod tests;