build.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use cfg_aliases::cfg_aliases;
  2. #[cfg(any(feature = "embedded-server", feature = "no-server"))]
  3. use std::{
  4. env,
  5. error::Error,
  6. fs::{read_to_string, File},
  7. io::{BufWriter, Write},
  8. path::Path,
  9. };
  10. #[cfg(any(feature = "embedded-server", feature = "no-server"))]
  11. pub fn main() -> Result<(), Box<dyn Error>> {
  12. shared();
  13. let out_dir = env::var("OUT_DIR")?;
  14. let dest_index_html_path = Path::new(&out_dir).join("index.tauri.html");
  15. let mut index_html_file = BufWriter::new(File::create(&dest_index_html_path)?);
  16. match env::var_os("TAURI_DIST_DIR") {
  17. Some(dist_path) => {
  18. let dist_path_string = dist_path.into_string().unwrap();
  19. let dist_path = Path::new(&dist_path_string);
  20. println!("cargo:rerun-if-changed={}", dist_path_string);
  21. let mut inlined_assets = match std::env::var_os("TAURI_INLINED_ASSETS") {
  22. Some(assets) => assets
  23. .into_string()
  24. .unwrap()
  25. .split('|')
  26. .map(|s| s.to_string())
  27. .filter(|s| s != "")
  28. .collect(),
  29. None => Vec::new(),
  30. };
  31. // the index.html is parsed so we always ignore it
  32. inlined_assets.push(
  33. dist_path
  34. .join("index.html")
  35. .into_os_string()
  36. .into_string()
  37. .expect("failed to convert dist path to string"),
  38. );
  39. if cfg!(feature = "no-server") {
  40. // on no-server we include_str() the index.tauri.html on the runner
  41. inlined_assets.push(
  42. dist_path
  43. .join("index.tauri.html")
  44. .into_os_string()
  45. .into_string()
  46. .expect("failed to convert dist path to string"),
  47. );
  48. }
  49. // include assets
  50. tauri_includedir_codegen::start("ASSETS")
  51. .dir(
  52. dist_path_string.clone(),
  53. tauri_includedir_codegen::Compression::None,
  54. )
  55. .build("data.rs", inlined_assets)
  56. .expect("failed to build data.rs");
  57. let original_index_html_path = Path::new(&dist_path_string).join("index.tauri.html");
  58. let original_index_html = read_to_string(original_index_html_path)?;
  59. write!(index_html_file, "{}", original_index_html)?;
  60. }
  61. None => {
  62. // dummy assets
  63. tauri_includedir_codegen::start("ASSETS")
  64. .dir("".to_string(), tauri_includedir_codegen::Compression::None)
  65. .build("data.rs", vec![])
  66. .expect("failed to build data.rs");
  67. write!(
  68. index_html_file,
  69. "<html><body>Build error: Couldn't find ENV: TAURI_DIST_DIR</body></html>"
  70. )?;
  71. println!("Build error: Couldn't find ENV: TAURI_DIST_DIR");
  72. }
  73. }
  74. Ok(())
  75. }
  76. #[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
  77. pub fn main() {
  78. shared();
  79. }
  80. fn shared() {
  81. setup_env_aliases();
  82. }
  83. fn setup_env_aliases() {
  84. cfg_aliases! {
  85. embedded_server: { feature = "embedded-server" },
  86. no_server: { feature = "no-server" },
  87. assets: { any(feature = "embedded-server", feature = "no-server") },
  88. dev: { not(any(feature = "embedded-server", feature = "no-server")) },
  89. all_api: { feature = "all-api" },
  90. // fs
  91. read_text_file: { any(all_api, feature = "read-text-file") },
  92. read_binary_file: { any(all_api, feature = "read-binary-file") },
  93. write_file: { any(all_api, feature = "write-file") },
  94. write_binary_file: { any(all_api, feature = "write-binary-file") },
  95. read_dir: { any(all_api, feature = "read-dir") },
  96. copy_file: { any(all_api, feature = "copy-file") },
  97. create_dir: { any(all_api, feature = "create_dir") },
  98. remove_dir: { any(all_api, feature = "remove-dir") },
  99. remove_file: { any(all_api, feature = "remove-file") },
  100. rename_file: { any(all_api, feature = "rename-file") },
  101. // window
  102. set_title: { any(all_api, feature = "set-title") },
  103. open: { any(all_api, feature = "open") },
  104. // process
  105. execute: { any(all_api, feature = "execute") },
  106. // event
  107. event: { any(all_api, feature = "event") },
  108. // dialog
  109. open_dialog: { any(all_api, feature = "open-dialog") },
  110. save_dialog: { any(all_api, feature = "save-dialog") },
  111. // http
  112. http_request: { any(all_api, feature = "http-request") },
  113. // cli
  114. cli: { feature = "cli" },
  115. // notification
  116. notification: { any(all_api, feature = "notification") },
  117. }
  118. }