1use clap::{CommandFactory, Parser};
2
3use std::fs;
4use std::io::{self, Write};
5use std::path::{Path, PathBuf};
6use std::time::Instant;
7
8pub mod cache;
9pub mod cli;
10pub mod config;
11pub mod config_resolver;
12pub mod diff;
13pub mod file_utils;
14pub mod markdown;
15pub mod state;
16pub mod token_count;
17pub mod tree;
18pub mod tree_sitter;
19
20use std::fs::File;
21
22use cache::CacheManager;
23use cli::Args;
24use config::{Config, load_config_from_path};
25use diff::render_per_file_diffs;
26use file_utils::{collect_files, confirm_overwrite, confirm_processing};
27use markdown::generate_markdown;
28use state::{ProjectState, StateComparison};
29use token_count::{count_file_tokens, count_tree_tokens, estimate_tokens};
30use tree::{build_file_tree, print_tree};
31
32#[derive(Debug, Clone)]
34pub struct DiffConfig {
35 pub context_lines: usize,
36 pub enabled: bool,
37 pub diff_only: bool,
38}
39
40impl Default for DiffConfig {
41 fn default() -> Self {
42 Self {
43 context_lines: 3,
44 enabled: false,
45 diff_only: false,
46 }
47 }
48}
49
50pub trait Prompter {
51 fn confirm_processing(&self, file_count: usize) -> io::Result<bool>;
52 fn confirm_overwrite(&self, file_path: &str) -> io::Result<bool>;
53}
54
55pub struct DefaultPrompter;
56
57impl Prompter for DefaultPrompter {
58 fn confirm_processing(&self, file_count: usize) -> io::Result<bool> {
59 confirm_processing(file_count)
60 }
61 fn confirm_overwrite(&self, file_path: &str) -> io::Result<bool> {
62 confirm_overwrite(file_path)
63 }
64}
65
66pub fn run_with_args(args: Args, config: Config, prompter: &impl Prompter) -> io::Result<()> {
67 let start_time = Instant::now();
68
69 let silent = std::env::var("CB_SILENT")
70 .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
71 .unwrap_or(false);
72
73 let final_args = args;
75 let mut resolved_base = PathBuf::from(&final_args.input);
78 let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
79 if resolved_base == Path::new(".")
80 && !cwd.join("context-builder.toml").exists()
81 && let Some(output_parent) = Path::new(&final_args.output).parent()
82 && output_parent
83 .file_name()
84 .map(|n| n == "output")
85 .unwrap_or(false)
86 && let Some(project_root) = output_parent.parent()
87 && project_root.join("context-builder.toml").exists()
88 {
89 resolved_base = project_root.to_path_buf();
90 }
91 let base_path = resolved_base.as_path();
92
93 if !base_path.exists() || !base_path.is_dir() {
94 if !silent {
95 eprintln!(
96 "Error: The specified input directory '{}' does not exist or is not a directory.",
97 final_args.input
98 );
99 }
100 return Err(io::Error::new(
101 io::ErrorKind::NotFound,
102 format!(
103 "Input directory '{}' does not exist or is not a directory",
104 final_args.input
105 ),
106 ));
107 }
108
109 let diff_config = if config.auto_diff.unwrap_or(false) {
111 Some(DiffConfig {
112 context_lines: config.diff_context_lines.unwrap_or(3),
113 enabled: true,
114 diff_only: final_args.diff_only,
115 })
116 } else {
117 None
118 };
119
120 if !final_args.preview
121 && !final_args.token_count
122 && Path::new(&final_args.output).exists()
123 && !final_args.yes
124 && !prompter.confirm_overwrite(&final_args.output)?
125 {
126 if !silent {
127 println!("Operation cancelled.");
128 }
129 return Err(io::Error::new(
130 io::ErrorKind::Interrupted,
131 "Operation cancelled by user",
132 ));
133 }
134
135 let mut auto_ignores: Vec<String> = vec![".context-builder".to_string()];
137
138 let output_path = Path::new(&final_args.output);
140 if let Ok(rel_output) = output_path.strip_prefix(base_path) {
141 if config.timestamped_output == Some(true) {
143 if let (Some(parent), Some(stem), Some(ext)) = (
145 rel_output.parent(),
146 output_path.file_stem().and_then(|s| s.to_str()),
147 output_path.extension().and_then(|s| s.to_str()),
148 ) {
149 let base_stem = if let Some(ref cfg_output) = config.output {
153 Path::new(cfg_output)
154 .file_stem()
155 .and_then(|s| s.to_str())
156 .unwrap_or(stem)
157 .to_string()
158 } else {
159 stem.to_string()
160 };
161 let glob = if parent == Path::new("") {
162 format!("{}_*.{}", base_stem, ext)
163 } else {
164 format!("{}/{}_*.{}", parent.display(), base_stem, ext)
165 };
166 auto_ignores.push(glob);
167 }
168 } else {
169 auto_ignores.push(rel_output.to_string_lossy().to_string());
171 }
172 } else {
173 let output_str = final_args.output.clone();
175 if config.timestamped_output == Some(true) {
176 if let (Some(stem), Some(ext)) = (
177 output_path.file_stem().and_then(|s| s.to_str()),
178 output_path.extension().and_then(|s| s.to_str()),
179 ) {
180 let base_stem = if let Some(ref cfg_output) = config.output {
181 Path::new(cfg_output)
182 .file_stem()
183 .and_then(|s| s.to_str())
184 .unwrap_or(stem)
185 .to_string()
186 } else {
187 stem.to_string()
188 };
189 if let Some(parent) = output_path.parent() {
190 let parent_str = parent.to_string_lossy();
191 if parent_str.is_empty() || parent_str == "." {
192 auto_ignores.push(format!("{}_*.{}", base_stem, ext));
193 } else {
194 auto_ignores.push(format!("{}/{}_*.{}", parent_str, base_stem, ext));
195 }
196 }
197 }
198 } else {
199 auto_ignores.push(output_str);
200 }
201 }
202
203 if let Some(ref output_folder) = config.output_folder {
205 auto_ignores.push(output_folder.clone());
206 }
207
208 let files = collect_files(
209 base_path,
210 &final_args.filter,
211 &final_args.ignore,
212 &auto_ignores,
213 )?;
214 let debug_config = std::env::var("CB_DEBUG_CONFIG").is_ok();
215 if debug_config {
216 eprintln!("[DEBUG][CONFIG] Args: {:?}", final_args);
217 eprintln!("[DEBUG][CONFIG] Raw Config: {:?}", config);
218 eprintln!("[DEBUG][CONFIG] Auto-ignores: {:?}", auto_ignores);
219 eprintln!("[DEBUG][CONFIG] Collected {} files", files.len());
220 for f in &files {
221 eprintln!("[DEBUG][CONFIG] - {}", f.path().display());
222 }
223 }
224
225 if !silent {
227 const LARGE_FILE_THRESHOLD: u64 = 100 * 1024; let mut large_files: Vec<(String, u64)> = Vec::new();
229 let mut total_size: u64 = 0;
230
231 for entry in &files {
232 if let Ok(metadata) = entry.path().metadata() {
233 let size = metadata.len();
234 total_size += size;
235 if size > LARGE_FILE_THRESHOLD {
236 let rel_path = entry
237 .path()
238 .strip_prefix(base_path)
239 .unwrap_or(entry.path())
240 .to_string_lossy()
241 .to_string();
242 large_files.push((rel_path, size));
243 }
244 }
245 }
246
247 if !large_files.is_empty() {
248 large_files.sort_by(|a, b| b.1.cmp(&a.1)); eprintln!(
250 "\nā {} large file(s) detected (>{} KB):",
251 large_files.len(),
252 LARGE_FILE_THRESHOLD / 1024
253 );
254 for (path, size) in large_files.iter().take(5) {
255 eprintln!(" {:>8} KB {}", size / 1024, path);
256 }
257 if large_files.len() > 5 {
258 eprintln!(" ... and {} more", large_files.len() - 5);
259 }
260 eprintln!(
261 " Total context size: {} KB across {} files\n",
262 total_size / 1024,
263 files.len()
264 );
265 }
266 }
267 let file_tree = build_file_tree(&files, base_path);
268
269 if final_args.preview {
270 if !silent {
271 println!("\n# File Tree Structure (Preview)\n");
272 print_tree(&file_tree, 0);
273 }
274 if !final_args.token_count {
275 return Ok(());
276 }
277 }
278
279 if final_args.token_count {
280 if !silent {
281 println!("\n# Token Count Estimation\n");
282 let mut total_tokens = 0;
283 total_tokens += estimate_tokens("# Directory Structure Report\n\n");
284 if !final_args.filter.is_empty() {
285 total_tokens += estimate_tokens(&format!(
286 "This document contains files from the `{}` directory with extensions: {} \n",
287 final_args.input,
288 final_args.filter.join(", ")
289 ));
290 } else {
291 total_tokens += estimate_tokens(&format!(
292 "This document contains all files from the `{}` directory, optimized for LLM consumption.\n",
293 final_args.input
294 ));
295 }
296 if !final_args.ignore.is_empty() {
297 total_tokens += estimate_tokens(&format!(
298 "Custom ignored patterns: {} \n",
299 final_args.ignore.join(", ")
300 ));
301 }
302 total_tokens += estimate_tokens("Content hash: 0000000000000000\n\n");
303 total_tokens += estimate_tokens("## File Tree Structure\n\n");
304 let tree_tokens = count_tree_tokens(&file_tree, 0);
305 total_tokens += tree_tokens;
306 let file_tokens: usize = files
307 .iter()
308 .map(|entry| count_file_tokens(base_path, entry, final_args.line_numbers))
309 .sum();
310 total_tokens += file_tokens;
311 println!("Estimated total tokens: {}", total_tokens);
312 println!("File tree tokens: {}", tree_tokens);
313 println!("File content tokens: {}", file_tokens);
314 }
315 return Ok(());
316 }
317
318 if !final_args.yes && !prompter.confirm_processing(files.len())? {
319 if !silent {
320 println!("Operation cancelled.");
321 }
322 return Err(io::Error::new(
323 io::ErrorKind::Interrupted,
324 "Operation cancelled by user",
325 ));
326 }
327
328 if config.auto_diff.unwrap_or(false) {
333 let mut effective_config = config.clone();
338 if !final_args.filter.is_empty() {
340 effective_config.filter = Some(final_args.filter.clone());
341 }
342 if !final_args.ignore.is_empty() {
343 effective_config.ignore = Some(final_args.ignore.clone());
344 }
345 effective_config.line_numbers = Some(final_args.line_numbers);
346
347 let current_state = ProjectState::from_files(
349 &files,
350 base_path,
351 &effective_config,
352 final_args.line_numbers,
353 )?;
354
355 let cache_manager = CacheManager::new(base_path, &effective_config);
357 let previous_state = match cache_manager.read_cache() {
358 Ok(state) => state,
359 Err(e) => {
360 if !silent {
361 eprintln!(
362 "Warning: Failed to read cache (proceeding without diff): {}",
363 e
364 );
365 }
366 None
367 }
368 };
369
370 let diff_cfg = diff_config.as_ref().unwrap();
371
372 let effective_previous = if let Some(prev) = previous_state.as_ref() {
374 if prev.config_hash != current_state.config_hash {
375 None
377 } else {
378 Some(prev)
379 }
380 } else {
381 None
382 };
383
384 let comparison = effective_previous.map(|prev| current_state.compare_with(prev));
386
387 let debug_autodiff = std::env::var("CB_DEBUG_AUTODIFF").is_ok();
388 if debug_autodiff {
389 eprintln!(
390 "[DEBUG][AUTODIFF] cache file: {}",
391 cache_manager.debug_cache_file_path().display()
392 );
393 eprintln!(
394 "[DEBUG][AUTODIFF] config_hash current={} prev={:?} invalidated={}",
395 current_state.config_hash,
396 previous_state.as_ref().map(|s| s.config_hash.clone()),
397 effective_previous.is_none() && previous_state.is_some()
398 );
399 eprintln!("[DEBUG][AUTODIFF] effective_config: {:?}", effective_config);
400 if let Some(prev) = previous_state.as_ref() {
401 eprintln!("[DEBUG][AUTODIFF] raw previous files: {}", prev.files.len());
402 }
403 if let Some(prev) = effective_previous {
404 eprintln!(
405 "[DEBUG][AUTODIFF] effective previous files: {}",
406 prev.files.len()
407 );
408 for k in prev.files.keys() {
409 eprintln!(" PREV: {}", k.display());
410 }
411 }
412 eprintln!(
413 "[DEBUG][AUTODIFF] current files: {}",
414 current_state.files.len()
415 );
416 for k in current_state.files.keys() {
417 eprintln!(" CURR: {}", k.display());
418 }
419 }
420
421 let cwd = std::env::current_dir().unwrap_or_else(|_| base_path.to_path_buf());
426 let sorted_paths: Vec<PathBuf> = files
427 .iter()
428 .map(|entry| {
429 entry
430 .path()
431 .strip_prefix(base_path)
432 .or_else(|_| entry.path().strip_prefix(&cwd))
433 .map(|p| p.to_path_buf())
434 .unwrap_or_else(|_| {
435 entry
436 .path()
437 .file_name()
438 .map(PathBuf::from)
439 .unwrap_or_else(|| entry.path().to_path_buf())
440 })
441 })
442 .collect();
443
444 let ts_config = markdown::TreeSitterConfig {
446 signatures: final_args.signatures,
447 structure: final_args.structure,
448 truncate: final_args.truncate.clone(),
449 visibility: final_args.visibility.clone(),
450 };
451
452 let mut final_doc = generate_markdown_with_diff(
454 ¤t_state,
455 comparison.as_ref(),
456 &final_args,
457 &file_tree,
458 diff_cfg,
459 &sorted_paths,
460 &ts_config,
461 )?;
462
463 if let Some(max_tokens) = final_args.max_tokens {
465 let max_bytes = max_tokens.saturating_mul(4);
466 if final_doc.len() > max_bytes {
467 let mut truncate_at = max_bytes;
469 while truncate_at > 0 && !final_doc.is_char_boundary(truncate_at) {
470 truncate_at -= 1;
471 }
472 final_doc.truncate(truncate_at);
473
474 let fence_count = final_doc.matches("\n```").count()
478 + if final_doc.starts_with("```") { 1 } else { 0 };
479 if fence_count % 2 != 0 {
480 final_doc.push_str("\n```\n");
481 }
482
483 final_doc.push_str("\n---\n\n");
484 final_doc.push_str(&format!(
485 "_Output truncated: exceeded {} token budget (estimated)._\n",
486 max_tokens
487 ));
488 }
489 }
490
491 let output_path = Path::new(&final_args.output);
493 if let Some(parent) = output_path.parent()
494 && !parent.exists()
495 && let Err(e) = fs::create_dir_all(parent)
496 {
497 return Err(io::Error::other(format!(
498 "Failed to create output directory {}: {}",
499 parent.display(),
500 e
501 )));
502 }
503 let mut final_output = fs::File::create(output_path)?;
504 final_output.write_all(final_doc.as_bytes())?;
505
506 if let Err(e) = cache_manager.write_cache(¤t_state)
508 && !silent
509 {
510 eprintln!("Warning: failed to update state cache: {}", e);
511 }
512
513 let duration = start_time.elapsed();
514 if !silent {
515 if let Some(comp) = &comparison {
516 if comp.summary.has_changes() {
517 println!(
518 "Documentation created successfully with {} changes: {}",
519 comp.summary.total_changes, final_args.output
520 );
521 } else {
522 println!(
523 "Documentation created successfully (no changes detected): {}",
524 final_args.output
525 );
526 }
527 } else {
528 println!(
529 "Documentation created successfully (initial state): {}",
530 final_args.output
531 );
532 }
533 println!("Processing time: {:.2?}", duration);
534
535 let output_bytes = final_doc.len();
537 print_context_window_warning(output_bytes, final_args.max_tokens);
538 }
539 return Ok(());
540 }
541
542 let ts_config = markdown::TreeSitterConfig {
545 signatures: final_args.signatures,
546 structure: final_args.structure,
547 truncate: final_args.truncate.clone(),
548 visibility: final_args.visibility.clone(),
549 };
550
551 if !silent && (ts_config.signatures || ts_config.structure || ts_config.truncate == "smart") {
553 #[cfg(not(feature = "tree-sitter-base"))]
554 {
555 eprintln!("ā ļø --signatures/--structure/--truncate smart require tree-sitter support.");
556 eprintln!(" Build with: cargo build --features tree-sitter-all");
557 eprintln!(" Falling back to standard output.\n");
558 }
559 }
560
561 generate_markdown(
562 &final_args.output,
563 &final_args.input,
564 &final_args.filter,
565 &final_args.ignore,
566 &file_tree,
567 &files,
568 base_path,
569 final_args.line_numbers,
570 config.encoding_strategy.as_deref(),
571 final_args.max_tokens,
572 &ts_config,
573 )?;
574
575 let duration = start_time.elapsed();
576 if !silent {
577 println!("Documentation created successfully: {}", final_args.output);
578 println!("Processing time: {:.2?}", duration);
579
580 let output_bytes = fs::metadata(&final_args.output)
582 .map(|m| m.len() as usize)
583 .unwrap_or(0);
584 print_context_window_warning(output_bytes, final_args.max_tokens);
585 }
586
587 Ok(())
588}
589
590fn print_context_window_warning(output_bytes: usize, max_tokens: Option<usize>) {
595 let estimated_tokens = output_bytes / 4;
596
597 println!("Estimated tokens: ~{}K", estimated_tokens / 1000);
598
599 if max_tokens.is_some() {
601 return;
602 }
603
604 const RECOMMENDED_LIMIT: usize = 128_000;
605
606 if estimated_tokens <= RECOMMENDED_LIMIT {
607 return;
608 }
609
610 eprintln!();
611 eprintln!(
612 "ā ļø Output is ~{}K tokens ā recommended limit is 128K for effective LLM context.",
613 estimated_tokens / 1000
614 );
615 eprintln!(" Large contexts degrade response quality. Consider narrowing the scope:");
616 eprintln!();
617 eprintln!(" ⢠--max-tokens 100000 Cap output to a token budget");
618 eprintln!(" ⢠--filter rs,toml Include only specific file types");
619 eprintln!(" ⢠--ignore docs,assets Exclude directories by name");
620 eprintln!(" ⢠--token-count Preview size without generating");
621 eprintln!();
622}
623
624fn generate_markdown_with_diff(
626 current_state: &ProjectState,
627 comparison: Option<&StateComparison>,
628 args: &Args,
629 file_tree: &tree::FileTree,
630 diff_config: &DiffConfig,
631 sorted_paths: &[PathBuf],
632 ts_config: &markdown::TreeSitterConfig,
633) -> io::Result<String> {
634 let mut output = String::new();
635
636 output.push_str("# Directory Structure Report\n\n");
638
639 output.push_str(&format!(
641 "**Project:** {}\n",
642 current_state.metadata.project_name
643 ));
644 output.push_str(&format!("**Generated:** {}\n", current_state.timestamp));
645
646 if !args.filter.is_empty() {
647 output.push_str(&format!("**Filters:** {}\n", args.filter.join(", ")));
648 }
649
650 if !args.ignore.is_empty() {
651 output.push_str(&format!("**Ignored:** {}\n", args.ignore.join(", ")));
652 }
653
654 output.push('\n');
655
656 if let Some(comp) = comparison {
658 if comp.summary.has_changes() {
659 output.push_str(&comp.summary.to_markdown());
660
661 let added_files: Vec<_> = comp
663 .file_diffs
664 .iter()
665 .filter(|d| matches!(d.status, diff::PerFileStatus::Added))
666 .collect();
667
668 if diff_config.diff_only && !added_files.is_empty() {
669 output.push_str("## Added Files\n\n");
670 for added in added_files {
671 output.push_str(&format!("### File: `{}`\n\n", added.path));
672 output.push_str("_Status: Added_\n\n");
673 let mut lines: Vec<String> = Vec::new();
675 for line in added.diff.lines() {
676 if let Some(rest) = line.strip_prefix("+ ") {
679 lines.push(rest.to_string());
680 } else if let Some(rest) = line.strip_prefix('+') {
681 lines.push(rest.to_string());
683 }
684 }
685 output.push_str("```text\n");
686 if args.line_numbers {
687 for (idx, l) in lines.iter().enumerate() {
688 output.push_str(&format!("{:>4} | {}\n", idx + 1, l));
689 }
690 } else {
691 for l in lines {
692 output.push_str(&l);
693 output.push('\n');
694 }
695 }
696 output.push_str("```\n\n");
697 }
698 }
699
700 let changed_diffs: Vec<diff::PerFileDiff> = comp
702 .file_diffs
703 .iter()
704 .filter(|d| d.is_changed())
705 .cloned()
706 .collect();
707 if !changed_diffs.is_empty() {
708 output.push_str("## File Differences\n\n");
709 let diff_markdown = render_per_file_diffs(&changed_diffs);
710 output.push_str(&diff_markdown);
711 }
712 } else {
713 output.push_str("## No Changes Detected\n\n");
714 }
715 }
716
717 output.push_str("## File Tree Structure\n\n");
719 let mut tree_output = Vec::new();
720 tree::write_tree_to_file(&mut tree_output, file_tree, 0)?;
721 output.push_str(&String::from_utf8_lossy(&tree_output));
722 output.push('\n');
723
724 if !diff_config.diff_only {
726 output.push_str("## File Contents\n\n");
727
728 for path in sorted_paths {
731 if let Some(file_state) = current_state.files.get(path) {
732 output.push_str(&format!("### File: `{}`\n\n", path.display()));
733 output.push_str(&format!("- Size: {} bytes\n", file_state.size));
734 output.push_str(&format!("- Modified: {:?}\n\n", file_state.modified));
735
736 let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("text");
738 let language = match extension {
739 "rs" => "rust",
740 "js" => "javascript",
741 "ts" => "typescript",
742 "py" => "python",
743 "json" => "json",
744 "toml" => "toml",
745 "md" => "markdown",
746 "yaml" | "yml" => "yaml",
747 "html" => "html",
748 "css" => "css",
749 _ => extension,
750 };
751
752 let signatures_only = ts_config.signatures
754 && crate::tree_sitter::is_supported_extension(extension);
755
756 if !signatures_only {
757 output.push_str(&format!("```{}\n", language));
758
759 if args.line_numbers {
760 for (i, line) in file_state.content.lines().enumerate() {
761 output.push_str(&format!("{:>4} | {}\n", i + 1, line));
762 }
763 } else {
764 output.push_str(&file_state.content);
765 if !file_state.content.ends_with('\n') {
766 output.push('\n');
767 }
768 }
769
770 output.push_str("```\n");
771 }
772
773 let mut enrichment_buf = Vec::new();
775 markdown::write_tree_sitter_enrichment(
776 &mut enrichment_buf,
777 &file_state.content,
778 extension,
779 ts_config,
780 )?;
781 if !enrichment_buf.is_empty() {
782 output.push_str(&String::from_utf8_lossy(&enrichment_buf));
783 }
784
785 output.push_str("\n");
786 }
787 }
788 }
789
790 Ok(output)
791}
792
793pub fn run() -> io::Result<()> {
794 env_logger::init();
795 let args = Args::parse();
796
797 if args.init {
799 return init_config();
800 }
801
802 let project_root = Path::new(&args.input);
804 let config = load_config_from_path(project_root);
805
806 if args.clear_cache {
808 let cache_path = project_root.join(".context-builder").join("cache");
809 if cache_path.exists() {
810 match fs::remove_dir_all(&cache_path) {
811 Ok(()) => println!("Cache cleared: {}", cache_path.display()),
812 Err(e) => eprintln!("Failed to clear cache ({}): {}", cache_path.display(), e),
813 }
814 } else {
815 println!("No cache directory found at {}", cache_path.display());
816 }
817 return Ok(());
818 }
819
820 if std::env::args().len() == 1 && config.is_none() {
821 Args::command().print_help()?;
822 return Ok(());
823 }
824
825 let resolution = crate::config_resolver::resolve_final_config(args, config.clone());
827
828 let silent = std::env::var("CB_SILENT")
830 .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
831 .unwrap_or(false);
832
833 if !silent {
834 for warning in &resolution.warnings {
835 eprintln!("Warning: {}", warning);
836 }
837 }
838
839 let final_args = Args {
841 input: resolution.config.input,
842 output: resolution.config.output,
843 filter: resolution.config.filter,
844 ignore: resolution.config.ignore,
845 line_numbers: resolution.config.line_numbers,
846 preview: resolution.config.preview,
847 token_count: resolution.config.token_count,
848 yes: resolution.config.yes,
849 diff_only: resolution.config.diff_only,
850 clear_cache: resolution.config.clear_cache,
851 max_tokens: resolution.config.max_tokens,
852 init: false,
853 signatures: resolution.config.signatures,
854 structure: resolution.config.structure,
855 truncate: resolution.config.truncate,
856 visibility: resolution.config.visibility,
857 };
858
859 let final_config = Config {
861 auto_diff: Some(resolution.config.auto_diff),
862 diff_context_lines: Some(resolution.config.diff_context_lines),
863 ..config.unwrap_or_default()
864 };
865
866 run_with_args(final_args, final_config, &DefaultPrompter)
867}
868
869fn detect_major_file_types() -> io::Result<Vec<String>> {
871 use std::collections::HashMap;
872 let mut extension_counts = HashMap::new();
873
874 let default_ignores = vec![
876 "docs".to_string(),
877 "target".to_string(),
878 ".git".to_string(),
879 "node_modules".to_string(),
880 ];
881
882 let files = crate::file_utils::collect_files(Path::new("."), &[], &default_ignores, &[])?;
884
885 for entry in files {
887 let path = entry.path();
888 if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
889 *extension_counts.entry(extension.to_string()).or_insert(0) += 1;
891 }
892 }
893
894 let mut extensions: Vec<(String, usize)> = extension_counts.into_iter().collect();
896 extensions.sort_by(|a, b| b.1.cmp(&a.1));
897
898 let top_extensions: Vec<String> = extensions.into_iter().take(5).map(|(ext, _)| ext).collect();
900
901 Ok(top_extensions)
902}
903
904fn init_config() -> io::Result<()> {
906 let config_path = Path::new("context-builder.toml");
907
908 if config_path.exists() {
909 println!("Config file already exists at {}", config_path.display());
910 println!("If you want to replace it, please remove it manually first.");
911 return Ok(());
912 }
913
914 let filter_suggestions = match detect_major_file_types() {
916 Ok(extensions) => extensions,
917 _ => vec!["rs".to_string(), "toml".to_string()], };
919
920 let filter_string = if filter_suggestions.is_empty() {
921 r#"["rs", "toml"]"#.to_string()
922 } else {
923 format!(r#"["{}"]"#, filter_suggestions.join(r#"", ""#))
924 };
925
926 let default_config_content = format!(
927 r#"# Context Builder Configuration File
928# This file was generated with sensible defaults based on the file types detected in your project
929
930# Output file name (or base name when timestamped_output is true)
931output = "context.md"
932
933# Optional folder to place the generated output file(s) in
934output_folder = "docs"
935
936# Append a UTC timestamp to the output file name (before extension)
937timestamped_output = true
938
939# Enable automatic diff generation (requires timestamped_output = true)
940auto_diff = true
941
942# Emit only change summary + modified file diffs (no full file bodies)
943diff_only = false
944
945# File extensions to include (no leading dot, e.g. "rs", "toml")
946filter = {}
947
948# File / directory names to ignore (exact name matches)
949ignore = ["docs", "target", ".git", "node_modules"]
950
951# Add line numbers to code blocks
952line_numbers = false
953"#,
954 filter_string
955 );
956
957 let mut file = File::create(config_path)?;
958 file.write_all(default_config_content.as_bytes())?;
959
960 println!("Config file created at {}", config_path.display());
961 println!("Detected file types: {}", filter_suggestions.join(", "));
962 println!("You can now customize it according to your project needs.");
963
964 Ok(())
965}
966
967#[cfg(test)]
968mod tests {
969 use super::*;
970 use std::io::Result;
971 use tempfile::tempdir;
972
973 struct MockPrompter {
975 confirm_processing_response: bool,
976 confirm_overwrite_response: bool,
977 }
978
979 impl MockPrompter {
980 fn new(processing: bool, overwrite: bool) -> Self {
981 Self {
982 confirm_processing_response: processing,
983 confirm_overwrite_response: overwrite,
984 }
985 }
986 }
987
988 impl Prompter for MockPrompter {
989 fn confirm_processing(&self, _file_count: usize) -> Result<bool> {
990 Ok(self.confirm_processing_response)
991 }
992
993 fn confirm_overwrite(&self, _file_path: &str) -> Result<bool> {
994 Ok(self.confirm_overwrite_response)
995 }
996 }
997
998 #[test]
999 fn test_diff_config_default() {
1000 let config = DiffConfig::default();
1001 assert_eq!(config.context_lines, 3);
1002 assert!(!config.enabled);
1003 assert!(!config.diff_only);
1004 }
1005
1006 #[test]
1007 fn test_diff_config_custom() {
1008 let config = DiffConfig {
1009 context_lines: 5,
1010 enabled: true,
1011 diff_only: true,
1012 };
1013 assert_eq!(config.context_lines, 5);
1014 assert!(config.enabled);
1015 assert!(config.diff_only);
1016 }
1017
1018 #[test]
1019 fn test_default_prompter() {
1020 let prompter = DefaultPrompter;
1021
1022 let result = prompter.confirm_processing(50);
1024 assert!(result.is_ok());
1025 assert!(result.unwrap());
1026 }
1027
1028 #[test]
1029 fn test_run_with_args_nonexistent_directory() {
1030 let args = Args {
1031 input: "/nonexistent/directory".to_string(),
1032 output: "output.md".to_string(),
1033 filter: vec![],
1034 ignore: vec![],
1035 line_numbers: false,
1036 preview: false,
1037 token_count: false,
1038 yes: false,
1039 diff_only: false,
1040 clear_cache: false,
1041 init: false,
1042 max_tokens: None,
1043 signatures: false,
1044 structure: false,
1045 truncate: "smart".to_string(),
1046 visibility: "all".to_string(),
1047 };
1048 let config = Config::default();
1049 let prompter = MockPrompter::new(true, true);
1050
1051 let result = run_with_args(args, config, &prompter);
1052 assert!(result.is_err());
1053 assert!(result.unwrap_err().to_string().contains("does not exist"));
1054 }
1055
1056 #[test]
1057 fn test_run_with_args_preview_mode() {
1058 let temp_dir = tempdir().unwrap();
1059 let base_path = temp_dir.path();
1060
1061 fs::write(base_path.join("test.rs"), "fn main() {}").unwrap();
1063 fs::create_dir(base_path.join("src")).unwrap();
1064 fs::write(base_path.join("src/lib.rs"), "pub fn hello() {}").unwrap();
1065
1066 let args = Args {
1067 input: ".".to_string(),
1068 output: "test.md".to_string(),
1069 filter: vec![],
1070 ignore: vec![],
1071 line_numbers: false,
1072 preview: false,
1073 token_count: false,
1074 yes: false,
1075 diff_only: false,
1076 clear_cache: false,
1077 init: false,
1078 max_tokens: None,
1079 signatures: false,
1080 structure: false,
1081 truncate: "smart".to_string(),
1082 visibility: "all".to_string(),
1083 };
1084 let config = Config::default();
1085 let prompter = MockPrompter::new(true, true);
1086
1087 unsafe {
1089 std::env::set_var("CB_SILENT", "1");
1090 }
1091 let result = run_with_args(args, config, &prompter);
1092 unsafe {
1093 std::env::remove_var("CB_SILENT");
1094 }
1095
1096 assert!(result.is_ok());
1097 }
1098
1099 #[test]
1100 fn test_run_with_args_token_count_mode() {
1101 let temp_dir = tempdir().unwrap();
1102 let base_path = temp_dir.path();
1103
1104 fs::write(base_path.join("small.txt"), "Hello world").unwrap();
1106
1107 let args = Args {
1108 input: base_path.to_string_lossy().to_string(),
1109 output: "test.md".to_string(),
1110 filter: vec![],
1111 ignore: vec![],
1112 line_numbers: false,
1113 preview: false,
1114 token_count: true,
1115 yes: false,
1116 diff_only: false,
1117 clear_cache: false,
1118 init: false,
1119 max_tokens: None,
1120 signatures: false,
1121 structure: false,
1122 truncate: "smart".to_string(),
1123 visibility: "all".to_string(),
1124 };
1125 let config = Config::default();
1126 let prompter = MockPrompter::new(true, true);
1127
1128 unsafe {
1129 std::env::set_var("CB_SILENT", "1");
1130 }
1131 let result = run_with_args(args, config, &prompter);
1132 unsafe {
1133 std::env::remove_var("CB_SILENT");
1134 }
1135
1136 assert!(result.is_ok());
1137 }
1138
1139 #[test]
1140 fn test_run_with_args_preview_and_token_count() {
1141 let temp_dir = tempdir().unwrap();
1142 let base_path = temp_dir.path();
1143
1144 fs::write(base_path.join("test.txt"), "content").unwrap();
1145
1146 let args = Args {
1147 input: base_path.to_string_lossy().to_string(),
1148 output: "test.md".to_string(),
1149 filter: vec![],
1150 ignore: vec![],
1151 line_numbers: false,
1152 preview: true,
1153 token_count: false,
1154 yes: false,
1155 diff_only: false,
1156 clear_cache: false,
1157 init: false,
1158 max_tokens: None,
1159 signatures: false,
1160 structure: false,
1161 truncate: "smart".to_string(),
1162 visibility: "all".to_string(),
1163 };
1164 let config = Config::default();
1165 let prompter = MockPrompter::new(true, true);
1166
1167 unsafe {
1168 std::env::set_var("CB_SILENT", "1");
1169 }
1170 let result = run_with_args(args, config, &prompter);
1171 unsafe {
1172 std::env::remove_var("CB_SILENT");
1173 }
1174
1175 assert!(result.is_ok());
1176 }
1177
1178 #[test]
1179 fn test_run_with_args_user_cancels_overwrite() {
1180 let temp_dir = tempdir().unwrap();
1181 let base_path = temp_dir.path();
1182 let output_path = temp_dir.path().join("existing.md");
1183
1184 fs::write(base_path.join("test.txt"), "content").unwrap();
1186 fs::write(&output_path, "existing content").unwrap();
1187
1188 let args = Args {
1189 input: base_path.to_string_lossy().to_string(),
1190 output: "test.md".to_string(),
1191 filter: vec![],
1192 ignore: vec!["target".to_string()],
1193 line_numbers: false,
1194 preview: false,
1195 token_count: false,
1196 yes: false,
1197 diff_only: false,
1198 clear_cache: false,
1199 init: false,
1200 max_tokens: None,
1201 signatures: false,
1202 structure: false,
1203 truncate: "smart".to_string(),
1204 visibility: "all".to_string(),
1205 };
1206 let config = Config::default();
1207 let prompter = MockPrompter::new(true, false); unsafe {
1210 std::env::set_var("CB_SILENT", "1");
1211 }
1212 let result = run_with_args(args, config, &prompter);
1213 unsafe {
1214 std::env::remove_var("CB_SILENT");
1215 }
1216
1217 assert!(result.is_err());
1218 assert!(result.unwrap_err().to_string().contains("cancelled"));
1219 }
1220
1221 #[test]
1222 fn test_run_with_args_user_cancels_processing() {
1223 let temp_dir = tempdir().unwrap();
1224 let base_path = temp_dir.path();
1225
1226 for i in 0..105 {
1228 fs::write(base_path.join(format!("file{}.txt", i)), "content").unwrap();
1229 }
1230
1231 let args = Args {
1232 input: base_path.to_string_lossy().to_string(),
1233 output: "test.md".to_string(),
1234 filter: vec!["rs".to_string()],
1235 ignore: vec![],
1236 line_numbers: false,
1237 preview: false,
1238 token_count: false,
1239 yes: false,
1240 diff_only: false,
1241 clear_cache: false,
1242 init: false,
1243 max_tokens: None,
1244 signatures: false,
1245 structure: false,
1246 truncate: "smart".to_string(),
1247 visibility: "all".to_string(),
1248 };
1249 let config = Config::default();
1250 let prompter = MockPrompter::new(false, true); unsafe {
1253 std::env::set_var("CB_SILENT", "1");
1254 }
1255 let result = run_with_args(args, config, &prompter);
1256 unsafe {
1257 std::env::remove_var("CB_SILENT");
1258 }
1259
1260 assert!(result.is_err());
1261 assert!(result.unwrap_err().to_string().contains("cancelled"));
1262 }
1263
1264 #[test]
1265 fn test_run_with_args_with_yes_flag() {
1266 let temp_dir = tempdir().unwrap();
1267 let base_path = temp_dir.path();
1268 let output_file_name = "test.md";
1269 let output_path = temp_dir.path().join(output_file_name);
1270
1271 fs::write(base_path.join("test.txt"), "Hello world").unwrap();
1272
1273 let args = Args {
1274 input: base_path.to_string_lossy().to_string(),
1275 output: output_path.to_string_lossy().to_string(),
1276 filter: vec![],
1277 ignore: vec!["ignored_dir".to_string()],
1278 line_numbers: false,
1279 preview: false,
1280 token_count: false,
1281 yes: true,
1282 diff_only: false,
1283 clear_cache: false,
1284 init: false,
1285 max_tokens: None,
1286 signatures: false,
1287 structure: false,
1288 truncate: "smart".to_string(),
1289 visibility: "all".to_string(),
1290 };
1291 let config = Config::default();
1292 let prompter = MockPrompter::new(true, true);
1293
1294 unsafe {
1295 std::env::set_var("CB_SILENT", "1");
1296 }
1297 let result = run_with_args(args, config, &prompter);
1298 unsafe {
1299 std::env::remove_var("CB_SILENT");
1300 }
1301
1302 assert!(result.is_ok());
1303 assert!(output_path.exists());
1304
1305 let content = fs::read_to_string(&output_path).unwrap();
1306 assert!(content.contains("Directory Structure Report"));
1307 assert!(content.contains("test.txt"));
1308 }
1309
1310 #[test]
1311 fn test_run_with_args_with_filters() {
1312 let temp_dir = tempdir().unwrap();
1313 let base_path = temp_dir.path();
1314 let output_file_name = "test.md";
1315 let output_path = temp_dir.path().join(output_file_name);
1316
1317 fs::write(base_path.join("code.rs"), "fn main() {}").unwrap();
1318 fs::write(base_path.join("readme.md"), "# README").unwrap();
1319 fs::write(base_path.join("data.json"), r#"{"key": "value"}"#).unwrap();
1320
1321 let args = Args {
1322 input: base_path.to_string_lossy().to_string(),
1323 output: output_path.to_string_lossy().to_string(),
1324 filter: vec!["rs".to_string(), "md".to_string()],
1325 ignore: vec![],
1326 line_numbers: true,
1327 preview: false,
1328 token_count: false,
1329 yes: true,
1330 diff_only: false,
1331 clear_cache: false,
1332 init: false,
1333 max_tokens: None,
1334 signatures: false,
1335 structure: false,
1336 truncate: "smart".to_string(),
1337 visibility: "all".to_string(),
1338 };
1339 let config = Config::default();
1340 let prompter = MockPrompter::new(true, true);
1341
1342 unsafe {
1343 std::env::set_var("CB_SILENT", "1");
1344 }
1345 let result = run_with_args(args, config, &prompter);
1346 unsafe {
1347 std::env::remove_var("CB_SILENT");
1348 }
1349
1350 assert!(result.is_ok());
1351
1352 let content = fs::read_to_string(&output_path).unwrap();
1353 assert!(content.contains("code.rs"));
1354 assert!(content.contains("readme.md"));
1355 assert!(!content.contains("data.json")); assert!(content.contains(" 1 |")); }
1358
1359 #[test]
1360 fn test_run_with_args_with_ignores() {
1361 let temp_dir = tempdir().unwrap();
1362 let base_path = temp_dir.path();
1363 let output_path = temp_dir.path().join("ignored.md");
1364
1365 fs::write(base_path.join("important.txt"), "important content").unwrap();
1366 fs::write(base_path.join("secret.txt"), "secret content").unwrap();
1367
1368 let args = Args {
1369 input: base_path.to_string_lossy().to_string(),
1370 output: output_path.to_string_lossy().to_string(),
1371 filter: vec![],
1372 ignore: vec!["secret.txt".to_string()],
1373 line_numbers: false,
1374 preview: false,
1375 token_count: false,
1376 yes: true,
1377 diff_only: false,
1378 clear_cache: false,
1379 init: false,
1380 max_tokens: None,
1381 signatures: false,
1382 structure: false,
1383 truncate: "smart".to_string(),
1384 visibility: "all".to_string(),
1385 };
1386 let config = Config::default();
1387 let prompter = MockPrompter::new(true, true);
1388
1389 unsafe {
1390 std::env::set_var("CB_SILENT", "1");
1391 }
1392 let result = run_with_args(args, config, &prompter);
1393 unsafe {
1394 std::env::remove_var("CB_SILENT");
1395 }
1396
1397 assert!(result.is_ok());
1398
1399 let content = fs::read_to_string(&output_path).unwrap();
1400 assert!(content.contains("important.txt"));
1401 }
1404
1405 #[test]
1406 fn test_auto_diff_without_previous_state() {
1407 let temp_dir = tempdir().unwrap();
1408 let base_path = temp_dir.path();
1409 let output_file_name = "test.md";
1410 let output_path = temp_dir.path().join(output_file_name);
1411
1412 fs::write(base_path.join("new.txt"), "new content").unwrap();
1413
1414 let args = Args {
1415 input: base_path.to_string_lossy().to_string(),
1416 output: output_path.to_string_lossy().to_string(),
1417 filter: vec![],
1418 ignore: vec![],
1419 line_numbers: false,
1420 preview: false,
1421 token_count: false,
1422 yes: true,
1423 diff_only: false,
1424 clear_cache: false,
1425 init: false,
1426 max_tokens: None,
1427 signatures: false,
1428 structure: false,
1429 truncate: "smart".to_string(),
1430 visibility: "all".to_string(),
1431 };
1432 let config = Config {
1433 auto_diff: Some(true),
1434 diff_context_lines: Some(5),
1435 ..Default::default()
1436 };
1437 let prompter = MockPrompter::new(true, true);
1438
1439 unsafe {
1440 std::env::set_var("CB_SILENT", "1");
1441 }
1442 let result = run_with_args(args, config, &prompter);
1443 unsafe {
1444 std::env::remove_var("CB_SILENT");
1445 }
1446
1447 assert!(result.is_ok());
1448 assert!(output_path.exists());
1449
1450 let content = fs::read_to_string(&output_path).unwrap();
1451 assert!(content.contains("new.txt"));
1452 }
1453
1454 #[test]
1455 fn test_run_creates_output_directory() {
1456 let temp_dir = tempdir().unwrap();
1457 let base_path = temp_dir.path();
1458 let output_dir = temp_dir.path().join("nested").join("output");
1459 let output_path = output_dir.join("result.md");
1460
1461 fs::write(base_path.join("test.txt"), "content").unwrap();
1462
1463 let args = Args {
1464 input: base_path.to_string_lossy().to_string(),
1465 output: output_path.to_string_lossy().to_string(),
1466 filter: vec![],
1467 ignore: vec![],
1468 line_numbers: false,
1469 preview: false,
1470 token_count: false,
1471 yes: true,
1472 diff_only: false,
1473 clear_cache: false,
1474 init: false,
1475 max_tokens: None,
1476 signatures: false,
1477 structure: false,
1478 truncate: "smart".to_string(),
1479 visibility: "all".to_string(),
1480 };
1481 let config = Config::default();
1482 let prompter = MockPrompter::new(true, true);
1483
1484 unsafe {
1485 std::env::set_var("CB_SILENT", "1");
1486 }
1487 let result = run_with_args(args, config, &prompter);
1488 unsafe {
1489 std::env::remove_var("CB_SILENT");
1490 }
1491
1492 assert!(result.is_ok());
1493 assert!(output_path.exists());
1494 assert!(output_dir.exists());
1495 }
1496
1497 #[test]
1498 fn test_generate_markdown_with_diff_no_comparison() {
1499 let temp_dir = tempdir().unwrap();
1500 let base_path = temp_dir.path();
1501
1502 fs::write(base_path.join("test.rs"), "fn main() {}").unwrap();
1503
1504 let files = collect_files(base_path, &[], &[], &[]).unwrap();
1505 let file_tree = build_file_tree(&files, base_path);
1506 let config = Config::default();
1507 let state = ProjectState::from_files(&files, base_path, &config, false).unwrap();
1508
1509 let args = Args {
1510 input: base_path.to_string_lossy().to_string(),
1511 output: "test.md".to_string(),
1512 filter: vec![],
1513 ignore: vec![],
1514 line_numbers: false,
1515 preview: false,
1516 token_count: false,
1517 yes: false,
1518 diff_only: false,
1519 clear_cache: false,
1520 init: false,
1521 max_tokens: None,
1522 signatures: false,
1523 structure: false,
1524 truncate: "smart".to_string(),
1525 visibility: "all".to_string(),
1526 };
1527
1528 let diff_config = DiffConfig::default();
1529
1530 let sorted_paths: Vec<PathBuf> = files
1531 .iter()
1532 .map(|e| {
1533 e.path()
1534 .strip_prefix(base_path)
1535 .unwrap_or(e.path())
1536 .to_path_buf()
1537 })
1538 .collect();
1539
1540 let ts_config = markdown::TreeSitterConfig {
1541 signatures: false,
1542 structure: false,
1543 truncate: "smart".to_string(),
1544 visibility: "all".to_string(),
1545 };
1546
1547 let result = generate_markdown_with_diff(
1548 &state,
1549 None,
1550 &args,
1551 &file_tree,
1552 &diff_config,
1553 &sorted_paths,
1554 &ts_config,
1555 );
1556 assert!(result.is_ok());
1557
1558 let content = result.unwrap();
1559 assert!(content.contains("Directory Structure Report"));
1560 assert!(content.contains("test.rs"));
1561 }
1562}