Skip to content

Commit

Permalink
refactor(cli): use tauri/custom-protocol instead of relying on user…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
3 people committed Feb 26, 2024
1 parent 60bf11a commit b9e6a01
Show file tree
Hide file tree
Showing 31 changed files with 260 additions and 263 deletions.
8 changes: 8 additions & 0 deletions .changes/remove-app-custom-protocol-feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@tauri-apps/cli": patch:breaking
"tauri-cli": patch:breaking
"tauri": patch:breaking
"tauri-build": patch:breaking
---

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")]`.
5 changes: 5 additions & 0 deletions .changes/tauri-build-dev-changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-build": patch:breaking
---

Removed `tauri_build::CodegenContext::dev()` and added `tauri_build::dev()`.
12 changes: 1 addition & 11 deletions core/tauri-build/src/codegen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use tauri_utils::config::FrontendDist;
#[cfg_attr(docsrs, doc(cfg(feature = "codegen")))]
#[derive(Debug)]
pub struct CodegenContext {
dev: bool,
config_path: PathBuf,
out_file: PathBuf,
capabilities: Option<Vec<PathBuf>>,
Expand All @@ -26,7 +25,6 @@ pub struct CodegenContext {
impl Default for CodegenContext {
fn default() -> Self {
Self {
dev: false,
config_path: PathBuf::from("tauri.conf.json"),
out_file: PathBuf::from("tauri-build-context.rs"),
capabilities: None,
Expand Down Expand Up @@ -68,14 +66,6 @@ impl CodegenContext {
self
}

/// Run the codegen in a `dev` context, meaning that Tauri is using a dev server or local file for development purposes,
/// usually with the `tauri dev` CLI command.
#[must_use]
pub fn dev(mut self) -> Self {
self.dev = true;
self
}

/// Adds a capability file to the generated context.
#[must_use]
pub fn capability<P: AsRef<Path>>(mut self, path: P) -> Self {
Expand Down Expand Up @@ -131,7 +121,7 @@ impl CodegenContext {
);

let code = context_codegen(ContextData {
dev: self.dev,
dev: crate::dev(),
config,
config_parent,
// it's very hard to have a build script for unit tests, so assume this is always called from
Expand Down
18 changes: 7 additions & 11 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use anyhow::Context;
pub use anyhow::Result;
use cargo_toml::Manifest;
use heck::AsShoutySnakeCase;

use tauri_utils::{
acl::build::parse_capabilities,
Expand Down Expand Up @@ -207,15 +206,6 @@ fn copy_frameworks(dest_dir: &Path, frameworks: &[String]) -> 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_{}", AsShoutySnakeCase(feature)))
.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) {
Expand Down Expand Up @@ -419,6 +409,12 @@ impl Attributes {
}
}

pub fn dev() -> bool {
std::env::var("DEP_TAURI_DEV")
.expect("missing `cargo:dev` instruction, please update tauri to latest")
== "true"
}

/// Run all build time helpers for your Tauri Application.
///
/// The current helpers include the following:
Expand Down Expand Up @@ -499,7 +495,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
mobile::generate_gradle_files(project_dir)?;
}

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

let ws_path = get_workspace_dir()?;
let mut manifest =
Expand Down
8 changes: 6 additions & 2 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,12 @@ fn alias(alias: &str, has_feature: bool) {
}

fn main() {
alias("custom_protocol", has_feature("custom-protocol"));
alias("dev", !has_feature("custom-protocol"));
let custom_protocol = has_feature("custom-protocol");
let dev = !custom_protocol;
alias("custom_protocol", custom_protocol);
alias("dev", dev);

println!("cargo:dev={}", dev);

let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
let mobile = target_os == "ios" || target_os == "android";
Expand Down
Loading

0 comments on commit b9e6a01

Please sign in to comment.