|
5 | 5 | #![cfg_attr(doc_cfg, feature(doc_cfg))] |
6 | 6 |
|
7 | 7 | pub use anyhow::Result; |
| 8 | +use heck::ToSnakeCase; |
8 | 9 | use tauri_utils::resources::{external_binaries, resource_relpath, ResourcePaths}; |
9 | 10 |
|
10 | 11 | use std::path::{Path, PathBuf}; |
@@ -61,6 +62,26 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> { |
61 | 62 | Ok(()) |
62 | 63 | } |
63 | 64 |
|
| 65 | +// checks if the given Cargo feature is enabled. |
| 66 | +fn has_feature(feature: &str) -> bool { |
| 67 | + // when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1 |
| 68 | + // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts |
| 69 | + std::env::var(format!( |
| 70 | + "CARGO_FEATURE_{}", |
| 71 | + feature.to_snake_case().to_uppercase() |
| 72 | + )) |
| 73 | + .map(|x| x == "1") |
| 74 | + .unwrap_or(false) |
| 75 | +} |
| 76 | + |
| 77 | +// creates a cfg alias if `has_feature` is true. |
| 78 | +// `alias` must be a snake case string. |
| 79 | +fn cfg_alias(alias: &str, has_feature: bool) { |
| 80 | + if has_feature { |
| 81 | + println!("cargo:rustc-cfg={}", alias); |
| 82 | + } |
| 83 | +} |
| 84 | + |
64 | 85 | /// Attributes used on Windows. |
65 | 86 | #[allow(dead_code)] |
66 | 87 | #[derive(Debug, Default)] |
@@ -163,6 +184,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> { |
163 | 184 | )?)? |
164 | 185 | }; |
165 | 186 |
|
| 187 | + cfg_alias("dev", !has_feature("custom-protocol")); |
| 188 | + |
166 | 189 | let mut manifest = Manifest::from_path("Cargo.toml")?; |
167 | 190 | if let Some(tauri) = manifest.dependencies.remove("tauri") { |
168 | 191 | let features = match tauri { |
|
0 commit comments