Skip to content

Commit

Permalink
fix(cli): prevent creating interface twice avoiding double manifest r…
Browse files Browse the repository at this point in the history
…ewrite (#7837)
  • Loading branch information
lucasfernog authored Feb 3, 2024
1 parent 791e291 commit e691208
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 128 deletions.
6 changes: 6 additions & 0 deletions .changes/prevent-double-rewrite-manifest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:enhance
"@tauri-apps/cli": patch:enhance
---

Prevent unneeded double Cargo.toml rewrite on `dev` and `build`.
28 changes: 16 additions & 12 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
.map(Target::from_triple)
.unwrap_or_else(Target::current);

let mut interface = setup(target, &mut options, false)?;

let config = get_config(target, options.config.as_deref())?;

let mut interface = AppInterface::new(
config.lock().unwrap().as_ref().unwrap(),
options.target.clone(),
)?;

setup(target, &interface, &mut options, false)?;

let config_guard = config.lock().unwrap();
let config_ = config_guard.as_ref().unwrap();

Expand Down Expand Up @@ -228,7 +234,12 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
Ok(())
}

pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppInterface> {
pub fn setup(
target: Target,
interface: &AppInterface,
options: &mut Options,
mobile: bool,
) -> Result<()> {
let (merge_config, merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

Expand All @@ -240,8 +251,6 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
let config_guard = config.lock().unwrap();
let config_ = config_guard.as_ref().unwrap();

let interface = AppInterface::new(config_, options.target.clone())?;

let bundle_identifier_source = match config_.find_bundle_identifier_overwriter() {
Some(source) if source == MERGE_CONFIG_EXTENSION_NAME => merge_config_path.unwrap_or(source),
Some(source) => source,
Expand Down Expand Up @@ -272,12 +281,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
}

if let Some(before_build) = config_.build.before_build_command.clone() {
run_hook(
"beforeBuildCommand",
before_build,
&interface,
options.debug,
)?;
run_hook("beforeBuildCommand", before_build, interface, options.debug)?;
}

if let Some(AppUrl::Url(WebviewUrl::App(web_asset_path))) = &config_.build.dist_dir {
Expand Down Expand Up @@ -320,7 +324,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
.extend(config_.build.features.clone().unwrap_or_default());
interface.build_options(&mut options.args, &mut options.features, mobile);

Ok(interface)
Ok(())
}

fn run_hook(name: &str, hook: HookCommand, interface: &AppInterface, debug: bool) -> Result<()> {
Expand Down
25 changes: 17 additions & 8 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ fn command_internal(mut options: Options) -> Result<()> {
.as_deref()
.map(Target::from_triple)
.unwrap_or_else(Target::current);
let mut interface = setup(target, &mut options, false)?;

let config = get_config(target, options.config.as_deref())?;

let mut interface = AppInterface::new(
config.lock().unwrap().as_ref().unwrap(),
options.target.clone(),
)?;

setup(target, &interface, &mut options, false)?;

let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
interface.dev(options.into(), move |status, reason| {
Expand Down Expand Up @@ -150,7 +159,12 @@ pub fn local_ip_address(force: bool) -> &'static IpAddr {
})
}

pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppInterface> {
pub fn setup(
target: Target,
interface: &AppInterface,
options: &mut Options,
mobile: bool,
) -> Result<()> {
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

Expand All @@ -159,11 +173,6 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

let interface = AppInterface::new(
config.lock().unwrap().as_ref().unwrap(),
options.target.clone(),
)?;

let mut dev_path = config
.lock()
.unwrap()
Expand Down Expand Up @@ -397,7 +406,7 @@ pub fn setup(target: Target, options: &mut Options, mobile: bool) -> Result<AppI
}
}

Ok(interface)
Ok(())
}

pub fn wait_dev_process<
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
process::ExitStatus,
sync::Arc,
};

use crate::helpers::config::Config;
Expand Down Expand Up @@ -87,7 +88,7 @@ pub trait Interface: Sized {
type AppSettings: AppSettings;

fn new(config: &Config, target: Option<String>) -> crate::Result<Self>;
fn app_settings(&self) -> &Self::AppSettings;
fn app_settings(&self) -> Arc<Self::AppSettings>;
fn env(&self) -> HashMap<&str, String>;
fn build(&mut self, options: Options) -> crate::Result<()>;
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
Expand Down
24 changes: 15 additions & 9 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub struct RustupTarget {
}

pub struct Rust {
app_settings: RustAppSettings,
app_settings: Arc<RustAppSettings>,
config_features: Vec<String>,
product_name: Option<String>,
available_targets: Option<Vec<RustupTarget>>,
Expand Down Expand Up @@ -138,15 +138,15 @@ impl Interface for Rust {
let app_settings = RustAppSettings::new(config, manifest, target)?;

Ok(Self {
app_settings,
app_settings: Arc::new(app_settings),
config_features: config.build.features.clone().unwrap_or_default(),
product_name: config.package.product_name.clone(),
available_targets: None,
})
}

fn app_settings(&self) -> &Self::AppSettings {
&self.app_settings
fn app_settings(&self) -> Arc<Self::AppSettings> {
self.app_settings.clone()
}

fn build(&mut self, options: Options) -> crate::Result<()> {
Expand Down Expand Up @@ -350,6 +350,8 @@ fn shared_options(
args.push("--bins".into());
let all_features = app_settings
.manifest
.lock()
.unwrap()
.all_enabled_features(if let Some(f) = features { f } else { &[] });
if !all_features.contains(&"tauri/rustls-tls".into()) {
features
Expand Down Expand Up @@ -382,7 +384,7 @@ fn dev_options(
shared_options(mobile, args, features, app_settings);

if !args.contains(&"--no-default-features".into()) {
let manifest_features = app_settings.manifest.features();
let manifest_features = app_settings.manifest.lock().unwrap().features();
let enable_features: Vec<String> = manifest_features
.get("default")
.cloned()
Expand Down Expand Up @@ -504,7 +506,7 @@ impl Rust {
match reload_config(config.as_deref()) {
Ok(config) => {
info!("Tauri configuration changed. Rewriting manifest...");
self.app_settings.manifest =
*self.app_settings.manifest.lock().unwrap() =
rewrite_manifest(config.lock().unwrap().as_ref().unwrap())?
}
Err(err) => {
Expand Down Expand Up @@ -672,7 +674,7 @@ impl CargoSettings {
}

pub struct RustAppSettings {
manifest: Manifest,
manifest: Mutex<Manifest>,
cargo_settings: CargoSettings,
cargo_package_settings: CargoPackageSettings,
package_settings: PackageSettings,
Expand Down Expand Up @@ -702,7 +704,7 @@ impl AppSettings for RustAppSettings {
self.target_triple.starts_with("x86_64") || self.target_triple.starts_with("aarch64");

let mut settings = tauri_config_to_bundle_settings(
&self.manifest,
&self.manifest.lock().unwrap(),
features,
config.tauri.bundle.clone(),
arch64bits,
Expand Down Expand Up @@ -855,6 +857,8 @@ impl AppSettings for RustAppSettings {
fn app_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("package")
Expand All @@ -867,6 +871,8 @@ impl AppSettings for RustAppSettings {
fn lib_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("lib")
Expand Down Expand Up @@ -991,7 +997,7 @@ impl RustAppSettings {
let target = Target::from_triple(&target_triple);

Ok(Self {
manifest,
manifest: Mutex::new(manifest),
cargo_settings,
cargo_package_settings,
package_settings,
Expand Down
12 changes: 10 additions & 2 deletions tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// SPDX-License-Identifier: MIT

use super::{detect_target_ok, ensure_init, env, get_app, get_config, read_options, MobileTarget};
use crate::{helpers::config::get as get_tauri_config, Result};
use crate::{
helpers::config::get as get_tauri_config,
interface::{AppInterface, Interface},
Result,
};
use clap::{ArgAction, Parser};

use cargo_mobile2::{
Expand Down Expand Up @@ -42,7 +46,11 @@ pub fn command(options: Options) -> Result<()> {
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
let cli_options = read_options(&tauri_config_.tauri.bundle.identifier);
let (config, metadata) = get_config(&get_app(tauri_config_), tauri_config_, &cli_options);
let (config, metadata) = get_config(
&get_app(tauri_config_, &AppInterface::new(tauri_config_, None)?),
tauri_config_,
&cli_options,
);
(config, metadata, cli_options)
};
ensure_init(config.project_dir(), MobileTarget::Android)?;
Expand Down
36 changes: 23 additions & 13 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
config::{get as get_tauri_config, ConfigHandle},
flock, resolve_merge_config,
},
interface::{AppSettings, Interface, Options as InterfaceOptions},
interface::{AppInterface, AppSettings, Interface, Options as InterfaceOptions},
mobile::{write_options, CliOptions},
Result,
};
Expand Down Expand Up @@ -87,16 +87,28 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

let mut build_options: BuildOptions = options.clone().into();
build_options.target = Some(
Target::all()
.get(Target::DEFAULT_KEY)
.unwrap()
.triple
.into(),
);

let tauri_config = get_tauri_config(
tauri_utils::platform::Target::Android,
options.config.as_deref(),
)?;
let (app, config, metadata) = {
let (interface, app, config, metadata) = {
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
let app = get_app(tauri_config_);

let interface = AppInterface::new(tauri_config_, build_options.target.clone())?;

let app = get_app(tauri_config_, &interface);
let (config, metadata) = get_config(&app, tauri_config_, &Default::default());
(app, config, metadata)
(interface, app, config, metadata)
};

set_var("WRY_RUSTWEBVIEWCLIENT_CLASS_EXTENSION", "");
Expand Down Expand Up @@ -132,7 +144,9 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {

let open = options.open;
let _handle = run_build(
interface,
options,
build_options,
tauri_config,
profile,
&config,
Expand All @@ -147,8 +161,11 @@ pub fn command(mut options: Options, noise_level: NoiseLevel) -> Result<()> {
Ok(())
}

#[allow(clippy::too_many_arguments)]
fn run_build(
interface: AppInterface,
mut options: Options,
mut build_options: BuildOptions,
tauri_config: ConfigHandle,
profile: Profile,
config: &AndroidConfig,
Expand All @@ -161,16 +178,9 @@ fn run_build(
options.aab = true;
}

let mut build_options: BuildOptions = options.clone().into();
build_options.target = Some(
Target::all()
.get(Target::DEFAULT_KEY)
.unwrap()
.triple
.into(),
);
let interface = crate::build::setup(
crate::build::setup(
tauri_utils::platform::Target::Android,
&interface,
&mut build_options,
true,
)?;
Expand Down
Loading

0 comments on commit e691208

Please sign in to comment.