Skip to content

Commit

Permalink
Update to clap 4.x and clap derive macros
Browse files Browse the repository at this point in the history
- also fix grammar and spelling in the help
  • Loading branch information
urkle committed Nov 14, 2023
1 parent 50159cc commit 7e8b673
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ version = "2.1.0"

[dependencies]
anyhow = "1"
clap = "2.32"
itertools = "0.10.3"
clap = { version = "4.1.14", features = ["derive"] }
itertools = "0.11.0"
rust-i18n-support = { path = "../support", version = "2.1.0" }
rust-i18n-extract = { path = "../extract", version = "2.1.0" }
serde = { version = "1", features = ["derive"] }
Expand Down
82 changes: 38 additions & 44 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,61 @@
use anyhow::Error;
use clap::{App, Arg, SubCommand};
use clap::{Args, Parser};

use std::{collections::HashMap, path::Path};

use rust_i18n_extract::{extractor, generator, iter};
mod config;

const APP_NAME: &str = "rust-i18n";
const ABOUT: &str = r#"Rust I18n command for help you simply to extract all untranslated texts from soruce code.
It will iter all Rust files in and extract all untranslated texts that used `t!` macro.
And then generate a YAML file and merge for existing texts.
#[derive(Parser)]
#[command(name = "cargo")]
#[command(bin_name = "cargo")]
enum CargoCli {
I18n(I18nArgs),
}

https://github.com/longbridgeapp/rust-i18n
"#;
#[derive(Args)]
#[command(author, version)]
// #[command(propagate_version = true)]
/// Rust I18n command to help you extract all untranslated texts from source code.
///
/// It will iterate all Rust files in the source directory and extract all untranslated texts
/// that used `t!` macro.
/// Then it will generate a YAML file and merge with the existing translations.
///
/// https://github.com/longbridgeapp/rust-i18n
struct I18nArgs {
/// Extract all untranslated I18n texts from source code
#[arg(default_value = "./")]
source: Option<String>,
}

fn main() -> Result<(), Error> {
let extract_command = SubCommand::with_name("i18n")
.about("Extract all untranslated I18n texts from soruce code")
.version(clap::crate_version!())
.arg(
Arg::with_name("source")
.help("Path of your Rust crate root and Cargo.toml")
.default_value("./"),
);

let app = App::new(APP_NAME)
.bin_name("cargo")
.about(ABOUT)
.subcommand(extract_command)
.get_matches();
let CargoCli::I18n(args) = CargoCli::parse();

let mut results = HashMap::new();

#[allow(clippy::single_match)]
match app.subcommand() {
("i18n", Some(sub_m)) => {
let source_path = sub_m.value_of("source").expect("Missing source path");
let source_path = args.source.expect("Missing source path");

let cfg = config::load(std::path::Path::new(source_path))?;
let cfg = config::load(std::path::Path::new(&source_path))?;

iter::iter_crate(source_path, |path, source| {
extractor::extract(&mut results, path, source)
})?;
iter::iter_crate(&source_path, |path, source| {
extractor::extract(&mut results, path, source)
})?;

let mut messages: Vec<_> = results.values().collect();
messages.sort_by_key(|m| m.index);
let mut messages: Vec<_> = results.values().collect();
messages.sort_by_key(|m| m.index);

let mut has_error = false;
let mut has_error = false;

let output_path = Path::new(source_path).join(&cfg.load_path);
let output_path = Path::new(&source_path).join(&cfg.load_path);

let result =
generator::generate(&output_path, &cfg.available_locales, messages.clone());
if result.is_err() {
has_error = true;
}
let result = generator::generate(&output_path, &cfg.available_locales, messages.clone());
if result.is_err() {
has_error = true;
}

if has_error {
std::process::exit(1);
}
}
_ => {}
if has_error {
std::process::exit(1);
}

Ok(())
Expand Down

0 comments on commit 7e8b673

Please sign in to comment.