util.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use console::Style;
  2. use similar::{ChangeTag, TextDiff};
  3. use std::path::{Path, PathBuf};
  4. use std::str::FromStr;
  5. use std::{
  6. fs::{File, OpenOptions},
  7. io::{Read, Write},
  8. };
  9. use tera::Tera;
  10. use walkdir::WalkDir;
  11. pub fn read_file(path: &str) -> Option<String> {
  12. let mut file = File::open(path).unwrap_or_else(|_| panic!("Unable to open file at {}", path));
  13. let mut content = String::new();
  14. match file.read_to_string(&mut content) {
  15. Ok(_) => Some(content),
  16. Err(e) => {
  17. log::error!("{}, with error: {:?}", path, e);
  18. Some("".to_string())
  19. }
  20. }
  21. }
  22. pub fn save_content_to_file_with_diff_prompt(content: &str, output_file: &str) {
  23. if Path::new(output_file).exists() {
  24. let old_content = read_file(output_file).unwrap();
  25. let new_content = content.to_owned();
  26. let write_to_file = || match OpenOptions::new()
  27. .create(true)
  28. .write(true)
  29. .append(false)
  30. .truncate(true)
  31. .open(output_file)
  32. {
  33. Ok(ref mut file) => {
  34. file.write_all(new_content.as_bytes()).unwrap();
  35. }
  36. Err(err) => {
  37. panic!("Failed to open log file: {}", err);
  38. }
  39. };
  40. if new_content != old_content {
  41. print_diff(old_content, new_content.clone());
  42. write_to_file()
  43. }
  44. } else {
  45. match OpenOptions::new().create(true).write(true).open(output_file) {
  46. Ok(ref mut file) => file.write_all(content.as_bytes()).unwrap(),
  47. Err(err) => panic!("Open or create to {} fail: {}", output_file, err),
  48. }
  49. }
  50. }
  51. pub fn print_diff(old_content: String, new_content: String) {
  52. let diff = TextDiff::from_lines(&old_content, &new_content);
  53. for op in diff.ops() {
  54. for change in diff.iter_changes(op) {
  55. let (sign, style) = match change.tag() {
  56. ChangeTag::Delete => ("-", Style::new().red()),
  57. ChangeTag::Insert => ("+", Style::new().green()),
  58. ChangeTag::Equal => (" ", Style::new()),
  59. };
  60. match change.tag() {
  61. ChangeTag::Delete => {
  62. print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
  63. }
  64. ChangeTag::Insert => {
  65. print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
  66. }
  67. ChangeTag::Equal => {}
  68. };
  69. }
  70. println!("---------------------------------------------------");
  71. }
  72. }
  73. #[allow(dead_code)]
  74. pub fn is_crate_dir(e: &walkdir::DirEntry) -> bool {
  75. let cargo = e.path().file_stem().unwrap().to_str().unwrap().to_string();
  76. cargo == *"Cargo"
  77. }
  78. #[allow(dead_code)]
  79. pub fn is_proto_file(e: &walkdir::DirEntry) -> bool {
  80. if e.path().extension().is_none() {
  81. return false;
  82. }
  83. let ext = e.path().extension().unwrap().to_str().unwrap().to_string();
  84. ext == *"proto"
  85. }
  86. pub fn is_hidden(entry: &walkdir::DirEntry) -> bool {
  87. entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
  88. }
  89. pub fn create_dir_if_not_exist(dir: &Path) {
  90. if !dir.exists() {
  91. std::fs::create_dir_all(dir).unwrap();
  92. }
  93. }
  94. pub fn path_string_with_component(path: &Path, components: Vec<&str>) -> String {
  95. path_buf_with_component(path, components).to_str().unwrap().to_string()
  96. }
  97. pub fn path_buf_with_component(path: &Path, components: Vec<&str>) -> PathBuf {
  98. let mut path_buf = path.to_path_buf();
  99. for component in components {
  100. path_buf.push(component);
  101. }
  102. path_buf
  103. }
  104. #[allow(dead_code)]
  105. pub fn walk_dir<P: AsRef<Path>, F1, F2>(dir: P, filter: F2, mut path_and_name: F1)
  106. where
  107. F1: FnMut(String, String),
  108. F2: Fn(&walkdir::DirEntry) -> bool,
  109. {
  110. for (path, name) in WalkDir::new(dir)
  111. .into_iter()
  112. .filter_map(|e| e.ok())
  113. .filter(|e| filter(e))
  114. .map(|e| {
  115. (
  116. e.path().to_str().unwrap().to_string(),
  117. e.path().file_stem().unwrap().to_str().unwrap().to_string(),
  118. )
  119. })
  120. {
  121. path_and_name(path, name);
  122. }
  123. }
  124. #[allow(dead_code)]
  125. pub fn suffix_relative_to_path(path: &str, base: &str) -> String {
  126. let base = Path::new(base);
  127. let path = Path::new(path);
  128. path.strip_prefix(base).unwrap().to_str().unwrap().to_owned()
  129. }
  130. pub fn get_tera(directory: &str) -> Tera {
  131. let mut root = format!("{}/src/code_gen/", env!("CARGO_MANIFEST_DIR"));
  132. root.push_str(directory);
  133. let root_absolute_path = match std::fs::canonicalize(&root) {
  134. Ok(p) => p.as_path().display().to_string(),
  135. Err(e) => {
  136. panic!("❌ Canonicalize file path {} failed {:?}", root, e);
  137. }
  138. };
  139. let mut template_path = format!("{}/**/*.tera", root_absolute_path);
  140. if cfg!(windows) {
  141. // remove "\\?\" prefix on windows
  142. template_path = format!("{}/**/*.tera", &root_absolute_path[4..]);
  143. }
  144. match Tera::new(template_path.as_ref()) {
  145. Ok(t) => t,
  146. Err(e) => {
  147. log::error!("Parsing error(s): {}", e);
  148. ::std::process::exit(1);
  149. }
  150. }
  151. }
  152. pub fn cache_dir() -> PathBuf {
  153. let mut path_buf = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
  154. path_buf.push(".cache");
  155. path_buf
  156. }