|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use std::env; |
| 7 | +use std::fs::File; |
| 8 | +use std::io::Write; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +pub fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 12 | + let out_dir = env::var("OUT_DIR")?; |
| 13 | + |
| 14 | + let mut embedded_file = File::create(Path::new(&out_dir).join("embedded_locales.rs"))?; |
| 15 | + |
| 16 | + writeln!(embedded_file, "// Generated at compile time - do not edit")?; |
| 17 | + writeln!( |
| 18 | + embedded_file, |
| 19 | + "// This file contains embedded English locale files" |
| 20 | + )?; |
| 21 | + writeln!(embedded_file)?; |
| 22 | + writeln!(embedded_file, "use std::collections::HashMap;")?; |
| 23 | + writeln!(embedded_file)?; |
| 24 | + |
| 25 | + // Start the function that returns embedded locales |
| 26 | + writeln!( |
| 27 | + embedded_file, |
| 28 | + "pub fn get_embedded_locales() -> HashMap<&'static str, &'static str> {{" |
| 29 | + )?; |
| 30 | + writeln!(embedded_file, " let mut locales = HashMap::new();")?; |
| 31 | + writeln!(embedded_file)?; |
| 32 | + |
| 33 | + // Try to detect if we're building for a specific utility by checking build configuration |
| 34 | + // This attempts to identify individual utility builds vs multicall binary builds |
| 35 | + let target_utility = detect_target_utility(); |
| 36 | + |
| 37 | + match target_utility { |
| 38 | + Some(util_name) => { |
| 39 | + // Embed only the specific utility's locale (cat.ftl for cat for example) |
| 40 | + embed_single_utility_locale(&mut embedded_file, &project_root()?, &util_name)?; |
| 41 | + } |
| 42 | + None => { |
| 43 | + // Embed all utilities locales (multicall binary or fallback) |
| 44 | + embed_all_utilities_locales(&mut embedded_file, &project_root()?)?; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + writeln!(embedded_file)?; |
| 49 | + writeln!(embedded_file, " locales")?; |
| 50 | + writeln!(embedded_file, "}}")?; |
| 51 | + |
| 52 | + embedded_file.flush()?; |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +/// Get the project root directory |
| 57 | +fn project_root() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> { |
| 58 | + let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; |
| 59 | + let uucore_path = std::path::Path::new(&manifest_dir); |
| 60 | + |
| 61 | + // Navigate from src/uucore to project root |
| 62 | + let project_root = uucore_path |
| 63 | + .parent() // src/ |
| 64 | + .and_then(|p| p.parent()) // project root |
| 65 | + .ok_or("Could not determine project root")?; |
| 66 | + |
| 67 | + Ok(project_root.to_path_buf()) |
| 68 | +} |
| 69 | + |
| 70 | +/// Attempt to detect which specific utility is being built |
| 71 | +fn detect_target_utility() -> Option<String> { |
| 72 | + use std::fs; |
| 73 | + |
| 74 | + // First check if an explicit environment variable was set |
| 75 | + if let Ok(target_util) = env::var("UUCORE_TARGET_UTIL") { |
| 76 | + if !target_util.is_empty() { |
| 77 | + return Some(target_util); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Check for a build configuration file in the target directory |
| 82 | + if let Ok(target_dir) = env::var("CARGO_TARGET_DIR") { |
| 83 | + let config_path = std::path::Path::new(&target_dir).join("uucore_target_util.txt"); |
| 84 | + if let Ok(content) = fs::read_to_string(&config_path) { |
| 85 | + let util_name = content.trim(); |
| 86 | + if !util_name.is_empty() && util_name != "multicall" { |
| 87 | + return Some(util_name.to_string()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Fallback: Check the default target directory |
| 93 | + if let Ok(project_root) = project_root() { |
| 94 | + let config_path = project_root.join("target/uucore_target_util.txt"); |
| 95 | + if let Ok(content) = fs::read_to_string(&config_path) { |
| 96 | + let util_name = content.trim(); |
| 97 | + if !util_name.is_empty() && util_name != "multicall" { |
| 98 | + return Some(util_name.to_string()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // If no configuration found, assume multicall build |
| 104 | + None |
| 105 | +} |
| 106 | + |
| 107 | +/// Embed locale for a single specific utility |
| 108 | +fn embed_single_utility_locale( |
| 109 | + embedded_file: &mut std::fs::File, |
| 110 | + project_root: &Path, |
| 111 | + util_name: &str, |
| 112 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 113 | + use std::fs; |
| 114 | + |
| 115 | + // Embed the specific utility's locale |
| 116 | + let locale_path = project_root |
| 117 | + .join("src/uu") |
| 118 | + .join(util_name) |
| 119 | + .join("locales/en-US.ftl"); |
| 120 | + |
| 121 | + if locale_path.exists() { |
| 122 | + let content = fs::read_to_string(&locale_path)?; |
| 123 | + writeln!(embedded_file, " // Locale for {util_name}")?; |
| 124 | + writeln!( |
| 125 | + embedded_file, |
| 126 | + " locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);" |
| 127 | + )?; |
| 128 | + writeln!(embedded_file)?; |
| 129 | + |
| 130 | + // Tell Cargo to rerun if this file changes |
| 131 | + println!("cargo:rerun-if-changed={}", locale_path.display()); |
| 132 | + } |
| 133 | + |
| 134 | + // Always embed uucore locale file if it exists |
| 135 | + let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl"); |
| 136 | + if uucore_locale_path.exists() { |
| 137 | + let content = fs::read_to_string(&uucore_locale_path)?; |
| 138 | + writeln!(embedded_file, " // Common uucore locale")?; |
| 139 | + writeln!( |
| 140 | + embedded_file, |
| 141 | + " locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);" |
| 142 | + )?; |
| 143 | + println!("cargo:rerun-if-changed={}", uucore_locale_path.display()); |
| 144 | + } |
| 145 | + |
| 146 | + Ok(()) |
| 147 | +} |
| 148 | + |
| 149 | +/// Embed locale files for all utilities (multicall binary) |
| 150 | +fn embed_all_utilities_locales( |
| 151 | + embedded_file: &mut std::fs::File, |
| 152 | + project_root: &Path, |
| 153 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 154 | + use std::fs; |
| 155 | + |
| 156 | + // Discover all uu_* directories |
| 157 | + let src_uu_dir = project_root.join("src/uu"); |
| 158 | + if !src_uu_dir.exists() { |
| 159 | + return Ok(()); |
| 160 | + } |
| 161 | + |
| 162 | + let mut util_dirs = Vec::new(); |
| 163 | + for entry in fs::read_dir(&src_uu_dir)? { |
| 164 | + let entry = entry?; |
| 165 | + if entry.file_type()?.is_dir() { |
| 166 | + if let Some(dir_name) = entry.file_name().to_str() { |
| 167 | + util_dirs.push(dir_name.to_string()); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + util_dirs.sort(); |
| 172 | + |
| 173 | + // Embed locale files for each utility |
| 174 | + for util_name in &util_dirs { |
| 175 | + let locale_path = src_uu_dir.join(util_name).join("locales/en-US.ftl"); |
| 176 | + if locale_path.exists() { |
| 177 | + let content = fs::read_to_string(&locale_path)?; |
| 178 | + writeln!(embedded_file, " // Locale for {util_name}")?; |
| 179 | + writeln!( |
| 180 | + embedded_file, |
| 181 | + " locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);" |
| 182 | + )?; |
| 183 | + writeln!(embedded_file)?; |
| 184 | + |
| 185 | + // Tell Cargo to rerun if this file changes |
| 186 | + println!("cargo:rerun-if-changed={}", locale_path.display()); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // Also embed uucore locale file if it exists |
| 191 | + let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl"); |
| 192 | + if uucore_locale_path.exists() { |
| 193 | + let content = fs::read_to_string(&uucore_locale_path)?; |
| 194 | + writeln!(embedded_file, " // Common uucore locale")?; |
| 195 | + writeln!( |
| 196 | + embedded_file, |
| 197 | + " locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);" |
| 198 | + )?; |
| 199 | + println!("cargo:rerun-if-changed={}", uucore_locale_path.display()); |
| 200 | + } |
| 201 | + |
| 202 | + embedded_file.flush()?; |
| 203 | + Ok(()) |
| 204 | +} |
0 commit comments