cargo_zng/
l10n.rs

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Localization text scrapper.
//!
//! See the [`l10n!`] documentation for more details.
//!
//! [`l10n!`]: https://zng-ui.github.io/doc/zng/l10n/macro.l10n.html#scrap-template

use std::{
    cmp::Ordering,
    fmt::Write as _,
    fs, io,
    path::{Path, PathBuf},
};

use clap::*;

use crate::util;

mod pseudo;
mod scraper;

#[derive(Args, Debug)]
pub struct L10nArgs {
    /// Rust files glob or directory
    #[arg(short, long, default_value = "")]
    input: String,

    /// L10n resources dir
    #[arg(short, long, default_value = "")]
    output: String,

    /// Package to scrap and copy dependencies
    ///
    /// If set the --input and --output default is src/**.rs and l10n/
    #[arg(short, long, default_value = "")]
    package: String,

    /// Path to Cargo.toml of crate to scrap and copy dependencies
    ///
    /// If set the --input and --output default to src/**.rs and l10n/
    #[arg(long, default_value = "")]
    manifest_path: String,

    /// Don't copy dependencies localization
    ///
    /// Use with --package or --manifest-path to not copy {dep-pkg}/l10n/*.ftl files
    #[arg(long, action)]
    no_deps: bool,

    /// Don't scrap `#.#.#-local` dependencies
    ///
    /// Use with --package or --manifest-path to not scrap local dependencies.
    #[arg(long, action)]
    no_local: bool,

    /// Don't scrap the target package.
    ///
    /// Use with --package or --manifest-path to only scrap dependencies.
    #[arg(long, action)]
    no_pkg: bool,

    /// Remove all previously copied dependency localization files.
    #[arg(long, action)]
    clean_deps: bool,

    /// Remove all previously scraped resources before scraping.
    #[arg(long, action)]
    clean_template: bool,

    /// Same as --clean-deps --clean-template
    #[arg(long, action)]
    clean: bool,

    /// Custom l10n macro names, comma separated
    #[arg(short, long, default_value = "")]
    macros: String,

    /// Generate pseudo locale from dir/lang
    ///
    /// EXAMPLE
    ///
    /// "l10n/en" generates pseudo from "l10n/en.ftl" and "l10n/en/*.ftl"
    #[arg(long, default_value = "")]
    pseudo: String,
    /// Generate pseudo mirrored locale
    #[arg(long, default_value = "")]
    pseudo_m: String,
    /// Generate pseudo wide locale
    #[arg(long, default_value = "")]
    pseudo_w: String,

    /// Only verify that the generated files are the same
    #[arg(long, action)]
    check: bool,

    /// Use verbose output.
    #[arg(short, long, action)]
    verbose: bool,
}

pub fn run(args: L10nArgs) {
    run_impl(args, false);
}
fn run_impl(mut args: L10nArgs, is_local_scrap_recursion: bool) {
    if !args.package.is_empty() && !args.manifest_path.is_empty() {
        fatal!("only one of --package --manifest-path must be set")
    }

    let mut input = String::new();
    let mut output = args.output.replace('\\', "/");

    if !args.input.is_empty() {
        input = args.input.replace('\\', "/");

        if !input.contains('*') && PathBuf::from(&input).is_dir() {
            input = format!("{}/**/*.rs", input.trim_end_matches('/'));
        }
    }
    if !args.package.is_empty() {
        if let Some(m) = crate::util::manifest_path_from_package(&args.package) {
            args.manifest_path = m;
        } else {
            fatal!("package `{}` not found in workspace", args.package);
        }
    }

    if !args.manifest_path.is_empty() {
        if !Path::new(&args.manifest_path).exists() {
            fatal!("{input} does not exist")
        }

        if let Some(path) = args.manifest_path.replace('\\', "/").strip_suffix("/Cargo.toml") {
            if output.is_empty() {
                output = format!("{path}/l10n");
            }
            if input.is_empty() {
                input = format!("{path}/src/**/*.rs");
            }
        } else {
            fatal!("expected path to Cargo.toml manifest file");
        }
    }

    if args.check {
        args.clean = false;
        args.clean_deps = false;
        args.clean_template = false;
    } else if args.clean {
        args.clean_deps = true;
        args.clean_template = true;
    }

    if args.verbose && !is_local_scrap_recursion {
        println!(
            "input: `{input}`\noutput: `{output}`\nclean_deps: {}\nclean_template: {}",
            args.clean_deps, args.clean_template
        );
    }

    if !input.is_empty() {
        if output.is_empty() {
            fatal!("--output is required for --input")
        }

        // scrap the target package
        if !args.no_pkg {
            if args.check {
                println!(r#"checking "{input}".."#);
            } else {
                println!(r#"scraping "{input}".."#);
            }

            let custom_macro_names: Vec<&str> = args.macros.split(',').map(|n| n.trim()).collect();
            // let args = ();

            let mut template = scraper::scrape_fluent_text(&input, &custom_macro_names);
            if !args.check {
                match template.entries.len() {
                    0 => println!("did not find any entry"),
                    1 => println!("found 1 entry"),
                    n => println!("found {n} entries"),
                }
            }

            if !template.entries.is_empty() || !template.notes.is_empty() {
                if let Err(e) = util::check_or_create_dir_all(args.check, &output) {
                    fatal!("cannot create dir `{output}`, {e}");
                }

                template.sort();

                let r = template.write(|file, contents| {
                    let mut output = PathBuf::from(&output);
                    output.push("template");

                    if args.clean_template {
                        debug_assert!(!args.check);
                        if args.verbose {
                            println!("removing `{}` to clean template", output.display());
                        }
                        if let Err(e) = fs::remove_dir_all(&output) {
                            if !matches!(e.kind(), io::ErrorKind::NotFound) {
                                error!("cannot remove `{}`, {e}", output.display());
                            }
                        }
                    }
                    util::check_or_create_dir_all(args.check, &output)?;
                    output.push(format!("{}.ftl", if file.is_empty() { "_" } else { file }));
                    util::check_or_write(args.check, output, contents, args.verbose)
                });
                if let Err(e) = r {
                    fatal!("error writing template files, {e}");
                }
            }
        }

        // cleanup dependencies
        let l10n_dir = Path::new(&output);
        if args.clean_deps {
            for entry in glob::glob(&format!("{}/*/deps", l10n_dir.display()))
                .unwrap_or_else(|e| fatal!("cannot cleanup deps in `{}`, {e}", l10n_dir.display()))
            {
                let dir = entry.unwrap_or_else(|e| fatal!("cannot cleanup deps, {e}"));
                if args.verbose {
                    println!("removing `{}` to clean deps", dir.display());
                }
                if let Err(e) = std::fs::remove_dir_all(&dir) {
                    if !matches!(e.kind(), io::ErrorKind::NotFound) {
                        error!("cannot remove `{}`, {e}", dir.display());
                    }
                }
            }
        }

        // collect dependencies
        let mut local = vec![];
        if !args.no_deps {
            let mut count = 0;
            let (workspace_root, deps) = util::dependencies(&args.manifest_path);
            for dep in deps {
                if dep.version.pre.as_str() == "local" && dep.manifest_path.starts_with(&workspace_root) {
                    local.push(dep);
                    continue;
                }

                let dep_l10n = dep.manifest_path.with_file_name("l10n");
                let dep_l10n_reader = match fs::read_dir(&dep_l10n) {
                    Ok(d) => d,
                    Err(e) => {
                        if !matches!(e.kind(), io::ErrorKind::NotFound) {
                            error!("cannot read `{}`, {e}", dep_l10n.display());
                        }
                        continue;
                    }
                };

                let mut any = false;

                // get l10n_dir/{lang}/deps/dep.name/dep.version/
                let mut l10n_dir = |lang: Option<&std::ffi::OsStr>| {
                    any = true;
                    let dir = l10n_dir.join(lang.unwrap()).join("deps");

                    let ignore_file = dir.join(".gitignore");

                    if !ignore_file.exists() {
                        // create dir and .gitignore file
                        (|| -> io::Result<()> {
                            util::check_or_create_dir_all(args.check, &dir)?;

                            let mut ignore = "# Dependency localization files\n".to_owned();

                            let output = Path::new(&output);
                            let custom_output = if output != Path::new(&args.manifest_path).with_file_name("l10n") {
                                format!(
                                    " --output \"{}\"",
                                    output.strip_prefix(std::env::current_dir().unwrap()).unwrap_or(output).display()
                                )
                                .replace('\\', "/")
                            } else {
                                String::new()
                            };
                            if !args.package.is_empty() {
                                writeln!(
                                    &mut ignore,
                                    "# Call `cargo zng l10n --package {}{custom_output} --no-pkg --no-local --clean-deps` to update",
                                    args.package
                                )
                                .unwrap();
                            } else {
                                let path = Path::new(&args.manifest_path);
                                let path = path.strip_prefix(std::env::current_dir().unwrap()).unwrap_or(path);
                                writeln!(
                                    &mut ignore,
                                    "# Call `cargo zng l10n --manifest-path \"{}\" --no-pkg --no-local --clean-deps` to update",
                                    path.display()
                                )
                                .unwrap();
                            }
                            writeln!(&mut ignore).unwrap();
                            writeln!(&mut ignore, "*").unwrap();
                            writeln!(&mut ignore, "!.gitignore").unwrap();

                            if let Err(e) = fs::write(&ignore_file, ignore.as_bytes()) {
                                fatal!("cannot write `{}`, {e}", ignore_file.display())
                            }

                            Ok(())
                        })()
                        .unwrap_or_else(|e| fatal!("cannot create `{}`, {e}", l10n_dir.display()));
                    }

                    let dir = dir.join(&dep.name).join(dep.version.to_string());
                    let _ = util::check_or_create_dir_all(args.check, &dir);

                    dir
                };

                // [(exporter_dep, ".../{lang}?/deps")]
                let mut reexport_deps = vec![];

                for dep_l10n_entry in dep_l10n_reader {
                    let dep_l10n_entry = match dep_l10n_entry {
                        Ok(e) => e.path(),
                        Err(e) => {
                            error!("cannot read `{}` entry, {e}", dep_l10n.display());
                            continue;
                        }
                    };
                    if dep_l10n_entry.is_dir() {
                        // l10n/{lang}/deps/{dep.name}/{dep.version}
                        let output_dir = l10n_dir(dep_l10n_entry.file_name());
                        let _ = util::check_or_create_dir_all(args.check, &output_dir);

                        let lang_dir_reader = match fs::read_dir(&dep_l10n_entry) {
                            Ok(d) => d,
                            Err(e) => {
                                error!("cannot read `{}`, {e}", dep_l10n_entry.display());
                                continue;
                            }
                        };

                        for lang_entry in lang_dir_reader {
                            let lang_entry = match lang_entry {
                                Ok(e) => e.path(),
                                Err(e) => {
                                    error!("cannot read `{}` entry, {e}", dep_l10n_entry.display());
                                    continue;
                                }
                            };

                            if lang_entry.is_dir() {
                                if lang_entry.file_name().map(|n| n == "deps").unwrap_or(false) {
                                    reexport_deps.push((&dep, lang_entry));
                                }
                            } else if lang_entry.is_file() && lang_entry.extension().map(|e| e == "ftl").unwrap_or(false) {
                                let _ = util::check_or_create_dir_all(args.check, &output_dir);
                                let to = output_dir.join(lang_entry.file_name().unwrap());
                                if let Err(e) = util::check_or_copy(args.check, &lang_entry, &to, args.verbose) {
                                    error!("cannot copy `{}` to `{}`, {e}", lang_entry.display(), to.display());
                                    continue;
                                }
                            }
                        }
                    }
                }

                reexport_deps.sort_by(|a, b| match a.0.name.cmp(&b.0.name) {
                    Ordering::Equal => b.0.version.cmp(&a.0.version),
                    o => o,
                });

                for (_, deps) in reexport_deps {
                    // dep/l10n/lang/deps/
                    let target = l10n_dir(deps.parent().and_then(|p| p.file_name()));

                    // deps/pkg-name/pkg-version/*.ftl
                    for entry in glob::glob(&deps.join("*/*/*.ftl").display().to_string()).unwrap() {
                        let entry = entry.unwrap_or_else(|e| fatal!("cannot read `{}` entry, {e}", deps.display()));
                        let target = target.join(entry.strip_prefix(&deps).unwrap());
                        if !target.exists() && entry.is_file() {
                            if let Err(e) = util::check_or_copy(args.check, &entry, &target, args.verbose) {
                                error!("cannot copy `{}` to `{}`, {e}", entry.display(), target.display());
                            }
                        }
                    }
                }

                count += any as u32;
            }
            println!("found {count} dependencies with localization");
        }

        // scrap local dependencies
        if !args.no_local {
            for dep in local {
                run_impl(
                    L10nArgs {
                        input: String::new(),
                        output: output.clone(),
                        package: String::new(),
                        manifest_path: dep.manifest_path.display().to_string(),
                        no_deps: true,
                        no_local: true,
                        no_pkg: false,
                        clean_deps: false,
                        clean_template: false,
                        clean: false,
                        macros: args.macros.clone(),
                        pseudo: String::new(),
                        pseudo_m: String::new(),
                        pseudo_w: String::new(),
                        check: args.check,
                        verbose: args.verbose,
                    },
                    true,
                )
            }
        }
    }

    if !args.pseudo.is_empty() {
        pseudo::pseudo(&args.pseudo, args.check, args.verbose);
    }
    if !args.pseudo_m.is_empty() {
        pseudo::pseudo_mirr(&args.pseudo_m, args.check, args.verbose);
    }
    if !args.pseudo_w.is_empty() {
        pseudo::pseudo_wide(&args.pseudo_w, args.check, args.verbose);
    }
}