Skip to content

Commit

Permalink
fix(cli): prevent creating interface twice
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 4, 2023
1 parent 41067ce commit 682d224
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 99 deletions.
14 changes: 7 additions & 7 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/api/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub fn run_app<R: Runtime, F: FnOnce(&App<R>) + Send + 'static>(
#[cfg(desktop)]
app.manage(PopupMenu(
tauri::menu::MenuBuilder::new(app)
.check("Tauri is awesome!")
.text("Do something")
.check("awesome", "Tauri is awesome!")
.text("do", "Do something")
.copy()
.build()?,
));
Expand Down
23 changes: 11 additions & 12 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
options.ci = options.ci || std::env::var("CI").is_ok();
let ci = options.ci;

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

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

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

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

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

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

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
pub fn setup(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 @@ -220,8 +226,6 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
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 @@ -252,12 +256,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
}

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 AppUrl::Url(WindowUrl::App(web_asset_path)) = &config_.build.dist_dir {
Expand Down Expand Up @@ -300,7 +299,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
.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
19 changes: 11 additions & 8 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ pub fn command(options: Options) -> Result<()> {
}

fn command_internal(mut options: Options) -> Result<()> {
let mut interface = setup(&mut options, false)?;
let config = get_config(options.config.as_deref())?;

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

setup(&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 @@ -135,7 +143,7 @@ pub fn local_ip_address(force: bool) -> &'static IpAddr {
})
}

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
pub fn setup(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 @@ -144,11 +152,6 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
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 @@ -384,7 +387,7 @@ pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
}
}

Ok(interface)
Ok(())
}

pub fn wait_dev_process<
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::{
helpers::{app_paths::tauri_dir, config::get as get_tauri_config},
interface::{AppInterface, Interface},
Result,
};

Expand Down Expand Up @@ -367,7 +368,7 @@ fn png(source: &DynamicImage, out_dir: &Path, ios_color: Rgba<u8>) -> Result<()>
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
crate::mobile::android::get_config(
&crate::mobile::get_app(tauri_config_),
&crate::mobile::get_app(tauri_config_, &AppInterface::new(tauri_config_, None)?),
tauri_config_,
&Default::default(),
)
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
28 changes: 19 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 Target {
}

pub struct Rust {
app_settings: RustAppSettings,
app_settings: Arc<RustAppSettings>,
config_features: Vec<String>,
product_name: Option<String>,
available_targets: Option<Vec<Target>>,
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 @@ -360,6 +360,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 @@ -392,7 +394,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 @@ -514,7 +516,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 @@ -679,7 +681,7 @@ impl CargoSettings {
}

pub struct RustAppSettings {
manifest: Manifest,
manifest: Mutex<Manifest>,
cargo_settings: CargoSettings,
cargo_package_settings: CargoPackageSettings,
package_settings: PackageSettings,
Expand All @@ -697,7 +699,11 @@ impl AppSettings for RustAppSettings {
config: &Config,
features: &[String],
) -> crate::Result<BundleSettings> {
tauri_config_to_bundle_settings(&self.manifest, features, config.tauri.bundle.clone())
tauri_config_to_bundle_settings(
&self.manifest.lock().unwrap(),
features,
config.tauri.bundle.clone(),
)
}

fn app_binary_path(&self, options: &Options) -> crate::Result<PathBuf> {
Expand Down Expand Up @@ -831,6 +837,8 @@ impl AppSettings for RustAppSettings {
fn app_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("package")
Expand All @@ -843,6 +851,8 @@ impl AppSettings for RustAppSettings {
fn lib_name(&self) -> Option<String> {
self
.manifest
.lock()
.unwrap()
.inner
.as_table()
.get("lib")
Expand Down Expand Up @@ -956,7 +966,7 @@ impl RustAppSettings {
});

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 tauri_mobile::{
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
Loading

0 comments on commit 682d224

Please sign in to comment.