ruby-tools 0.0.0

Ruby language tools for Rusty Ruby
Documentation
//! RDoc 文档生成器
//! 从 Ruby 代码中提取注释并生成 HTML 文档

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);
                }
            }
        }
    }
}