summaryrefslogtreecommitdiff
path: root/gen/gen_completions_entries.rs
blob: 0e030e6b26d065a0f90d53a7e2c2f6bcb372fb26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use just_template::{Template, tmpl};
use std::path::PathBuf;

use crate::r#gen::constants::{COMPLETIONS, COMPLETIONS_PATH, COMPLETIONS_TEMPLATE};

/// Generate completions file from comp directory using just_template
pub async fn generate_completions_file(repo_root: &PathBuf) {
    let template_path = repo_root.join(COMPLETIONS_TEMPLATE);
    let output_path = repo_root.join(COMPLETIONS);
    let comps_dir = repo_root.join(COMPLETIONS_PATH);

    // Read the template
    let template_content = tokio::fs::read_to_string(&template_path).await.unwrap();

    // Collect all completion files
    let mut all_completions: Vec<(String, String)> = Vec::new();
    let mut all_nodes: Vec<String> = Vec::new();

    if comps_dir.exists() && comps_dir.is_dir() {
        let mut entries = tokio::fs::read_dir(&comps_dir).await.unwrap();
        while let Some(entry) = entries.next_entry().await.unwrap() {
            let path = entry.path();

            if !path.is_file() {
                continue;
            }

            let extension = match path.extension() {
                Some(ext) => ext,
                None => continue,
            };

            if extension != "rs" {
                continue;
            }

            let file_name = match path.file_stem().and_then(|s| s.to_str()) {
                Some(name) => name,
                None => continue,
            };

            let node_name = just_fmt::lower_case!(file_name);

            all_completions.push((file_name.to_string(), node_name.clone()));
            all_nodes.push(node_name);
        }
    }

    // Create template
    let mut template = Template::from(template_content);

    // Generate match arms for each completion
    for (comp_name, node_name) in &all_completions {
        tmpl!(template += {
            comp_match_arms {
                (comp_name = comp_name, comp_node_name = node_name)
            }
        });
    }

    for node in all_nodes {
        tmpl!(template += {
            comp_node_name {
                (comp_node_name = just_fmt::lower_case!(node))
            }
        });
    }

    // Expand the template
    let final_content = template.expand().unwrap();

    // Write the generated code
    tokio::fs::write(output_path, final_content).await.unwrap();

    println!(
        "Generated completions file with {} completions using just_template",
        all_completions.len()
    );
}