Skip to content

Commit

Permalink
fix(cli): set current dir to tauri dir before reading config, closes #…
Browse files Browse the repository at this point in the history
…6771 (#6782)

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
fix(cli): set current dir to tauri dir before reading config, closes #6771
  • Loading branch information
3 people committed May 26, 2023
1 parent 735db1c commit 1ed2600
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 396 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-mobile-cwd-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'cli.rs': 'patch'
'cli.js': 'patch'
---

Set current directory to tauri directory before reading config file.
15 changes: 4 additions & 11 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, AppUrl, HookCommand, WindowUrl, MERGE_CONFIG_EXTENSION_NAME},
resolve_merge_config,
updater_signature::{read_key_from_file, secret_key as updater_secret_key, sign_file},
},
interface::{AppInterface, AppSettings, Interface},
Expand Down Expand Up @@ -233,22 +234,14 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
}

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
let (merge_config, merge_config_path) = match &options.config {
Some(config) if config.starts_with('{') => (Some(config.to_string()), None),
Some(config) => (
Some(std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?),
Some(config.clone()),
),
None => (None, None),
};

let (merge_config, merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

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

let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

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

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

Expand Down
18 changes: 6 additions & 12 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, reload as reload_config, AppUrl, BeforeDevCommand, WindowUrl},
resolve_merge_config,
},
interface::{AppInterface, DevProcess, ExitReason, Interface},
CommandExt, Result,
Expand Down Expand Up @@ -137,21 +138,14 @@ pub fn local_ip_address(force: bool) -> &'static IpAddr {
}

pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {
let tauri_path = tauri_dir();
options.config = if let Some(config) = &options.config {
Some(if config.starts_with('{') {
config.to_string()
} else {
std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?
})
} else {
None
};

set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
let (merge_config, _merge_config_path) = resolve_merge_config(&options.config)?;
options.config = merge_config;

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

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(),
Expand Down
8 changes: 7 additions & 1 deletion tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use tauri_utils::config::*;

use std::{
collections::HashMap,
env::{set_var, var_os},
env::{current_dir, set_current_dir, set_var, var_os},
ffi::OsStr,
process::exit,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -165,7 +165,13 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
}
}

// the `Config` deserializer for `package > version` can resolve the version from a path relative to the config path
// so we actually need to change the current working directory here
let current_dir = current_dir()?;
set_current_dir(config_path.parent().unwrap())?;
let config: Config = serde_json::from_value(config)?;
// revert to previous working directory
set_current_dir(current_dir)?;

*config_handle().lock().unwrap() = Some(ConfigMetadata {
inner: config,
Expand Down
15 changes: 15 additions & 0 deletions tooling/cli/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod template;
pub mod updater_signature;
pub mod web_dev_server;

use anyhow::Context;

use std::{
collections::HashMap,
path::{Path, PathBuf},
Expand Down Expand Up @@ -52,3 +54,16 @@ pub fn cross_command(bin: &str) -> Command {
let cmd = Command::new(bin);
cmd
}

pub fn resolve_merge_config(
config: &Option<String>,
) -> crate::Result<(Option<String>, Option<String>)> {
match config {
Some(config) if config.starts_with('{') => Ok((Some(config.to_string()), None)),
Some(config) => Ok((
Some(std::fs::read_to_string(config).with_context(|| "failed to read custom configuration")?),
Some(config.clone()),
)),
None => Ok((None, None)),
}
}
48 changes: 29 additions & 19 deletions tooling/cli/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

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

use std::{
collections::HashMap,
Expand Down Expand Up @@ -356,24 +359,31 @@ fn png(source: &DynamicImage, out_dir: &Path, ios_color: Rgba<u8>) -> Result<()>
}

let mut entries = desktop_entries(out_dir);
let _ = crate::mobile::android::with_config(
Some(Default::default()),
|_app, config, _metadata, _cli_options| {
let android_out = out_dir.parent().unwrap().join(format!(
"gen/android/{}/app/src/main/res/",
config.app().name_snake()
));
let out = if android_out.exists() {
android_out
} else {
let out = out_dir.join("android");
create_dir_all(&out).context("Can't create Android output directory")?;
out
};
entries.extend(android_entries(&out)?);
Ok(())
},
);

// Android
let (config, _metadata) = {
let tauri_config = get_tauri_config(None)?;

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_),
tauri_config_,
&Default::default(),
)
};
let android_out = out_dir.parent().unwrap().join(format!(
"gen/android/{}/app/src/main/res/",
config.app().name_snake()
));
let out = if android_out.exists() {
android_out
} else {
let out = out_dir.join("android");
create_dir_all(&out).context("Can't create Android output directory")?;
out
};
entries.extend(android_entries(&out)?);

let ios_out = out_dir
.parent()
Expand Down
28 changes: 4 additions & 24 deletions tooling/cli/src/mobile/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ use super::{
log_finished, read_options, setup_dev_config, CliOptions, Target as MobileTarget,
MIN_DEVICE_MATCH_SCORE,
};
use crate::{
helpers::config::{get as get_tauri_config, Config as TauriConfig},
Result,
};
use crate::{helpers::config::Config as TauriConfig, Result};

mod android_studio_script;
mod build;
Expand Down Expand Up @@ -97,11 +94,10 @@ pub fn command(cli: Cli, verbosity: u8) -> Result<()> {
}

pub fn get_config(
app: Option<App>,
app: &App,
config: &TauriConfig,
cli_options: &CliOptions,
) -> (App, AndroidConfig, AndroidMetadata) {
let app = app.unwrap_or_else(|| get_app(config));
) -> (AndroidConfig, AndroidMetadata) {
let android_options = cli_options.clone();

let raw = RawAndroidConfig {
Expand Down Expand Up @@ -158,23 +154,7 @@ pub fn get_config(
src_main_dir.join("generated"),
);

(app, config, metadata)
}

pub fn with_config<T>(
cli_options: Option<CliOptions>,
f: impl FnOnce(&App, &AndroidConfig, &AndroidMetadata, CliOptions) -> Result<T>,
) -> Result<T> {
let (app, config, metadata, cli_options) = {
let tauri_config = get_tauri_config(None)?;
let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
let cli_options =
cli_options.unwrap_or_else(|| read_options(&tauri_config_.tauri.bundle.identifier));
let (app, config, metadata) = get_config(None, tauri_config_, &cli_options);
(app, config, metadata, cli_options)
};
f(&app, &config, &metadata, cli_options)
(config, metadata)
}

fn env() -> Result<Env> {
Expand Down
59 changes: 33 additions & 26 deletions tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::{detect_target_ok, ensure_init, env, with_config, MobileTarget};
use crate::Result;
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 clap::{ArgAction, Parser};

use tauri_mobile::{
Expand Down Expand Up @@ -36,28 +36,35 @@ pub fn command(options: Options) -> Result<()> {
Profile::Debug
};

with_config(None, |_app, config, metadata, cli_options| {
ensure_init(config.project_dir(), MobileTarget::Android)?;

let env = env()?;

call_for_targets_with_fallback(
options.targets.unwrap_or_default().iter(),
&detect_target_ok,
&env,
|target: &Target| {
target
.build(
config,
metadata,
&env,
cli_options.noise_level,
true,
profile,
)
.map_err(Into::into)
},
)
.map_err(|e| anyhow::anyhow!(e.to_string()))?
})
let tauri_config = get_tauri_config(None)?;

let (config, metadata, cli_options) = {
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);
(config, metadata, cli_options)
};
ensure_init(config.project_dir(), MobileTarget::Android)?;

let env = env()?;

call_for_targets_with_fallback(
options.targets.unwrap_or_default().iter(),
&detect_target_ok,
&env,
|target: &Target| {
target
.build(
&config,
&metadata,
&env,
cli_options.noise_level,
true,
profile,
)
.map_err(Into::into)
},
)
.map_err(|e| anyhow::anyhow!(e.to_string()))?
}
Loading

0 comments on commit 1ed2600

Please sign in to comment.