Unverified Commit 3b3b2614 authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

Adding assets for a simple example

parent a7cbdcb4
/target
Cargo.lock
assets
name: "traduki"
descrption: "Translate assets for you application or library"
other_key:
text: |
This is a random key that can be used anywhere, really!
Yaml was chosen as a format because it's a key-value representation,
but can also pretty easily be used to write long multi-line strings.
reflow: false # this setting specifies whether "\n" characters should be stripped from the text
error: "An error has occured and it's probably MY fault!"
name: "traduki"
descrption: "Traduki akiroj por via programo biblioteko"
other_key:
text: |
Ĉi tio estas sencela indexo, kiu povas esti utiza ie ajn, vere.
Yaml estis elektika kiel formato ĉar ĝi estas indexo-valoro reprezentado,
sed povas utizi facila skribi longa plurlinia signovico
reflow: false
error: "Eraro okazis kaj estas mia kulpo"
......@@ -4,5 +4,5 @@ include!(concat!(env!("OUT_DIR"), "/traduki.rs"));
fn main() {
println!("{}", concat!(env!("OUT_DIR"), "/traduki.rs"));
println!("{}", clap::test_key());
// println!("{}", clap::test_key());
}
......@@ -10,38 +10,38 @@ pub(crate) use {key::Key, section::Section};
use std::{
fs::{self, File},
io::Write,
path::Path,
path::{Path, PathBuf},
};
pub fn bootstrap<S: Into<String>>(assets: S) -> Result<(), &'static str> {
fn out_dir() -> PathBuf {
let env_var = std::env::var("OUT_DIR");
let out = Path::new(env_var.as_ref().unwrap());
let a = assets.into();
let assets = Path::new(&a);
Path::new(env_var.as_ref().unwrap()).into()
}
fn asset_dir<S: Into<String>>(s: S) -> PathBuf {
let s = s.into();
println!("cargo:rerun-if-changed={}", s);
Path::new(&s).into()
}
parser::load_keys("whatevs", vec![&assets.join("app").join("en_GB.yml")]);
pub fn bootstrap<S: Into<String>>(assets: S) {
let out = out_dir();
let assets = asset_dir(assets);
let _ = fs::remove_dir_all(out);
fs::create_dir_all(out).unwrap();
let mut f = File::create(out.join("traduki.rs")).unwrap();
// Clear the previous codegen
let _ = fs::remove_dir_all(&out);
fs::create_dir_all(&out).expect("Write error: failed to create $OUT directory scaffold!");
// Create a new file for translations
let mut f = File::create(out.join("traduki.rs")).expect("Write error: Failed to create file");
f.write_all(
Section::new("clap")
.add_key(
Key::new("test_key")
.add_value("en_GB", "hello world")
.add_value("eo", "saluton mondo"),
)
.add_child(
Section::new("insert").add_key(
Key::new("other_key")
.add_value("en_GB", "hello world")
.add_value("eo", "saluton mondo"),
),
)
.generate()
parser::handle_root(&assets)
.expect("Write error: failed to read asset directory tree. Do you have permissions?")
.into_iter()
.fold(String::new(), |prev, section| {
format!("{}\n{}", prev, section.generate())
})
.as_bytes(),
)
.unwrap();
Ok(())
.expect("Write error: failed to write generated translations file!");
}
......@@ -9,24 +9,44 @@ use std::{
};
use yaml_rust::{Yaml, YamlLoader};
pub(crate) fn parse_section(path: &Path) -> io::Result<Section> {
type FileEntries = Vec<DirEntry>;
type DirEntries = Vec<DirEntry>;
pub(crate) fn handle_root(path: &Path) -> io::Result<Vec<Section>> {
fs::read_dir(path)?
.into_iter()
.filter_map(|entry| {
let e = entry.unwrap();
if e.file_type().unwrap().is_dir() {
Some(parse_section(&e.path()))
} else {
eprintln!(
"cargo:warning=Skipping key in asset root: `{}`!",
e.path().file_name().unwrap().to_str().unwrap()
);
None
}
})
.collect()
}
fn parse_section(path: &Path) -> io::Result<Section> {
let name = path.file_name().unwrap().to_str().unwrap();
let (dirs, files) = split_dir(path)?;
Ok(fs::read_dir(path)?
let file_paths: Vec<_> = files.iter().map(|e| e.path()).collect();
let section = load_keys(name, file_paths.iter().map(|p| p.as_path()).collect())
.into_iter()
.fold(Section::new(name), |sec, entry| {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_dir() {
sec.add_child(parse_section(&entry.path()).unwrap())
} else {
sec
}
}))
.fold(Section::new(name), |sec, key| sec.add_key(key));
dirs.iter().map(|e| e.path()).fold(Ok(section), |sec, pb| {
let new = parse_section(pb.as_path())?;
sec.and_then(|sec| Ok(sec.add_child(new)))
})
}
fn split_dir(path: &Path) -> io::Result<(Vec<DirEntry>, Vec<DirEntry>)> {
fn split_dir(path: &Path) -> io::Result<(DirEntries, FileEntries)> {
let (a, b): (Vec<_>, Vec<_>) = fs::read_dir(path)?
.into_iter()
.map(|entry| {
......@@ -54,7 +74,7 @@ fn split_dir(path: &Path) -> io::Result<(Vec<DirEntry>, Vec<DirEntry>)> {
///
/// Because the keys can fall-back to the default translation, it's
/// not a critical error. Bit missing the default language is.
pub(crate) fn load_keys(section: &str, path: Vec<&Path>) -> Vec<Key> {
fn load_keys(section: &str, path: Vec<&Path>) -> Vec<Key> {
let def = crate::env::default();
let mut map: BTreeMap<String, Vec<Yaml>> =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment