|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::io; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +fn main() -> io::Result<()> { |
| 7 | + // Get the folder name (crate name) from the argument |
| 8 | + let args: Vec<String> = env::args().collect(); |
| 9 | + if args.len() != 3 { |
| 10 | + eprintln!("Usage: doc_gen <crate_folder> <doc_folder>"); |
| 11 | + std::process::exit(1); |
| 12 | + } |
| 13 | + |
| 14 | + let crate_folder = &args[1]; |
| 15 | + let doc_folder = &args[2]; |
| 16 | + let crate_path = Path::new(crate_folder); |
| 17 | + let doc_path = Path::new(doc_folder); |
| 18 | + |
| 19 | + // Ensure the crate folder exists |
| 20 | + if !crate_path.is_dir() { |
| 21 | + eprintln!("Error: {} is not a valid directory.", crate_folder); |
| 22 | + std::process::exit(1); |
| 23 | + } |
| 24 | + // Ensure the doc folder exists |
| 25 | + if !doc_path.is_dir() { |
| 26 | + eprintln!("Error: {} is not a valid directory.", doc_folder); |
| 27 | + std::process::exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + // Define the path to the lib.rs file inside the crate |
| 31 | + let lib_rs_path = crate_path.join("src").join("lib.rs"); |
| 32 | + if !lib_rs_path.exists() { |
| 33 | + eprintln!("Error: {} does not exist.", lib_rs_path.display()); |
| 34 | + std::process::exit(1); |
| 35 | + } |
| 36 | + //crate name we are generating doc for |
| 37 | + let crate_name = crate_path |
| 38 | + .file_name() |
| 39 | + .unwrap() |
| 40 | + .to_string_lossy() |
| 41 | + .to_string(); |
| 42 | + // Output folder under doc where folder would be created if necessary |
| 43 | + let output_path = doc_path.join(&crate_name); |
| 44 | + if !output_path.exists() { |
| 45 | + fs::create_dir_all(&output_path)?; |
| 46 | + } |
| 47 | + |
| 48 | + // Read the lib.rs file to generate the content for introduction.md |
| 49 | + let lib_rs_content = fs::read_to_string(lib_rs_path)?; |
| 50 | + let mut lines = lib_rs_content.lines(); |
| 51 | + |
| 52 | + // Extract the title and description |
| 53 | + let title = lines |
| 54 | + .next() |
| 55 | + .unwrap_or("") |
| 56 | + .replace("#", "") |
| 57 | + .trim() |
| 58 | + .to_string(); |
| 59 | + let description: Vec<String> = lines |
| 60 | + .clone() |
| 61 | + .take_while(|line| line.starts_with("##")) |
| 62 | + .map(|line| line.trim_start_matches("##").trim().to_string()) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + // Create the introduction.md content |
| 66 | + let mut introduction_md_content = format!("# {}\n\n", title); |
| 67 | + for desc in description { |
| 68 | + //introduction_md_content.push_str(&format!("{}\n\n", desc)); |
| 69 | + introduction_md_content.push_str(&format!("{}", desc)); |
| 70 | + } |
| 71 | + |
| 72 | + introduction_md_content.push_str("```rust,ignore\n"); |
| 73 | + for line in lines { |
| 74 | + introduction_md_content.push_str(&format!("{}\n", line)); |
| 75 | + } |
| 76 | + introduction_md_content.push_str("```\n\n"); |
| 77 | + introduction_md_content.push_str(&format!( |
| 78 | + "[Source](https://github.com/ratulb/programming_problems_in_rust/blob/master/{}/src/lib.rs)\n", |
| 79 | + crate_name |
| 80 | + )); |
| 81 | + |
| 82 | + // Write the introduction.md file (overwrite if it already exists) |
| 83 | + let introduction_md_path = output_path.join("introduction.md"); |
| 84 | + fs::write(introduction_md_path, introduction_md_content)?; |
| 85 | + // Update the SUMMARY.md file in the project-level src directory |
| 86 | + let summary_md_path = doc_path.join("SUMMARY.md"); |
| 87 | + let mut summary_md_content = fs::read_to_string(&summary_md_path)?; |
| 88 | + |
| 89 | + // Add a new line with the link to the introduction.md if it's not already present |
| 90 | + let summary_entry = format!("* [{}](./{}/introduction.md)", title, crate_name); |
| 91 | + if !summary_md_content.contains(&summary_entry) { |
| 92 | + summary_md_content.push_str(&summary_entry); |
| 93 | + fs::write(summary_md_path, summary_md_content)?; |
| 94 | + } |
| 95 | + |
| 96 | + println!("Successfully generated introduction.md and updated SUMMARY.md."); |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments