refactor(cli): remove mutex on config (#14791)

* refactor(cli): remove mutex on config

* Fix ios

* Clippy

* Fix ios

* Unused import

* Fix ios closure and clippy

* Import `ConfigMetadata`

* Remove life time tags
This commit is contained in:
Tony
2026-01-18 21:09:36 +08:00
committed by GitHub
parent 10a8066db3
commit 08e35fcda0
8 changed files with 57 additions and 67 deletions

View File

@@ -113,22 +113,20 @@ fn command_internal(mut options: Options, dirs: Dirs) -> Result<()> {
.map(Target::from_triple)
.unwrap_or_else(Target::current);
let mut cfg = get_config(
let mut config = get_config(
target,
&options.config.iter().map(|c| &c.0).collect::<Vec<_>>(),
dirs.tauri,
)?;
let mut interface = AppInterface::new(&cfg, options.target.clone(), dirs.tauri)?;
let mut interface = AppInterface::new(&config, options.target.clone(), dirs.tauri)?;
setup(&interface, &mut options, &mut cfg, &dirs)?;
let config = Mutex::new(cfg);
setup(&interface, &mut options, &mut config, &dirs)?;
let exit_on_panic = options.exit_on_panic;
let no_watch = options.no_watch;
interface.dev(
&config,
&mut config,
options.into(),
move |status, reason| on_app_exit(status, reason, exit_on_panic, no_watch),
&dirs,

View File

@@ -9,7 +9,6 @@ use std::{
path::{Path, PathBuf},
process::ExitStatus,
sync::Arc,
sync::Mutex,
};
use crate::{
@@ -115,21 +114,23 @@ pub trait Interface: Sized {
fn build(&mut self, options: Options, dirs: &Dirs) -> crate::Result<PathBuf>;
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
options: Options,
on_exit: F,
dirs: &Dirs,
) -> crate::Result<()>;
fn mobile_dev<R: Fn(MobileOptions) -> crate::Result<Box<dyn DevProcess + Send>>>(
fn mobile_dev<
R: Fn(MobileOptions, &ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>,
>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
options: MobileOptions,
runner: R,
dirs: &Dirs,
) -> crate::Result<()>;
fn watch<R: Fn() -> crate::Result<Box<dyn DevProcess + Send>>>(
fn watch<R: Fn(&ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
options: WatcherOptions,
runner: R,
dirs: &Dirs,

View File

@@ -194,7 +194,7 @@ impl Interface for Rust {
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
mut options: Options,
on_exit: F,
dirs: &Dirs,
@@ -221,25 +221,26 @@ impl Interface for Rust {
Ok(())
} else {
let merge_configs = options.config.iter().map(|c| &c.0).collect::<Vec<_>>();
let run = Arc::new(|rust: &mut Rust| {
let on_exit = on_exit.clone();
rust.run_dev(options.clone(), run_args.clone(), move |status, reason| {
on_exit(status, reason)
})
});
self.run_dev_watcher(
config,
&options.additional_watch_folders,
&merge_configs,
run,
|rust: &mut Rust, _config| {
let on_exit = on_exit.clone();
rust.run_dev(options.clone(), run_args.clone(), move |status, reason| {
on_exit(status, reason)
})
},
dirs,
)
}
}
fn mobile_dev<R: Fn(MobileOptions) -> crate::Result<Box<dyn DevProcess + Send>>>(
fn mobile_dev<
R: Fn(MobileOptions, &ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>,
>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
mut options: MobileOptions,
runner: R,
dirs: &Dirs,
@@ -254,7 +255,7 @@ impl Interface for Rust {
);
if options.no_watch {
runner(options)?;
runner(options, config)?;
Ok(())
} else {
self.watch(
@@ -263,26 +264,25 @@ impl Interface for Rust {
config: options.config.clone(),
additional_watch_folders: options.additional_watch_folders.clone(),
},
move || runner(options.clone()),
move |config| runner(options.clone(), config),
dirs,
)
}
}
fn watch<R: Fn() -> crate::Result<Box<dyn DevProcess + Send>>>(
fn watch<R: Fn(&ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
options: WatcherOptions,
runner: R,
dirs: &Dirs,
) -> crate::Result<()> {
let merge_configs = options.config.iter().map(|c| &c.0).collect::<Vec<_>>();
let run = Arc::new(|_rust: &mut Rust| runner());
self.run_dev_watcher(
config,
&options.additional_watch_folders,
&merge_configs,
run,
|_rust: &mut Rust, config| runner(config),
dirs,
)
}
@@ -539,15 +539,17 @@ impl Rust {
.map(|c| Box::new(c) as Box<dyn DevProcess + Send>)
}
fn run_dev_watcher<F: Fn(&mut Rust) -> crate::Result<Box<dyn DevProcess + Send>>>(
fn run_dev_watcher<
F: Fn(&mut Rust, &ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>,
>(
&mut self,
config: &Mutex<ConfigMetadata>,
config: &mut ConfigMetadata,
additional_watch_folders: &[PathBuf],
merge_configs: &[&serde_json::Value],
run: Arc<F>,
run: F,
dirs: &Dirs,
) -> crate::Result<()> {
let child = run(self)?;
let child = run(self, config)?;
let process = Arc::new(Mutex::new(child));
let (tx, rx) = sync_channel(1);
@@ -593,9 +595,9 @@ impl Rust {
if let Some(event_path) = event.paths.first() {
if !ignore_matcher.is_ignore(event_path, event_path.is_dir()) {
if is_configuration_file(self.app_settings.target_platform, event_path)
&& reload_config(&mut config.lock().unwrap(), merge_configs, dirs.tauri).is_ok()
&& reload_config(config, merge_configs, dirs.tauri).is_ok()
{
let (manifest, modified) = rewrite_manifest(&config.lock().unwrap(), dirs.tauri)?;
let (manifest, modified) = rewrite_manifest(config, dirs.tauri)?;
if modified {
*self.app_settings.manifest.lock().unwrap() = manifest;
// no need to run the watcher logic, the manifest was modified
@@ -619,7 +621,7 @@ impl Rust {
break;
}
}
*p = run(self)?;
*p = run(self, config)?;
}
}
}

View File

@@ -28,7 +28,6 @@ use cargo_mobile2::{
use std::env::set_current_dir;
use std::path::Path;
use std::sync::Mutex;
#[derive(Debug, Clone, Parser)]
#[clap(
@@ -119,7 +118,7 @@ pub struct BuiltApplication {
pub fn command(options: Options, noise_level: NoiseLevel) -> Result<BuiltApplication> {
let dirs = crate::helpers::app_paths::resolve_dirs();
let tauri_config = Mutex::new(get_tauri_config(
let tauri_config = get_tauri_config(
tauri_utils::platform::Target::Android,
&options
.config
@@ -127,7 +126,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<BuiltApplica
.map(|conf| &conf.0)
.collect::<Vec<_>>(),
dirs.tauri,
)?);
)?;
run(options, noise_level, &dirs, &tauri_config)
}
@@ -135,7 +134,7 @@ pub fn run(
options: Options,
noise_level: NoiseLevel,
dirs: &Dirs,
tauri_config: &Mutex<ConfigMetadata>,
tauri_config: &ConfigMetadata,
) -> Result<BuiltApplication> {
delete_codegen_vars();
@@ -151,7 +150,6 @@ pub fn run(
)
.unwrap();
build_options.target = Some(first_target.triple.into());
let tauri_config = &tauri_config.lock().unwrap();
let (interface, config, metadata) = {
let interface = AppInterface::new(tauri_config, build_options.target.clone(), dirs.tauri)?;

View File

@@ -35,7 +35,6 @@ use cargo_mobile2::{
};
use url::Host;
use std::sync::Mutex;
use std::{env::set_current_dir, net::Ipv4Addr, path::PathBuf};
#[derive(Debug, Clone, Parser)]
@@ -256,8 +255,6 @@ fn run_dev(
crate::dev::setup(&interface, &mut dev_options, &mut tauri_config, dirs)?;
let tauri_config = Mutex::new(tauri_config);
let interface_options = InterfaceOptions {
debug: !dev_options.release_mode,
target: dev_options.target.clone(),
@@ -270,7 +267,7 @@ fn run_dev(
configure_cargo(&mut env, config)?;
generate_tauri_properties(config, &tauri_config.lock().unwrap(), true)?;
generate_tauri_properties(config, &tauri_config, true)?;
let installed_targets =
crate::interface::rust::installation::installed_targets().unwrap_or_default();
@@ -306,7 +303,7 @@ fn run_dev(
let open = options.open;
interface.mobile_dev(
&tauri_config,
&mut tauri_config,
MobileOptions {
debug: !options.release_mode,
features: options.features,
@@ -315,7 +312,7 @@ fn run_dev(
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
},
|options| {
|options, tauri_config| {
let cli_options = CliOptions {
dev: true,
features: options.features.clone(),
@@ -329,9 +326,9 @@ fn run_dev(
}),
};
let _handle = write_options(&tauri_config.lock().unwrap(), cli_options)?;
let _handle = write_options(tauri_config, cli_options)?;
inject_resources(config, &tauri_config.lock().unwrap())?;
inject_resources(config, tauri_config)?;
if open {
open_and_wait(config, &env)

View File

@@ -9,11 +9,11 @@ use cargo_mobile2::{
};
use clap::{ArgAction, Parser};
use std::path::PathBuf;
use std::sync::Mutex;
use super::{configure_cargo, device_prompt, env};
use crate::{
error::Context,
helpers::config::ConfigMetadata,
interface::{DevProcess, Interface, WatcherOptions},
mobile::{DevChild, TargetDevice},
ConfigValue, Result,
@@ -79,7 +79,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
};
let dirs = crate::helpers::app_paths::resolve_dirs();
let cfg = crate::helpers::config::get_config(
let mut tauri_config = crate::helpers::config::get_config(
tauri_utils::platform::Target::Android,
&options
.config
@@ -88,7 +88,6 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
.collect::<Vec<_>>(),
dirs.tauri,
)?;
let tauri_config = Mutex::new(cfg);
let mut built_application = super::build::run(
super::build::Options {
debug: !options.release,
@@ -125,7 +124,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
if let Some(device) = device {
let config = built_application.config.clone();
let release = options.release;
let runner = move || {
let runner = move |_tauri_config: &ConfigMetadata| {
device
.run(
&config,
@@ -150,10 +149,10 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
};
if options.no_watch {
runner()?;
runner(&tauri_config)?;
} else {
built_application.interface.watch(
&tauri_config,
&mut tauri_config,
WatcherOptions {
config: options.config,
additional_watch_folders: options.additional_watch_folders,

View File

@@ -35,7 +35,6 @@ use cargo_mobile2::{
};
use url::Host;
use std::sync::Mutex;
use std::{env::set_current_dir, net::Ipv4Addr, path::PathBuf};
const PHYSICAL_IPHONE_DEV_WARNING: &str = "To develop on physical phones you need the `--host` option (not required for Simulators). See the documentation for more information: https://v2.tauri.app/develop/#development-server";
@@ -304,8 +303,6 @@ fn run_dev(
crate::dev::setup(&interface, &mut dev_options, &mut tauri_config, &dirs)?;
let tauri_config = Mutex::new(tauri_config);
let app_settings = interface.app_settings();
let out_dir = app_settings.out_dir(
&InterfaceOptions {
@@ -321,7 +318,7 @@ fn run_dev(
let open = options.open;
interface.mobile_dev(
&tauri_config,
&mut tauri_config,
MobileOptions {
debug: true,
features: options.features,
@@ -330,7 +327,7 @@ fn run_dev(
no_watch: options.no_watch,
additional_watch_folders: options.additional_watch_folders,
},
|options| {
|options, tauri_config| {
let cli_options = CliOptions {
dev: true,
features: options.features.clone(),
@@ -340,7 +337,7 @@ fn run_dev(
config: dev_options.config.clone(),
target_device: None,
};
let _handle = write_options(&tauri_config.lock().unwrap(), cli_options)?;
let _handle = write_options(tauri_config, cli_options)?;
let open_xcode = || {
if !set_host {

View File

@@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT
use std::path::PathBuf;
use std::sync::Mutex;
use cargo_mobile2::opts::{NoiseLevel, Profile};
use clap::{ArgAction, Parser};
@@ -11,7 +10,7 @@ use clap::{ArgAction, Parser};
use super::{device_prompt, env};
use crate::{
error::Context,
helpers::config::get_config as get_tauri_config,
helpers::config::{get_config as get_tauri_config, ConfigMetadata},
interface::{DevProcess, Interface, WatcherOptions},
mobile::{DevChild, TargetDevice},
ConfigValue, Result,
@@ -98,17 +97,16 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
&dirs,
)?;
let cfg = get_tauri_config(
let mut tauri_config = get_tauri_config(
tauri_utils::platform::Target::Ios,
&options.config.iter().map(|c| &c.0).collect::<Vec<_>>(),
dirs.tauri,
)?;
let tauri_config = Mutex::new(cfg);
// options.open is handled by the build command
// so all we need to do here is run the app on the selected device
if let Some(device) = device {
let runner = move || {
let runner = move |_tauri_config: &ConfigMetadata| {
device
.run(
&built_application.config,
@@ -126,10 +124,10 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
};
if options.no_watch {
runner()?;
runner(&tauri_config)?;
} else {
built_application.interface.watch(
&tauri_config,
&mut tauri_config,
WatcherOptions {
config: options.config,
additional_watch_folders: options.additional_watch_folders,