Prechádzať zdrojové kódy

warn the user if they try to launch without a renderer selected

Evan Almloff 1 rok pred
rodič
commit
f42bc61288
2 zmenil súbory, kde vykonal 56 pridanie a 2 odobranie
  1. 47 0
      packages/dioxus/build.rs
  2. 9 2
      packages/dioxus/src/launch.rs

+ 47 - 0
packages/dioxus/build.rs

@@ -0,0 +1,47 @@
+fn main() {
+	// Warn the user if they enabled the launch feature without any renderers
+    if feature_enabled("launch") {
+		let liveview_renderers = [
+			"liveview",
+			"axum",
+			"salvo",
+			"warp",
+			"rocket",
+		];
+		let fullstack_renderers = [
+			"axum",
+			"salvo",
+			"warp",
+		];
+		let client_renderers = [
+			"desktop",
+			"mobile",
+			"web",
+			"tui",
+		];
+		let client_renderer_selected = client_renderers.iter().any(|renderer| feature_enabled(renderer));
+		if feature_enabled("fullstack") {
+			let server_fullstack_enabled = fullstack_renderers.iter().any(|renderer| feature_enabled(renderer));
+			if !server_fullstack_enabled && !client_renderer_selected {
+				println!("cargo:warning=You have enabled the launch and fullstack features, but have not enabled any renderers. The application will not be able to launch. Try enabling one of the following renderers: {} for the server or one of the following renderers: {} for the client.", fullstack_renderers.join(", "), client_renderers.join(", "));
+			}
+		}
+
+		if feature_enabled("liveview") {
+			let server_selected = liveview_renderers.iter().any(|renderer| feature_enabled(renderer));
+			if !server_selected {
+				println!("cargo:warning=You have enabled the launch and liveview features, but have not enabled any liveview renderers. The application will not be able to launch. Try enabling one of the following renderers: {}", liveview_renderers.join(", "));
+			}
+		}
+
+		if !client_renderer_selected {
+			println!("cargo:warning=You have enabled the launch feature, but have not enabled any client renderers. The application will not be able to launch. Try enabling one of the following renderers: {}, fullstack or liveview", client_renderers.join(", "));
+		}
+    }
+}
+
+fn feature_enabled(feature: &str) -> bool {
+	let feature = "CARGO_FEATURE_".to_owned() + &feature.to_uppercase().replace("-", "_");
+	println!("cargo:rerun-if-env-changed={}", feature);
+	std::env::var(feature).is_ok()
+}

+ 9 - 2
packages/dioxus/src/launch.rs

@@ -161,15 +161,18 @@ mod current_platform {
     #[cfg(all(feature = "desktop", not(feature = "fullstack")))]
     pub use dioxus_desktop::launch::*;
 
+    #[cfg(all(feature = "mobile", not(feature = "fullstack")))]
+    pub use dioxus_desktop::launch::*;
+
     #[cfg(feature = "fullstack")]
     pub use dioxus_fullstack::launch::*;
 
-    #[cfg(all(feature = "web", not(any(feature = "desktop", feature = "fullstack"))))]
+    #[cfg(all(feature = "web", not(any(feature = "desktop", feature = "mobile", feature = "fullstack"))))]
     pub use dioxus_web::launch::*;
 
     #[cfg(all(
         feature = "liveview",
-        not(any(feature = "web", feature = "desktop", feature = "fullstack"))
+        not(any(feature = "web", feature = "desktop", feature = "mobile", feature = "fullstack"))
     ))]
     pub use dioxus_liveview::launch::*;
 
@@ -179,6 +182,7 @@ mod current_platform {
             feature = "liveview",
             feature = "web",
             feature = "desktop",
+            feature = "mobile",
             feature = "fullstack"
         ))
     ))]
@@ -187,6 +191,7 @@ mod current_platform {
     #[cfg(not(any(
         feature = "liveview",
         feature = "desktop",
+        feature = "mobile",
         feature = "web",
         feature = "tui",
         feature = "fullstack"
@@ -196,6 +201,7 @@ mod current_platform {
     #[cfg(not(any(
         feature = "liveview",
         feature = "desktop",
+        feature = "mobile",
         feature = "web",
         feature = "tui",
         feature = "fullstack"
@@ -205,6 +211,7 @@ mod current_platform {
         contexts: Vec<Box<super::ValidContext>>,
         platform_config: (),
     ) {
+        panic!("No platform feature enabled. Please enable one of the following features: liveview, desktop, mobile, web, tui, fullstack to use the launch API.");
     }
 }