Skip to content

Commit b9e6a01

Browse files
refactor(cli): use tauri/custom-protocol instead of relying on user having custom-protocol in their cargo features (#8937)
* refactor(cli): use `tauri/custom-protocol` instead of relying on user having `custom-protocol` in their cargo features * tauri-build dev cfg * pass build-feature when building apk * run beforeBuildCommand before first build for plugins * clippy * fix * mut * enhance dev/prod checks * lint [skip ci] --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio> Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 60bf11a commit b9e6a01

File tree

31 files changed

+260
-263
lines changed

31 files changed

+260
-263
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@tauri-apps/cli": patch:breaking
3+
"tauri-cli": patch:breaking
4+
"tauri": patch:breaking
5+
"tauri-build": patch:breaking
6+
---
7+
8+
The `custom-protocol` Cargo feature is no longer required on your application and is now ignored. To check if running on production, use `#[cfg(not(dev))]` instead of `#[cfg(feature = "custom-protocol")]`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch:breaking
3+
---
4+
5+
Removed `tauri_build::CodegenContext::dev()` and added `tauri_build::dev()`.

core/tauri-build/src/codegen/context.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use tauri_utils::config::FrontendDist;
1717
#[cfg_attr(docsrs, doc(cfg(feature = "codegen")))]
1818
#[derive(Debug)]
1919
pub struct CodegenContext {
20-
dev: bool,
2120
config_path: PathBuf,
2221
out_file: PathBuf,
2322
capabilities: Option<Vec<PathBuf>>,
@@ -26,7 +25,6 @@ pub struct CodegenContext {
2625
impl Default for CodegenContext {
2726
fn default() -> Self {
2827
Self {
29-
dev: false,
3028
config_path: PathBuf::from("tauri.conf.json"),
3129
out_file: PathBuf::from("tauri-build-context.rs"),
3230
capabilities: None,
@@ -68,14 +66,6 @@ impl CodegenContext {
6866
self
6967
}
7068

71-
/// Run the codegen in a `dev` context, meaning that Tauri is using a dev server or local file for development purposes,
72-
/// usually with the `tauri dev` CLI command.
73-
#[must_use]
74-
pub fn dev(mut self) -> Self {
75-
self.dev = true;
76-
self
77-
}
78-
7969
/// Adds a capability file to the generated context.
8070
#[must_use]
8171
pub fn capability<P: AsRef<Path>>(mut self, path: P) -> Self {
@@ -131,7 +121,7 @@ impl CodegenContext {
131121
);
132122

133123
let code = context_codegen(ContextData {
134-
dev: self.dev,
124+
dev: crate::dev(),
135125
config,
136126
config_parent,
137127
// it's very hard to have a build script for unit tests, so assume this is always called from

core/tauri-build/src/lib.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use anyhow::Context;
1616
pub use anyhow::Result;
1717
use cargo_toml::Manifest;
18-
use heck::AsShoutySnakeCase;
1918

2019
use tauri_utils::{
2120
acl::build::parse_capabilities,
@@ -207,15 +206,6 @@ fn copy_frameworks(dest_dir: &Path, frameworks: &[String]) -> Result<()> {
207206
Ok(())
208207
}
209208

210-
// checks if the given Cargo feature is enabled.
211-
fn has_feature(feature: &str) -> bool {
212-
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
213-
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
214-
std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
215-
.map(|x| x == "1")
216-
.unwrap_or(false)
217-
}
218-
219209
// creates a cfg alias if `has_feature` is true.
220210
// `alias` must be a snake case string.
221211
fn cfg_alias(alias: &str, has_feature: bool) {
@@ -419,6 +409,12 @@ impl Attributes {
419409
}
420410
}
421411

412+
pub fn dev() -> bool {
413+
std::env::var("DEP_TAURI_DEV")
414+
.expect("missing `cargo:dev` instruction, please update tauri to latest")
415+
== "true"
416+
}
417+
422418
/// Run all build time helpers for your Tauri Application.
423419
///
424420
/// The current helpers include the following:
@@ -499,7 +495,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
499495
mobile::generate_gradle_files(project_dir)?;
500496
}
501497

502-
cfg_alias("dev", !has_feature("custom-protocol"));
498+
cfg_alias("dev", dev());
503499

504500
let ws_path = get_workspace_dir()?;
505501
let mut manifest =

core/tauri/build.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ fn alias(alias: &str, has_feature: bool) {
205205
}
206206

207207
fn main() {
208-
alias("custom_protocol", has_feature("custom-protocol"));
209-
alias("dev", !has_feature("custom-protocol"));
208+
let custom_protocol = has_feature("custom-protocol");
209+
let dev = !custom_protocol;
210+
alias("custom_protocol", custom_protocol);
211+
alias("dev", dev);
212+
213+
println!("cargo:dev={}", dev);
210214

211215
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
212216
let mobile = target_os == "ios" || target_os == "android";

0 commit comments

Comments
 (0)