use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
fn main() {
let args: Vec<String> = env::args().collect();
let paths: Vec<&str> = args[1..].iter().map(|s| s.as_str()).collect();
println!("rdoc - Ruby documentation generator");
if paths.is_empty() {
println!("Generating documentation for current directory...");
generate_docs(&["."]);
} else {
println!("Generating documentation for specified paths...");
generate_docs(&paths);
}
}
fn generate_docs(paths: &[&str]) {
let output_dir = Path::new("doc");
if !output_dir.exists() {
match fs::create_dir_all(output_dir) {
Ok(_) => println!("Created output directory: doc/"),
Err(e) => {
println!("Error creating output directory: {:?}", e);
return;
}
}
}
for path in paths {
let path_obj = Path::new(path);
if path_obj.is_file() {
process_file(path_obj, output_dir);
} else if path_obj.is_dir() {
process_directory(path_obj, output_dir);
} else {
println!("Warning: {} is not a file or directory", path);
}
}
println!("Documentation generated in doc/ directory");
}
fn process_file(file_path: &Path, output_dir: &Path) {
if file_path.extension().map_or(false, |ext| ext == "rb") {
println!("Processing file: {}", file_path.display());
}
}
fn process_directory(dir_path: &Path, output_dir: &Path) {
println!("Processing directory: {}", dir_path.display());
if let Ok(entries) = fs::read_dir(dir_path) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() {
process_file(&path, output_dir);
} else if path.is_dir() {
let sub_output_dir = output_dir.join(path.file_name().unwrap());
if let Err(e) = fs::create_dir_all(&sub_output_dir) {
println!("Error creating subdirectory: {:?}", e);
continue;
}
process_directory(&path, &sub_output_dir);
}
}
}
}
}