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