A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use regex::Regex;
  4. use std::cmp::max;
  5. use std::error::Error;
  6. use std::fs::File as FsFile;
  7. use std::io;
  8. use std::io::{BufRead, BufReader};
  9. use std::path::Path;
  10. use std::path::PathBuf;
  11. #[derive(Debug)]
  12. pub struct Translation<'a, C, D, R> {
  13. path: D,
  14. version: &'a str,
  15. locale: C,
  16. command_runner: &'a R,
  17. }
  18. impl<'a, D, C: AsRef<str>, R: CommandRunner> Translation<'a, C, D, R> {
  19. pub fn new(path: D, version: &'a str, locale: C, command_runner: &'a R) -> Self {
  20. Self {
  21. path,
  22. version,
  23. locale,
  24. command_runner,
  25. }
  26. }
  27. }
  28. impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Translation<'_, C, D, R> {
  29. fn get_pairs(&self) -> Vec<(String, PathBuf)> {
  30. let version_x = self
  31. .version
  32. .trim_end_matches(|c: char| c.is_digit(10))
  33. .to_owned()
  34. + "x";
  35. let locale = self.locale.as_ref();
  36. let path_locale = if locale == "de_DE" {
  37. "de".to_owned()
  38. } else {
  39. locale.to_lowercase().replace('_', "-")
  40. };
  41. let mut res = vec![];
  42. for &(in_slug, out_slug) in &[
  43. ("", ""),
  44. ("cc/", "continents-cities-"),
  45. ("admin/", "admin-"),
  46. ("admin/network/", "admin-network-"),
  47. ] {
  48. for format in &["po", "mo"] {
  49. res.push((
  50. format!("https://translate.wordpress.org/projects/wp/{}/{}{}/default/export-translations?format={}", version_x, in_slug, path_locale, format),
  51. [self.path.as_ref(), format!("{}{}.{}", out_slug, self.locale.as_ref(), format).as_ref()].iter().collect()
  52. ))
  53. }
  54. }
  55. res
  56. }
  57. }
  58. impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Symbol for Translation<'_, C, D, R> {
  59. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  60. let mut newest = String::new();
  61. let match_date = Regex::new("(?m)^\"PO-Revision-Date: (.+)\\+0000\\\\n\"$").unwrap();
  62. for (_, target) in self.get_pairs() {
  63. match FsFile::open(target.clone()) {
  64. Err(e) => {
  65. // Check if file exists
  66. return if e.kind() == io::ErrorKind::NotFound {
  67. Ok(false)
  68. } else {
  69. Err(Box::new(e))
  70. };
  71. }
  72. Ok(file) => {
  73. if target.ends_with(".po") {
  74. let reader = BufReader::new(file);
  75. for content in reader.lines() {
  76. if let Some(match_result) = match_date.captures(&content?) {
  77. newest = max(newest, match_result[1].to_string());
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. let upstream = self.command_runner.get_output(
  86. "curl",
  87. args![format!(
  88. "https://api.wordpress.org/core/version-check/1.7/?version={}&locale={}",
  89. self.version,
  90. self.locale.as_ref()
  91. )],
  92. )?;
  93. Ok(String::from_utf8(upstream)?.contains(&format!(
  94. r###"language":"{}","version":"{}","updated":"{}"###,
  95. self.locale.as_ref(),
  96. self.version,
  97. newest
  98. )))
  99. }
  100. fn execute(&self) -> Result<(), Box<dyn Error>> {
  101. for (source, target) in self.get_pairs() {
  102. self
  103. .command_runner
  104. .run_successfully("curl", args!["--compressed", "-o", target, source,])?;
  105. }
  106. Ok(())
  107. }
  108. }