build.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use std::fs::{self, File};
  2. use std::io::Write;
  3. use std::path::{Path, PathBuf};
  4. fn main() {
  5. // Step 1: Find `migrations/` folder recursively
  6. let root = Path::new("src");
  7. for migration_path in find_migrations_dirs(root) {
  8. // Step 2: Collect all files inside the migrations dir
  9. let mut files = Vec::new();
  10. visit_dirs(&migration_path, &mut files).expect("Failed to read migrations directory");
  11. files.sort_by(|path_a, path_b| {
  12. let path_a = path_a.file_name().unwrap().to_str().unwrap();
  13. let path_b = path_b.file_name().unwrap().to_str().unwrap();
  14. let prefix_a = path_a
  15. .split("_")
  16. .next()
  17. .and_then(|prefix| prefix.parse::<usize>().ok())
  18. .unwrap_or_default();
  19. let prefix_b = path_b
  20. .split("_")
  21. .next()
  22. .and_then(|prefix| prefix.parse::<usize>().ok())
  23. .unwrap_or_default();
  24. if prefix_a != 0 && prefix_b != 0 {
  25. prefix_a.cmp(&prefix_b)
  26. } else {
  27. path_a.cmp(path_b)
  28. }
  29. });
  30. // Step 3: Output file path (e.g., `src/db/migrations.rs`)
  31. let parent = migration_path.parent().unwrap();
  32. let skip_path = parent.to_str().unwrap_or_default().len();
  33. let dest_path = parent.join("migrations.rs");
  34. let mut out_file = File::create(&dest_path).expect("Failed to create migrations.rs");
  35. let skip_name = migration_path.to_str().unwrap_or_default().len();
  36. writeln!(out_file, "/// @generated").unwrap();
  37. writeln!(out_file, "/// Auto-generated by build.rs").unwrap();
  38. writeln!(
  39. out_file,
  40. "pub static MIGRATIONS: &[(&str, &str, &str)] = &["
  41. )
  42. .unwrap();
  43. for path in &files {
  44. let parts = path.to_str().unwrap().replace("\\", "/")[skip_name + 1..]
  45. .split("/")
  46. .map(|x| x.to_owned())
  47. .collect::<Vec<_>>();
  48. let prefix = if parts.len() == 2 {
  49. parts.first().map(|x| x.to_owned()).unwrap_or_default()
  50. } else {
  51. "".to_owned()
  52. };
  53. let rel_name = &path.file_name().unwrap().to_str().unwrap();
  54. let rel_path = &path.to_str().unwrap().replace("\\", "/")[skip_path..]; // for Windows
  55. writeln!(
  56. out_file,
  57. " (\"{prefix}\", \"{rel_name}\", include_str!(r#\".{rel_path}\"#)),"
  58. )
  59. .unwrap();
  60. println!("cargo:rerun-if-changed={}", path.display());
  61. }
  62. writeln!(out_file, "];").unwrap();
  63. println!("cargo:rerun-if-changed={}", migration_path.display());
  64. }
  65. }
  66. fn find_migrations_dirs(root: &Path) -> Vec<PathBuf> {
  67. let mut found = Vec::new();
  68. find_migrations_dirs_rec(root, &mut found);
  69. found
  70. }
  71. fn find_migrations_dirs_rec(dir: &Path, found: &mut Vec<PathBuf>) {
  72. if let Ok(entries) = fs::read_dir(dir) {
  73. for entry in entries.flatten() {
  74. let path = entry.path();
  75. if path.is_dir() {
  76. if path.file_name().unwrap_or_default() == "migrations" {
  77. found.push(path.clone());
  78. }
  79. find_migrations_dirs_rec(&path, found);
  80. }
  81. }
  82. }
  83. }
  84. fn visit_dirs(dir: &Path, files: &mut Vec<PathBuf>) -> std::io::Result<()> {
  85. for entry in fs::read_dir(dir)? {
  86. let entry = entry?;
  87. let path = entry.path();
  88. if path.is_dir() {
  89. visit_dirs(&path, files)?;
  90. } else if path.is_file() {
  91. files.push(path);
  92. }
  93. }
  94. Ok(())
  95. }