Skip to content

Commit

Permalink
feat(build): create dev alias (#4212)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 25, 2022
1 parent ac5b2d6 commit 9cdcf9b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/dev-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-build": patch
---

Create `dev` cfg alias.
1 change: 1 addition & 0 deletions core/tauri-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tauri-codegen = { version = "1.0.0-rc.7", path = "../tauri-codegen", optional =
tauri-utils = { version = "1.0.0-rc.7", path = "../tauri-utils", features = [ "build", "resources" ] }
cargo_toml = "0.11"
serde_json = "1"
heck = "0.4"

[target."cfg(windows)".dependencies]
winres = "0.1"
Expand Down
23 changes: 23 additions & 0 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]

pub use anyhow::Result;
use heck::ToSnakeCase;
use tauri_utils::resources::{external_binaries, resource_relpath, ResourcePaths};

use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -61,6 +62,26 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {
Ok(())
}

// checks if the given Cargo feature is enabled.
fn has_feature(feature: &str) -> bool {
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
std::env::var(format!(
"CARGO_FEATURE_{}",
feature.to_snake_case().to_uppercase()
))
.map(|x| x == "1")
.unwrap_or(false)
}

// creates a cfg alias if `has_feature` is true.
// `alias` must be a snake case string.
fn cfg_alias(alias: &str, has_feature: bool) {
if has_feature {
println!("cargo:rustc-cfg={}", alias);
}
}

/// Attributes used on Windows.
#[allow(dead_code)]
#[derive(Debug, Default)]
Expand Down Expand Up @@ -163,6 +184,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
)?)?
};

cfg_alias("dev", !has_feature("custom-protocol"));

let mut manifest = Manifest::from_path("Cargo.toml")?;
if let Some(tauri) = manifest.dependencies.remove("tauri") {
let features = match tauri {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl<R: Runtime> WindowManager<R> {
///
/// * In dev mode, this will be based on the `devPath` configuration value.
/// * Otherwise, this will be based on the `distDir` configuration value.
#[cfg(custom_protocol)]
#[cfg(not(dev))]
fn base_path(&self) -> &AppUrl {
&self.inner.config.build.dist_dir
}
Expand Down
3 changes: 2 additions & 1 deletion examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9cdcf9b

Please sign in to comment.