|
| 1 | +use std::fs; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +use tracing::info; |
| 5 | + |
| 6 | +use crate::domain::plan::Plan; |
| 7 | +use crate::error::Result; |
| 8 | + |
| 9 | +/// Files and directories that wiggum generates. |
| 10 | +const GENERATED_FILES: &[&str] = &[ |
| 11 | + "PROGRESS.md", |
| 12 | + "IMPLEMENTATION_PLAN.md", |
| 13 | + "AGENTS.md", |
| 14 | + ".vscode/orchestrator.prompt.md", |
| 15 | +]; |
| 16 | + |
| 17 | +/// Collect all wiggum-generated paths that exist on disk. |
| 18 | +/// |
| 19 | +/// # Errors |
| 20 | +/// |
| 21 | +/// Returns an error if task resolution fails. |
| 22 | +pub fn collect_targets(plan: &Plan, project_path: &Path) -> Result<Vec<PathBuf>> { |
| 23 | + let mut targets = Vec::new(); |
| 24 | + |
| 25 | + for file in GENERATED_FILES { |
| 26 | + let path = project_path.join(file); |
| 27 | + if path.exists() { |
| 28 | + targets.push(path); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Collect individual task files from the plan so we only remove |
| 33 | + // files wiggum would have generated — not hand-written files that |
| 34 | + // happen to live in tasks/. |
| 35 | + let resolved = plan.resolve_tasks()?; |
| 36 | + let tasks_dir = project_path.join("tasks"); |
| 37 | + if tasks_dir.is_dir() { |
| 38 | + for t in &resolved { |
| 39 | + let filename = format!("T{:02}-{}.md", t.number, t.slug); |
| 40 | + let path = tasks_dir.join(&filename); |
| 41 | + if path.exists() { |
| 42 | + targets.push(path); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // If the tasks dir is empty after removal, mark it for cleanup too |
| 48 | + targets.push(tasks_dir); |
| 49 | + |
| 50 | + targets.sort(); |
| 51 | + targets.dedup(); |
| 52 | + Ok(targets) |
| 53 | +} |
| 54 | + |
| 55 | +/// Remove wiggum-generated artifacts from the project directory. |
| 56 | +/// |
| 57 | +/// Returns the list of paths that were actually removed. |
| 58 | +/// |
| 59 | +/// # Errors |
| 60 | +/// |
| 61 | +/// Returns an error if task resolution or file removal fails. |
| 62 | +pub fn remove_artifacts(plan: &Plan, project_path: &Path) -> Result<Vec<PathBuf>> { |
| 63 | + let targets = collect_targets(plan, project_path)?; |
| 64 | + let mut removed = Vec::new(); |
| 65 | + |
| 66 | + // Remove files first, then directories (so dirs are empty when we try) |
| 67 | + for path in targets.iter().filter(|p| p.is_file()) { |
| 68 | + fs::remove_file(path)?; |
| 69 | + info!("Removed file: {}", path.display()); |
| 70 | + removed.push(path.clone()); |
| 71 | + } |
| 72 | + |
| 73 | + for path in targets.iter().filter(|p| p.is_dir()) { |
| 74 | + if is_dir_empty(path) { |
| 75 | + fs::remove_dir(path)?; |
| 76 | + info!("Removed directory: {}", path.display()); |
| 77 | + removed.push(path.clone()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Clean up .vscode/ if empty after removing orchestrator.prompt.md |
| 82 | + let vscode_dir = project_path.join(".vscode"); |
| 83 | + if vscode_dir.is_dir() && is_dir_empty(&vscode_dir) { |
| 84 | + fs::remove_dir(&vscode_dir)?; |
| 85 | + info!("Removed empty directory: {}", vscode_dir.display()); |
| 86 | + removed.push(vscode_dir); |
| 87 | + } |
| 88 | + |
| 89 | + Ok(removed) |
| 90 | +} |
| 91 | + |
| 92 | +fn is_dir_empty(path: &Path) -> bool { |
| 93 | + path.read_dir() |
| 94 | + .map(|mut entries| entries.next().is_none()) |
| 95 | + .unwrap_or(false) |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +#[allow(clippy::unwrap_used)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + use crate::domain::plan::Plan; |
| 103 | + use std::fs; |
| 104 | + use tempfile::TempDir; |
| 105 | + |
| 106 | + fn sample_plan(path: &str) -> Plan { |
| 107 | + let toml = format!( |
| 108 | + r#" |
| 109 | +[project] |
| 110 | +name = "test-project" |
| 111 | +description = "test" |
| 112 | +language = "rust" |
| 113 | +path = "{path}" |
| 114 | +
|
| 115 | +[[phases]] |
| 116 | +name = "Phase 1" |
| 117 | +order = 1 |
| 118 | +
|
| 119 | +[[phases.tasks]] |
| 120 | +slug = "setup" |
| 121 | +title = "Project setup" |
| 122 | +goal = "Set up the project" |
| 123 | +depends_on = [] |
| 124 | +
|
| 125 | +[[phases.tasks]] |
| 126 | +slug = "model" |
| 127 | +title = "Domain model" |
| 128 | +goal = "Define domain types" |
| 129 | +depends_on = ["setup"] |
| 130 | +"# |
| 131 | + ); |
| 132 | + Plan::from_toml(&toml).unwrap() |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn collect_targets_finds_existing_files() { |
| 137 | + let tmp = TempDir::new().unwrap(); |
| 138 | + let root = tmp.path(); |
| 139 | + |
| 140 | + // Create the files wiggum would generate |
| 141 | + fs::write(root.join("PROGRESS.md"), "progress").unwrap(); |
| 142 | + fs::write(root.join("IMPLEMENTATION_PLAN.md"), "plan").unwrap(); |
| 143 | + fs::write(root.join("AGENTS.md"), "agents").unwrap(); |
| 144 | + fs::create_dir_all(root.join(".vscode")).unwrap(); |
| 145 | + fs::write(root.join(".vscode/orchestrator.prompt.md"), "orch").unwrap(); |
| 146 | + fs::create_dir_all(root.join("tasks")).unwrap(); |
| 147 | + fs::write(root.join("tasks/T01-setup.md"), "task1").unwrap(); |
| 148 | + fs::write(root.join("tasks/T02-model.md"), "task2").unwrap(); |
| 149 | + |
| 150 | + let plan = sample_plan(&root.to_string_lossy()); |
| 151 | + let targets = collect_targets(&plan, root).unwrap(); |
| 152 | + |
| 153 | + assert!(targets.iter().any(|p| p.ends_with("PROGRESS.md"))); |
| 154 | + assert!( |
| 155 | + targets |
| 156 | + .iter() |
| 157 | + .any(|p| p.ends_with("IMPLEMENTATION_PLAN.md")) |
| 158 | + ); |
| 159 | + assert!(targets.iter().any(|p| p.ends_with("AGENTS.md"))); |
| 160 | + assert!( |
| 161 | + targets |
| 162 | + .iter() |
| 163 | + .any(|p| p.ends_with("orchestrator.prompt.md")) |
| 164 | + ); |
| 165 | + assert!(targets.iter().any(|p| p.ends_with("T01-setup.md"))); |
| 166 | + assert!(targets.iter().any(|p| p.ends_with("T02-model.md"))); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn collect_targets_ignores_missing_files() { |
| 171 | + let tmp = TempDir::new().unwrap(); |
| 172 | + let root = tmp.path(); |
| 173 | + |
| 174 | + let plan = sample_plan(&root.to_string_lossy()); |
| 175 | + let targets = collect_targets(&plan, root).unwrap(); |
| 176 | + |
| 177 | + // Only the tasks/ dir entry (which doesn't exist either) |
| 178 | + // should be present — all file entries are skipped |
| 179 | + assert!(!targets.iter().any(|p| p.is_file())); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn remove_artifacts_deletes_generated_files() { |
| 184 | + let tmp = TempDir::new().unwrap(); |
| 185 | + let root = tmp.path(); |
| 186 | + |
| 187 | + fs::write(root.join("PROGRESS.md"), "progress").unwrap(); |
| 188 | + fs::write(root.join("IMPLEMENTATION_PLAN.md"), "plan").unwrap(); |
| 189 | + fs::write(root.join("AGENTS.md"), "agents").unwrap(); |
| 190 | + fs::create_dir_all(root.join(".vscode")).unwrap(); |
| 191 | + fs::write(root.join(".vscode/orchestrator.prompt.md"), "orch").unwrap(); |
| 192 | + fs::create_dir_all(root.join("tasks")).unwrap(); |
| 193 | + fs::write(root.join("tasks/T01-setup.md"), "task1").unwrap(); |
| 194 | + fs::write(root.join("tasks/T02-model.md"), "task2").unwrap(); |
| 195 | + |
| 196 | + // Also create a non-wiggum file that should survive |
| 197 | + fs::write(root.join("Cargo.toml"), "[package]").unwrap(); |
| 198 | + |
| 199 | + let plan = sample_plan(&root.to_string_lossy()); |
| 200 | + let removed = remove_artifacts(&plan, root).unwrap(); |
| 201 | + |
| 202 | + assert!(!root.join("PROGRESS.md").exists()); |
| 203 | + assert!(!root.join("IMPLEMENTATION_PLAN.md").exists()); |
| 204 | + assert!(!root.join("AGENTS.md").exists()); |
| 205 | + assert!(!root.join(".vscode/orchestrator.prompt.md").exists()); |
| 206 | + assert!(!root.join(".vscode").exists()); // empty dir cleaned up |
| 207 | + assert!(!root.join("tasks/T01-setup.md").exists()); |
| 208 | + assert!(!root.join("tasks/T02-model.md").exists()); |
| 209 | + assert!(!root.join("tasks").exists()); // empty dir cleaned up |
| 210 | + |
| 211 | + // Non-wiggum file survives |
| 212 | + assert!(root.join("Cargo.toml").exists()); |
| 213 | + |
| 214 | + assert!(removed.len() >= 7); |
| 215 | + } |
| 216 | + |
| 217 | + #[test] |
| 218 | + fn remove_artifacts_preserves_non_wiggum_task_files() { |
| 219 | + let tmp = TempDir::new().unwrap(); |
| 220 | + let root = tmp.path(); |
| 221 | + |
| 222 | + fs::create_dir_all(root.join("tasks")).unwrap(); |
| 223 | + fs::write(root.join("tasks/T01-setup.md"), "task1").unwrap(); |
| 224 | + fs::write(root.join("tasks/my-custom-notes.md"), "keep me").unwrap(); |
| 225 | + |
| 226 | + let plan = sample_plan(&root.to_string_lossy()); |
| 227 | + remove_artifacts(&plan, root).unwrap(); |
| 228 | + |
| 229 | + assert!(!root.join("tasks/T01-setup.md").exists()); |
| 230 | + // Custom file survives |
| 231 | + assert!(root.join("tasks/my-custom-notes.md").exists()); |
| 232 | + // tasks/ dir survives because it's not empty |
| 233 | + assert!(root.join("tasks").is_dir()); |
| 234 | + } |
| 235 | + |
| 236 | + #[test] |
| 237 | + fn remove_artifacts_preserves_non_wiggum_vscode_files() { |
| 238 | + let tmp = TempDir::new().unwrap(); |
| 239 | + let root = tmp.path(); |
| 240 | + |
| 241 | + fs::create_dir_all(root.join(".vscode")).unwrap(); |
| 242 | + fs::write(root.join(".vscode/orchestrator.prompt.md"), "orch").unwrap(); |
| 243 | + fs::write(root.join(".vscode/settings.json"), "{}").unwrap(); |
| 244 | + |
| 245 | + let plan = sample_plan(&root.to_string_lossy()); |
| 246 | + remove_artifacts(&plan, root).unwrap(); |
| 247 | + |
| 248 | + assert!(!root.join(".vscode/orchestrator.prompt.md").exists()); |
| 249 | + // settings.json survives |
| 250 | + assert!(root.join(".vscode/settings.json").exists()); |
| 251 | + // .vscode/ dir survives because it's not empty |
| 252 | + assert!(root.join(".vscode").is_dir()); |
| 253 | + } |
| 254 | +} |
0 commit comments