|
| 1 | +use std::collections::BTreeSet; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +use crate::config::Context; |
| 7 | +use crate::output::{self, Format}; |
| 8 | + |
| 9 | +use super::io::{ |
| 10 | + collect_generated_doc_pages, prune_empty_doc_dirs, read_codewiki_meta, |
| 11 | + reject_symlinked_doc_path, safe_doc_path, |
| 12 | +}; |
| 13 | +use super::truth_digest::TRUTH_DIGEST_META_PATH; |
| 14 | +use super::{CODEWIKI_META_PATH, DEFAULT_OUT_DIR, OWNERSHIP_META_PATH}; |
| 15 | + |
| 16 | +#[derive(Debug, Serialize)] |
| 17 | +pub struct CodewikiPurgeSummary { |
| 18 | + pub command: &'static str, |
| 19 | + pub project_id: String, |
| 20 | + pub project_root: String, |
| 21 | + pub out_dir: String, |
| 22 | + pub markdown_removed: usize, |
| 23 | + pub metadata_removed: usize, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Default, PartialEq, Eq)] |
| 27 | +pub(crate) struct CodewikiPurgeCounts { |
| 28 | + pub(crate) markdown_removed: usize, |
| 29 | + pub(crate) metadata_removed: usize, |
| 30 | +} |
| 31 | + |
| 32 | +pub fn run_purge( |
| 33 | + ctx: &Context, |
| 34 | + out: Option<String>, |
| 35 | + force: bool, |
| 36 | + format: Format, |
| 37 | +) -> anyhow::Result<()> { |
| 38 | + let out_dir = out.unwrap_or_else(|| DEFAULT_OUT_DIR.to_string()); |
| 39 | + let out_path = Path::new(&out_dir); |
| 40 | + if !force { |
| 41 | + eprintln!( |
| 42 | + "This will purge generated CodeWiki output under {} for project {}. Re-run with --force to confirm.", |
| 43 | + out_path.display(), |
| 44 | + ctx.project_id |
| 45 | + ); |
| 46 | + return Ok(()); |
| 47 | + } |
| 48 | + |
| 49 | + let counts = purge_generated_output(out_path)?; |
| 50 | + let summary = CodewikiPurgeSummary { |
| 51 | + command: "codewiki purge", |
| 52 | + project_id: ctx.project_id.clone(), |
| 53 | + project_root: ctx.project_root.display().to_string(), |
| 54 | + out_dir, |
| 55 | + markdown_removed: counts.markdown_removed, |
| 56 | + metadata_removed: counts.metadata_removed, |
| 57 | + }; |
| 58 | + match format { |
| 59 | + Format::Json => output::print_json(&summary), |
| 60 | + Format::Text => output::print_text(&format!( |
| 61 | + "purged {} generated Markdown pages and {} metadata files from {}", |
| 62 | + summary.markdown_removed, summary.metadata_removed, summary.out_dir |
| 63 | + )), |
| 64 | + }?; |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +pub(crate) fn purge_generated_output(out_dir: &Path) -> anyhow::Result<CodewikiPurgeCounts> { |
| 69 | + let meta = read_codewiki_meta(out_dir)?; |
| 70 | + let mut generated_docs = BTreeSet::new(); |
| 71 | + generated_docs.extend(meta.docs.keys().cloned()); |
| 72 | + generated_docs.extend(meta.generated_docs); |
| 73 | + generated_docs.extend(collect_generated_doc_pages(out_dir)?); |
| 74 | + |
| 75 | + let mut counts = CodewikiPurgeCounts::default(); |
| 76 | + for doc_path in generated_docs { |
| 77 | + if !doc_path.ends_with(".md") { |
| 78 | + continue; |
| 79 | + } |
| 80 | + if remove_generated_path(out_dir, &doc_path)? { |
| 81 | + counts.markdown_removed += 1; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + for metadata_path in [ |
| 86 | + CODEWIKI_META_PATH, |
| 87 | + OWNERSHIP_META_PATH, |
| 88 | + TRUTH_DIGEST_META_PATH, |
| 89 | + ] { |
| 90 | + if remove_generated_path(out_dir, metadata_path)? { |
| 91 | + counts.metadata_removed += 1; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Ok(counts) |
| 96 | +} |
| 97 | + |
| 98 | +fn remove_generated_path(out_dir: &Path, relative_path: &str) -> anyhow::Result<bool> { |
| 99 | + let target = safe_doc_path(out_dir, relative_path)?; |
| 100 | + reject_symlinked_doc_path(out_dir, &target)?; |
| 101 | + match std::fs::remove_file(&target) { |
| 102 | + Ok(()) => { |
| 103 | + prune_empty_doc_dirs(out_dir, &target)?; |
| 104 | + Ok(true) |
| 105 | + } |
| 106 | + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false), |
| 107 | + Err(error) => Err(error.into()), |
| 108 | + } |
| 109 | +} |
0 commit comments