build.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #[cfg(any(feature = "embedded-server", feature = "no-server"))]
  2. use std::{
  3. env,
  4. error::Error,
  5. fs::{read_to_string, File},
  6. io::{BufWriter, Write},
  7. path::Path,
  8. };
  9. #[cfg(any(feature = "embedded-server", feature = "no-server"))]
  10. pub fn main() -> Result<(), Box<dyn Error>> {
  11. shared();
  12. let out_dir = env::var("OUT_DIR")?;
  13. let dest_index_html_path = Path::new(&out_dir).join("index.tauri.html");
  14. let mut index_html_file = BufWriter::new(File::create(&dest_index_html_path)?);
  15. match env::var_os("TAURI_DIST_DIR") {
  16. Some(dist_path) => {
  17. let dist_path_string = dist_path.into_string().unwrap();
  18. println!("cargo:rerun-if-changed={}", dist_path_string);
  19. let inlined_assets = match std::env::var_os("TAURI_INLINED_ASSETS") {
  20. Some(assets) => assets
  21. .into_string()
  22. .unwrap()
  23. .split('|')
  24. .map(|s| s.to_string())
  25. .collect(),
  26. None => Vec::new(),
  27. };
  28. // include assets
  29. tauri_includedir_codegen::start("ASSETS")
  30. .dir(
  31. dist_path_string.clone(),
  32. tauri_includedir_codegen::Compression::None,
  33. )
  34. .build("data.rs", inlined_assets)
  35. .expect("failed to build data.rs");
  36. let original_index_html_path = Path::new(&dist_path_string).join("index.tauri.html");
  37. let original_index_html = read_to_string(original_index_html_path)?;
  38. write!(index_html_file, "{}", original_index_html)?;
  39. }
  40. None => {
  41. // dummy assets
  42. tauri_includedir_codegen::start("ASSETS")
  43. .dir("".to_string(), tauri_includedir_codegen::Compression::None)
  44. .build("data.rs", vec![])
  45. .expect("failed to build data.rs");
  46. write!(
  47. index_html_file,
  48. "<html><body>Build error: Couldn't find ENV: TAURI_DIST_DIR</body></html>"
  49. )?;
  50. println!("Build error: Couldn't find ENV: TAURI_DIST_DIR");
  51. }
  52. }
  53. Ok(())
  54. }
  55. #[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
  56. pub fn main() {
  57. shared();
  58. }
  59. fn shared() {
  60. if let Some(tauri_dir) = std::env::var_os("TAURI_DIR") {
  61. let mut tauri_path = std::path::PathBuf::from(tauri_dir);
  62. tauri_path.push("tauri.conf.json");
  63. println!("cargo:rerun-if-changed={:?}", tauri_path);
  64. }
  65. }