Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/change-pr-14836.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/cli": patch:changes
"tauri-cli": patch:changes
---

Continued refactors of tauri-cli, fix too weak atomics.
2 changes: 1 addition & 1 deletion crates/tauri-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
config::{get_config, ConfigMetadata, FrontendDist},
},
info::plugins::check_mismatched_packages,
interface::{rust::get_cargo_target_dir, AppInterface, Interface},
interface::{rust::get_cargo_target_dir, AppInterface},
ConfigValue, Result,
};
use clap::{ArgAction, Parser};
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-cli/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
config::{get_config, ConfigMetadata},
updater_signature,
},
interface::{AppInterface, AppSettings, Interface},
interface::{AppInterface, AppSettings},
ConfigValue,
};

Expand Down
20 changes: 8 additions & 12 deletions crates/tauri-cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
config::{get_config, reload_config, BeforeDevCommand, ConfigMetadata, FrontendDist},
},
info::plugins::check_mismatched_packages,
interface::{AppInterface, ExitReason, Interface},
interface::{AppInterface, ExitReason},
CommandExt, ConfigValue, Error, Result,
};

Expand All @@ -25,13 +25,13 @@ use std::{
process::{exit, Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex, OnceLock,
OnceLock,
},
};

mod builtin_dev_server;

static BEFORE_DEV: OnceLock<Mutex<Arc<SharedChild>>> = OnceLock::new();
static BEFORE_DEV: OnceLock<SharedChild> = OnceLock::new();
static KILL_BEFORE_DEV_FLAG: AtomicBool = AtomicBool::new(false);

#[cfg(unix)]
Expand Down Expand Up @@ -205,21 +205,18 @@ pub fn setup(

let child = SharedChild::spawn(&mut command)
.unwrap_or_else(|_| panic!("failed to run `{before_dev}`"));
let child = Arc::new(child);
let child_ = child.clone();

let child = BEFORE_DEV.get_or_init(move || child);
std::thread::spawn(move || {
let status = child_
let status = child
.wait()
.expect("failed to wait on \"beforeDevCommand\"");
if !(status.success() || KILL_BEFORE_DEV_FLAG.load(Ordering::Relaxed)) {
if !(status.success() || KILL_BEFORE_DEV_FLAG.load(Ordering::SeqCst)) {
log::error!("The \"beforeDevCommand\" terminated with a non-zero status code.");
exit(status.code().unwrap_or(1));
}
});

BEFORE_DEV.set(Mutex::new(child)).unwrap();

let _ = ctrlc::set_handler(move || {
kill_before_dev_process();
exit(130);
Expand Down Expand Up @@ -336,11 +333,10 @@ pub fn on_app_exit(code: Option<i32>, reason: ExitReason, exit_on_panic: bool, n

pub fn kill_before_dev_process() {
if let Some(child) = BEFORE_DEV.get() {
let child = child.lock().unwrap();
if KILL_BEFORE_DEV_FLAG.load(Ordering::Relaxed) {
if KILL_BEFORE_DEV_FLAG.load(Ordering::SeqCst) {
return;
}
KILL_BEFORE_DEV_FLAG.store(true, Ordering::Relaxed);
KILL_BEFORE_DEV_FLAG.store(true, Ordering::SeqCst);
#[cfg(windows)]
{
let powershell_path = std::env::var("SYSTEMROOT").map_or_else(
Expand Down
5 changes: 1 addition & 4 deletions crates/tauri-cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ use tauri_utils::config::HookCommand;

#[cfg(not(target_os = "windows"))]
use crate::Error;
use crate::{
interface::{AppInterface, Interface},
CommandExt,
};
use crate::{interface::AppInterface, CommandExt};

pub fn command_env(debug: bool) -> HashMap<&'static str, String> {
let mut map = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-cli/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use crate::Result;
use clap::{Parser, Subcommand};

use crate::interface::{AppInterface, AppSettings, Interface};
use crate::interface::{AppInterface, AppSettings};

#[derive(Debug, Parser)]
#[clap(about = "Inspect values used by Tauri")]
Expand Down
40 changes: 1 addition & 39 deletions crates/tauri-cli/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
pub mod rust;

use std::{
collections::HashMap,
path::{Path, PathBuf},
process::ExitStatus,
sync::Arc,
};

use crate::{
error::Context, helpers::app_paths::Dirs, helpers::config::Config,
helpers::config::ConfigMetadata,
};
use crate::{error::Context, helpers::config::Config};
use tauri_bundler::bundle::{PackageType, Settings, SettingsBuilder};

pub use rust::{MobileOptions, Options, Rust as AppInterface, WatcherOptions};

pub trait DevProcess {
fn kill(&self) -> std::io::Result<()>;
fn try_wait(&self) -> std::io::Result<Option<ExitStatus>>;
#[allow(unused)]
fn wait(&self) -> std::io::Result<ExitStatus>;
#[allow(unused)]
Expand Down Expand Up @@ -104,35 +98,3 @@ pub enum ExitReason {
/// Regular exit.
NormalExit,
}

pub trait Interface: Sized {
type AppSettings: AppSettings;

fn new(config: &Config, target: Option<String>, tauri_dir: &Path) -> crate::Result<Self>;
fn app_settings(&self) -> Arc<Self::AppSettings>;
fn env(&self) -> HashMap<&str, String>;
fn build(&mut self, options: Options, dirs: &Dirs) -> crate::Result<PathBuf>;
fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
config: &mut ConfigMetadata,
options: Options,
on_exit: F,
dirs: &Dirs,
) -> crate::Result<()>;
fn mobile_dev<
R: Fn(MobileOptions, &ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>,
>(
&mut self,
config: &mut ConfigMetadata,
options: MobileOptions,
runner: R,
dirs: &Dirs,
) -> crate::Result<()>;
fn watch<R: Fn(&ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
config: &mut ConfigMetadata,
options: WatcherOptions,
runner: R,
dirs: &Dirs,
) -> crate::Result<()>;
}
58 changes: 25 additions & 33 deletions crates/tauri-cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tauri_bundler::{
};
use tauri_utils::config::{parse::is_configuration_file, DeepLinkProtocol, RunnerConfig, Updater};

use super::{AppSettings, DevProcess, ExitReason, Interface};
use super::{AppSettings, DevProcess, ExitReason};
use crate::{
error::{Context, Error, ErrorExt},
helpers::{
Expand Down Expand Up @@ -134,10 +134,8 @@ pub struct Rust {
main_binary_name: Option<String>,
}

impl Interface for Rust {
type AppSettings = RustAppSettings;

fn new(config: &Config, target: Option<String>, tauri_dir: &Path) -> crate::Result<Self> {
impl Rust {
pub fn new(config: &Config, target: Option<String>, tauri_dir: &Path) -> crate::Result<Self> {
let manifest = {
let (tx, rx) = sync_channel(1);
let mut watcher = new_debouncer(Duration::from_secs(1), None, move |r| {
Expand Down Expand Up @@ -177,11 +175,11 @@ impl Interface for Rust {
})
}

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

fn build(&mut self, options: Options, dirs: &Dirs) -> crate::Result<PathBuf> {
pub fn build(&mut self, options: Options, dirs: &Dirs) -> crate::Result<PathBuf> {
desktop::build(
options,
&self.app_settings,
Expand All @@ -192,7 +190,7 @@ impl Interface for Rust {
)
}

fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
pub fn dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
config: &mut ConfigMetadata,
mut options: Options,
Expand All @@ -212,7 +210,7 @@ impl Interface for Rust {

if options.no_watch {
let (tx, rx) = sync_channel(1);
self.run_dev(options, run_args, move |status, reason| {
self.run_dev(options, &run_args, move |status, reason| {
on_exit(status, reason);
tx.send(()).unwrap();
})?;
Expand All @@ -227,16 +225,18 @@ impl Interface for Rust {
&merge_configs,
|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)
})
rust
.run_dev(options.clone(), &run_args, move |status, reason| {
on_exit(status, reason)
})
.map(|child| Box::new(child) as Box<dyn DevProcess + Send>)
},
dirs,
)
}
}

fn mobile_dev<
pub fn mobile_dev<
R: Fn(MobileOptions, &ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>,
>(
&mut self,
Expand Down Expand Up @@ -270,7 +270,7 @@ impl Interface for Rust {
}
}

fn watch<R: Fn(&ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>>(
pub fn watch<R: Fn(&ConfigMetadata) -> crate::Result<Box<dyn DevProcess + Send>>>(
&mut self,
config: &mut ConfigMetadata,
options: WatcherOptions,
Expand All @@ -287,7 +287,7 @@ impl Interface for Rust {
)
}

fn env(&self) -> HashMap<&str, String> {
pub fn env(&self) -> HashMap<&str, String> {
let mut env = HashMap::new();
env.insert(
"TAURI_ENV_TARGET_TRIPLE",
Expand Down Expand Up @@ -363,7 +363,7 @@ fn build_ignore_matcher(dir: &Path) -> IgnoreMatcher {

ignore_builder.add(path);

if let Ok(ignore_file) = std::env::var("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
if let Some(ignore_file) = std::env::var_os("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
ignore_builder.add(dir.join(ignore_file));
}

Expand Down Expand Up @@ -395,7 +395,7 @@ fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
let mut builder = ignore::WalkBuilder::new(dir);
builder.add_custom_ignore_filename(".taurignore");
let _ = builder.add_ignore(default_gitignore);
if let Ok(ignore_file) = std::env::var("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
if let Some(ignore_file) = std::env::var_os("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
builder.add_ignore(ignore_file);
}
builder.require_git(false).ignore(false).max_depth(Some(1));
Expand Down Expand Up @@ -492,17 +492,16 @@ impl Rust {
fn run_dev<F: Fn(Option<i32>, ExitReason) + Send + Sync + 'static>(
&mut self,
options: Options,
run_args: Vec<String>,
run_args: &[String],
on_exit: F,
) -> crate::Result<Box<dyn DevProcess + Send>> {
) -> crate::Result<desktop::DevChild> {
desktop::run_dev(
options,
run_args,
&mut self.available_targets,
self.config_features.clone(),
on_exit,
)
.map(|c| Box::new(c) as Box<dyn DevProcess + Send>)
}

fn run_dev_watcher<
Expand All @@ -515,9 +514,7 @@ impl Rust {
run: F,
dirs: &Dirs,
) -> crate::Result<()> {
let child = run(self, config)?;

let process = Arc::new(Mutex::new(child));
let mut child = run(self, config)?;
let (tx, rx) = sync_channel(1);

let watch_folders = get_watch_folders(additional_watch_folders, dirs.tauri)?;
Expand Down Expand Up @@ -582,17 +579,12 @@ impl Rust {
display_path(event_path.strip_prefix(dirs.frontend).unwrap_or(event_path))
);

let mut p = process.lock().unwrap();
p.kill().context("failed to kill app process")?;
child.kill().context("failed to kill app process")?;

// wait for the process to exit
// note that on mobile, kill() already waits for the process to exit (duct implementation)
loop {
if !matches!(p.try_wait(), Ok(None)) {
break;
}
}
*p = run(self, config)?;
let _ = child.wait();
child = run(self, config)?;
}
}
}
Expand Down Expand Up @@ -1382,7 +1374,7 @@ fn tauri_config_to_bundle_settings(
if enabled_features.contains(&"tray-icon".into())
|| enabled_features.contains(&"tauri/tray-icon".into())
{
let (tray_kind, path) = std::env::var("TAURI_LINUX_AYATANA_APPINDICATOR")
let (tray_kind, path) = std::env::var_os("TAURI_LINUX_AYATANA_APPINDICATOR")
.map(|ayatana| {
if ayatana == "true" || ayatana == "1" {
(
Expand All @@ -1404,7 +1396,7 @@ fn tauri_config_to_bundle_settings(
)
}
})
.unwrap_or_else(|_| pkgconfig_utils::get_appindicator_library_path());
.unwrap_or_else(pkgconfig_utils::get_appindicator_library_path);
match tray_kind {
pkgconfig_utils::TrayKind::Ayatana => {
depends_deb.push("libayatana-appindicator3-1".into());
Expand Down
Loading
Loading