util.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #[allow(dead_code)]
  98. pub fn path_buf_with_component(path: &Path, components: Vec<&str>) -> PathBuf {
  99. let mut path_buf = path.to_path_buf();
  100. for component in components {
  101. path_buf.push(component);
  102. }
  103. path_buf
  104. }
  105. #[allow(dead_code)]
  106. pub fn walk_dir<P: AsRef<Path>, F1, F2>(dir: P, filter: F2, mut path_and_name: F1)
  107. where
  108. F1: FnMut(String, String),
  109. F2: Fn(&walkdir::DirEntry) -> bool,
  110. {
  111. for (path, name) in WalkDir::new(dir)
  112. .into_iter()
  113. .filter_map(|e| e.ok())
  114. .filter(|e| filter(e))
  115. .map(|e| {
  116. (
  117. e.path().to_str().unwrap().to_string(),
  118. e.path().file_stem().unwrap().to_str().unwrap().to_string(),
  119. )
  120. })
  121. {
  122. path_and_name(path, name);
  123. }
  124. }
  125. #[allow(dead_code)]
  126. pub fn suffix_relative_to_path(path: &str, base: &str) -> String {
  127. let base = Path::new(base);
  128. let path = Path::new(path);
  129. path.strip_prefix(base).unwrap().to_str().unwrap().to_owned()
  130. }
  131. pub fn get_tera(directory: &str) -> Tera {
  132. let mut root = format!("{}/src/", env!("CARGO_MANIFEST_DIR"));
  133. root.push_str(directory);
  134. let root_absolute_path = match std::fs::canonicalize(&root) {
  135. Ok(p) => p.as_path().display().to_string(),
  136. Err(e) => {
  137. panic!("❌ Canonicalize file path {} failed {:?}", root, e);
  138. }
  139. };
  140. let mut template_path = format!("{}/**/*.tera", root_absolute_path);
  141. if cfg!(windows) {
  142. // remove "\\?\" prefix on windows
  143. template_path = format!("{}/**/*.tera", &root_absolute_path[4..]);
  144. }
  145. match Tera::new(template_path.as_ref()) {
  146. Ok(t) => t,
  147. Err(e) => {
  148. log::error!("Parsing error(s): {}", e);
  149. ::std::process::exit(1);
  150. }
  151. }
  152. }
  153. pub fn cache_dir() -> PathBuf {
  154. let mut path_buf = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
  155. path_buf.push(".cache");
  156. path_buf
  157. }