use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use async_trait::async_trait; use regex::Regex; use std::cmp::max; use std::error::Error; use std::fs::File as FsFile; use std::io; use std::io::{BufRead, BufReader}; use std::path::Path; use std::path::PathBuf; #[derive(Debug)] pub struct Translation<'a, C, D, R> { path: D, version: &'a str, locale: C, command_runner: &'a R, } impl<'a, D, C: AsRef, R: CommandRunner> Translation<'a, C, D, R> { pub fn new(path: D, version: &'a str, locale: C, command_runner: &'a R) -> Self { Self { path, version, locale, command_runner, } } } impl, D: AsRef, R: CommandRunner> Translation<'_, C, D, R> { fn get_pairs(&self) -> Vec<(String, PathBuf)> { let version_x = self .version .trim_end_matches(|c: char| c.is_digit(10)) .to_owned() + "x"; let locale = self.locale.as_ref(); let path_locale = if locale == "de_DE" { "de".to_owned() } else { locale.to_lowercase().replace('_', "-") }; let mut res = vec![]; for &(in_slug, out_slug) in &[ ("", ""), ("cc/", "continents-cities-"), ("admin/", "admin-"), ("admin/network/", "admin-network-"), ] { for format in &["po", "mo"] { res.push(( format!("https://translate.wordpress.org/projects/wp/{}/{}{}/default/export-translations?format={}", version_x, in_slug, path_locale, format), [self.path.as_ref(), format!("{}{}.{}", out_slug, self.locale.as_ref(), format).as_ref()].iter().collect() )) } } res } } #[async_trait(?Send)] impl, D: AsRef, R: CommandRunner> Symbol for Translation<'_, C, D, R> { async fn target_reached(&self) -> Result> { let mut newest = String::new(); let match_date = Regex::new("(?m)^\"PO-Revision-Date: (.+)\\+0000\\\\n\"$").unwrap(); for (_, target) in self.get_pairs() { match FsFile::open(target.clone()) { Err(e) => { // Check if file exists return if e.kind() == io::ErrorKind::NotFound { Ok(false) } else { Err(Box::new(e)) }; } Ok(file) => { if target.ends_with(".po") { let reader = BufReader::new(file); for content in reader.lines() { if let Some(match_result) = match_date.captures(&content?) { newest = max(newest, match_result[1].to_string()); break; } } } } } } let upstream = self .command_runner .get_output( "curl", args![format!( "https://api.wordpress.org/core/version-check/1.7/?version={}&locale={}", self.version, self.locale.as_ref() )], ) .await?; Ok(String::from_utf8(upstream)?.contains(&format!( r###"language":"{}","version":"{}","updated":"{}"###, self.locale.as_ref(), self.version, newest ))) } async fn execute(&self) -> Result<(), Box> { for (source, target) in self.get_pairs() { self .command_runner .run_successfully("curl", args!["--compressed", "-o", target, source,]) .await?; } Ok(()) } }