|
| 1 | +use serde::Deserialize; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::fs::File; |
| 4 | +use std::io::Write; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +#[derive(Debug, Deserialize)] |
| 8 | +struct Language { |
| 9 | + #[serde(default)] |
| 10 | + r#type: Option<String>, |
| 11 | + #[serde(default)] |
| 12 | + extensions: Vec<String>, |
| 13 | + #[serde(default)] |
| 14 | + filenames: Vec<String>, |
| 15 | + #[serde(default)] |
| 16 | + interpreters: Vec<String>, |
| 17 | + #[serde(default)] |
| 18 | + language_id: Option<i32>, |
| 19 | +} |
| 20 | + |
| 21 | +fn main() { |
| 22 | + println!("cargo:rerun-if-changed=languages.yml"); |
| 23 | + |
| 24 | + // Read and parse languages.yml |
| 25 | + let yaml_content = |
| 26 | + std::fs::read_to_string("languages.yml").expect("Failed to read languages.yml"); |
| 27 | + let languages: HashMap<String, Language> = |
| 28 | + serde_yaml::from_str(&yaml_content).expect("Failed to parse languages.yml"); |
| 29 | + |
| 30 | + // Generate the rust code |
| 31 | + let mut code = String::new(); |
| 32 | + |
| 33 | + // Add the use statements |
| 34 | + code.push_str("use once_cell::sync::Lazy;\n"); |
| 35 | + code.push_str("use std::collections::HashSet;\n\n"); |
| 36 | + |
| 37 | + // Generate source extensions set |
| 38 | + code.push_str("pub static SOURCE_EXTENSIONS: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n"); |
| 39 | + code.push_str(" let mut set = HashSet::new();\n\n"); |
| 40 | + |
| 41 | + for (_, lang) in &languages { |
| 42 | + for ext in &lang.extensions { |
| 43 | + let ext = ext.trim_start_matches('.'); |
| 44 | + code.push_str(&format!(" set.insert(\"{}\");\n", ext)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + code.push_str(" set\n"); |
| 49 | + code.push_str("});\n\n"); |
| 50 | + |
| 51 | + // Generate filename mappings |
| 52 | + code.push_str("pub static KNOWN_FILENAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n"); |
| 53 | + code.push_str(" let mut set = HashSet::new();\n\n"); |
| 54 | + |
| 55 | + for (_, lang) in &languages { |
| 56 | + for filename in &lang.filenames { |
| 57 | + code.push_str(&format!(" set.insert(\"{}\");\n", filename)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + code.push_str(" set\n"); |
| 62 | + code.push_str("});\n\n"); |
| 63 | + |
| 64 | + // Generate interpreter mappings |
| 65 | + code.push_str("pub static INTERPRETER_NAMES: Lazy<HashSet<&'static str>> = Lazy::new(|| {\n"); |
| 66 | + code.push_str(" let mut set = HashSet::new();\n\n"); |
| 67 | + |
| 68 | + for (_, lang) in &languages { |
| 69 | + for interpreter in &lang.interpreters { |
| 70 | + code.push_str(&format!(" set.insert(\"{}\");\n", interpreter)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + code.push_str(" set\n"); |
| 75 | + code.push_str("});\n"); |
| 76 | + |
| 77 | + // Write the generated code to file |
| 78 | + let out_dir = std::env::var_os("OUT_DIR").unwrap(); |
| 79 | + let dest_path = Path::new(&out_dir).join("languages.rs"); |
| 80 | + let mut f = File::create(dest_path).unwrap(); |
| 81 | + f.write_all(code.as_bytes()).unwrap(); |
| 82 | +} |
0 commit comments