build.rs 4.1 KB

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