build.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #[macro_use]
  2. extern crate serde_derive;
  3. extern crate serde_json;
  4. use std::env;
  5. use std::io::Write;
  6. #[path = "src/config.rs"]
  7. mod config;
  8. #[cfg(not(feature = "dev-server"))]
  9. extern crate tauri_includedir_codegen;
  10. #[cfg(feature = "embedded-server")]
  11. mod tcp;
  12. fn main() {
  13. let out_dir = env::var("OUT_DIR").unwrap();
  14. let dest_path = std::path::Path::new(&out_dir).join("tauri_src");
  15. let mut file = std::fs::File::create(&dest_path).unwrap();
  16. let tauri_src: String;
  17. let config = config::get();
  18. #[cfg(not(any(feature = "embedded-server", feature = "no-server")))]
  19. {
  20. tauri_src = if config.dev_path.starts_with("http") {
  21. config.dev_path
  22. } else {
  23. let dev_path = std::path::Path::new(&config.dev_path).join("index.tauri.html");
  24. println!("{}", format!("cargo:rerun-if-changed={:?}", dev_path));
  25. std::fs::read_to_string(dev_path).unwrap()
  26. };
  27. }
  28. #[cfg(not(feature = "dev-server"))]
  29. {
  30. match env::var("TAURI_DIST_DIR") {
  31. Ok(dist_path) => {
  32. // include assets
  33. tauri_includedir_codegen::start("ASSETS")
  34. .dir(dist_path, tauri_includedir_codegen::Compression::None)
  35. .build("data.rs", config.inlined_assets)
  36. .unwrap()
  37. }
  38. Err(_e) => panic!("Build error: Couldn't find ENV: {}", _e),
  39. }
  40. }
  41. #[cfg(feature = "embedded-server")]
  42. {
  43. // define URL
  44. let port;
  45. let port_valid;
  46. if config.embedded_server.port == "random" {
  47. match tcp::get_available_port() {
  48. Some(available_port) => {
  49. port = available_port.to_string();
  50. port_valid = true;
  51. }
  52. None => {
  53. port = "0".to_string();
  54. port_valid = false;
  55. }
  56. }
  57. } else {
  58. port = config.embedded_server.port;
  59. port_valid = crate::tcp::port_is_available(
  60. port
  61. .parse::<u16>()
  62. .expect(&format!("Invalid port {}", port)),
  63. );
  64. }
  65. if port_valid {
  66. let mut url = format!("{}:{}", config.embedded_server.host, port);
  67. if !url.starts_with("http") {
  68. url = format!("http://{}", url);
  69. }
  70. tauri_src = url.to_string();
  71. } else {
  72. panic!(format!("Port {} is not valid or not open", port));
  73. }
  74. }
  75. #[cfg(feature = "no-server")]
  76. {
  77. let index_path = std::path::Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html");
  78. println!("{}", format!("cargo:rerun-if-changed={:?}", index_path));
  79. tauri_src = std::fs::read_to_string(index_path).unwrap();
  80. }
  81. file.write_all(tauri_src.as_bytes()).unwrap();
  82. }