Skip to content

Commit

Permalink
Add build.rs for glob all yaml files for cargo build cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 6, 2021
1 parent 5bb271c commit 0fe075d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
authors = ["Jason Lee <[email protected]>"]
build = "build.rs"
categories = ["localization", "internationalization"]
description = "Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation texts."
edition = "2018"
Expand All @@ -9,11 +10,15 @@ license = "MIT"
name = "rust-i18n"
readme = "README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "0.2.1-alpha.0"
version = "0.2.1"

[dependencies]
lazy_static = "1.4.0"
rust-i18n-support = {path = "./crates/support", version = "0.2"}

[dev-dependencies]
foo = {path = "examples/foo"}

[build-dependencies]
glob = "0.3"
regex = "1"
27 changes: 27 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://github.com/longbridgeapp/rust-i18n/blob/v0.1.6/crates/support/src/lib.rs#L9
fn workdir(dest: &str) -> String {
let seperator = regex::Regex::new(r"(/target/(.+?)/build/)|(\\target\\(.+?)\\build\\)")
.expect("Invalid regex");
let parts = seperator.split(dest).collect::<Vec<_>>();

if parts.len() < 2 {
panic!("Parse workdir error, {} not correct.", dest);
}

parts[0].to_string()
}

fn find_all_yaml_for_cargo_cache() {
let dest = std::env::var("OUT_DIR").expect("OUT_DIR env not found");
let workdir = workdir(&dest);
let locale_path = format!("{}/**/locales/**/*.yml", workdir);

for entry in glob::glob(&locale_path).expect("Failed to read glob pattern") {
let entry = entry.unwrap();
println!("cargo:rerun-if-changed={}", entry.display());
}
}

fn main() {
find_all_yaml_for_cargo_cache();
}
2 changes: 1 addition & 1 deletion crates/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "rust-i18n-support"
readme = "../../README.md"
repository = "https://github.com/longbridgeapp/rust-i18n"
version = "0.2.1"
version = "0.2.2"

[dependencies]
glob = "0.3"
Expand Down
3 changes: 1 addition & 2 deletions crates/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ fn load_locales(locales_path: &str) -> Translations {
let path_pattern = format!("{}/**/*.yml", locales_path);

if is_debug() {
println!("cargo:i18n-locale-path={}", &path_pattern);
println!("cargo:i18n-locale={}", &path_pattern);
}

for entry in glob(&path_pattern).expect("Failed to read glob pattern") {
let entry = entry.unwrap();
if is_debug() {
println!("cargo:i18n-load={}", &entry.display());
}
println!("cargo:rerun-if-changed={}", entry.display());

let file = File::open(entry).expect("Failed to open the YAML file");
let mut reader = std::io::BufReader::new(file);
Expand Down

0 comments on commit 0fe075d

Please sign in to comment.