spec_driven_docs/services/
self_manifest.rs1use camino::Utf8Path;
10
11use crate::adapters::fs::sha256_file;
12use crate::domain::manifest::{CANON_SOURCE, MANIFEST_PATH, Manifest, SCHEMA_VERSION};
13use crate::domain::ownership::{AdoptedEntry, IntegrationBlock, ManagedEntry};
14use crate::domain::profile::{DocsRoot, ProfileId};
15use crate::domain::version::CanonVersion;
16use crate::error::AppError;
17
18pub(crate) fn is_canon_checkout(root: &Utf8Path) -> bool {
19 std::fs::read_to_string(root.join("Cargo.toml"))
20 .is_ok_and(|cargo| cargo.contains("name = \"spec-driven-docs\""))
21}
22
23fn sorted_files(root: &Utf8Path, dir: &str, matches: impl Fn(&str) -> bool) -> Vec<String> {
24 let mut names: Vec<String> = root
25 .join(dir)
26 .read_dir_utf8()
27 .map(|entries| {
28 entries
29 .filter_map(Result::ok)
30 .map(|entry| entry.file_name().to_string())
31 .filter(|name| matches(name))
32 .collect()
33 })
34 .unwrap_or_default();
35 names.sort();
36 names
37 .into_iter()
38 .map(|name| format!("{dir}/{name}"))
39 .collect()
40}
41
42fn skill_files(root: &Utf8Path) -> Vec<String> {
44 let mut names: Vec<String> = root
45 .join("skills")
46 .read_dir_utf8()
47 .map(|entries| {
48 entries
49 .filter_map(Result::ok)
50 .filter(|entry| entry.path().is_dir())
51 .map(|entry| entry.file_name().to_string())
52 .collect()
53 })
54 .unwrap_or_default();
55 names.sort();
56 names
57 .into_iter()
58 .map(|name| format!("skills/{name}/SKILL.md"))
59 .collect()
60}
61
62pub fn regenerate(root: &Utf8Path) -> Result<String, AppError> {
69 if !is_canon_checkout(root) {
70 return Err(AppError::Refused("not the canon checkout".to_string()));
71 }
72
73 let mut managed = Vec::new();
74 #[allow(clippy::case_sensitive_file_extension_comparisons)]
78 let jsonc = |name: &str| name.ends_with(".jsonc");
79 for path in sorted_files(root, ".markdownlint", jsonc) {
80 managed.push(ManagedEntry {
81 source: path.clone().into(),
82 destination: path.clone().into(),
83 sha256: sha256_file(&root.join(&path))?,
84 });
85 }
86 for path in skill_files(root) {
87 managed.push(ManagedEntry {
88 source: path.clone().into(),
89 destination: path.clone().into(),
90 sha256: sha256_file(&root.join(&path))?,
91 });
92 }
93 for (path, _) in crate::embedded::shared_artifacts() {
96 let path = format!("skill-shared/{path}");
97 managed.push(ManagedEntry {
98 source: path.clone().into(),
99 destination: path.clone().into(),
100 sha256: sha256_file(&root.join(&path))?,
101 });
102 }
103 #[allow(clippy::case_sensitive_file_extension_comparisons)]
105 let spec = |name: &str| name.starts_with("SPEC-") && name.ends_with(".md");
106 let mut adopted_paths = sorted_files(root, "_docs/specs", spec);
107 for template in crate::domain::profile::CANON_TEMPLATES {
108 adopted_paths.push((*template).to_string());
109 }
110 let mut adopted = Vec::new();
111 for path in adopted_paths {
112 let digest = sha256_file(&root.join(&path))?;
113 adopted.push(AdoptedEntry {
114 source: path.clone().into(),
115 destination: path.into(),
116 sha256: digest.clone(),
117 baseline_sha256: digest,
118 });
119 }
120 let registry = "_docs/reference/tracking.yaml";
123 let registry_baseline = "templates/TEMPLATE-tracking.yaml";
124 if root.join(registry).is_file() {
125 adopted.push(AdoptedEntry {
126 source: registry_baseline.into(),
127 destination: registry.into(),
128 sha256: sha256_file(&root.join(registry))?,
129 baseline_sha256: sha256_file(&root.join(registry_baseline))?,
130 });
131 }
132
133 let config = std::fs::read_to_string(root.join(".pre-commit-config.yaml"))?;
134 let marker_hash = crate::domain::marker::block_hash(&config).ok_or_else(|| {
135 AppError::Refused("no managed block in .pre-commit-config.yaml".to_string())
136 })?;
137
138 let installed_at = crate::services::installer::recorded_field(root, "installed_at")
143 .and_then(|value| value.as_str().map(String::from))
144 .unwrap_or_else(|| {
145 jiff::Timestamp::now()
146 .strftime("%Y-%m-%dT%H:%M:%SZ")
147 .to_string()
148 });
149 let plan_zone = crate::services::installer::resolved_plan_zone(root, None)?;
150 let docs_scratch = crate::services::installer::resolved_docs_scratch(root, None)?;
151
152 let manifest = Manifest {
153 schema_version: SCHEMA_VERSION,
154 canon_version: CanonVersion::current(),
155 canon_source: CANON_SOURCE.to_string(),
156 profile: ProfileId::KnowledgeBase,
157 docs_root: DocsRoot::UnderscoreDocs,
158 installed_at,
159 plan_zone,
160 docs_scratch,
161 managed_files: managed,
162 adopted_files: adopted,
163 integration_blocks: vec![IntegrationBlock {
164 path: ".pre-commit-config.yaml".into(),
165 marker_hash,
166 }],
167 };
168 crate::adapters::fs::write_file(&root.join(MANIFEST_PATH), manifest.to_json().as_bytes())?;
169 Ok(format!("OK regenerated {MANIFEST_PATH}"))
170}