Skip to content

Commit

Permalink
feat(cli): check if project identifier or lib name changed (#10479)
Browse files Browse the repository at this point in the history
* feat(cli): check if project identifier changed

* implement ios check

* also check lib name

* clippy

* ensure_init from xcode-script

* fill change file [skip ci]
  • Loading branch information
lucasfernog authored Aug 5, 2024
1 parent 7e810cb commit ca68689
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-mobile-checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:enhance
"@tauri-apps/cli": patch:enhance
---

Check if identifier or lib name changed when running mobile commands.
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/android/android_studio_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ pub fn command(options: Options) -> Result<()> {
);
(config, metadata, cli_options)
};
ensure_init(config.project_dir(), MobileTarget::Android)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Android,
)?;

let env = env()?;

Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

ensure_init(config.project_dir(), MobileTarget::Android)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Android,
)?;

let mut env = env()?;
configure_cargo(&app, Some((&mut env, &config)))?;
Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
let tauri_path = tauri_dir();
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;

ensure_init(config.project_dir(), MobileTarget::Android)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Android,
)?;
run_dev(
interface,
options,
Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/android/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ pub fn command() -> Result<()> {
&Default::default(),
)
};
ensure_init(config.project_dir(), MobileTarget::Android)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Android,
)?;
inject_assets(&config, tauri_config.lock().unwrap().as_ref().unwrap())?;
let env = env()?;
os::open_file_with("Android Studio", config.project_dir(), &env.base).map_err(Into::into)
Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/ios/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
let tauri_path = tauri_dir();
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;

ensure_init(config.project_dir(), MobileTarget::Ios)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Ios,
)?;
inject_assets(&config)?;

let info_plist_path = config
Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/ios/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
let tauri_path = tauri_dir();
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;

ensure_init(config.project_dir(), MobileTarget::Ios)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Ios,
)?;
inject_assets(&config)?;

let info_plist_path = config
Expand Down
7 changes: 6 additions & 1 deletion tooling/cli/src/mobile/ios/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ pub fn command() -> Result<()> {
)
};

ensure_init(config.project_dir(), MobileTarget::Ios)?;
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Ios,
)?;
inject_assets(&config)?;
let env = env()?;
os::open_file_with("Xcode", config.project_dir(), &env).map_err(Into::into)
Expand Down
8 changes: 7 additions & 1 deletion tooling/cli/src/mobile/ios/xcode_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::{env, get_app, get_config, read_options};
use super::{ensure_init, env, get_app, get_config, read_options, MobileTarget};
use crate::{
helpers::config::get as get_tauri_config,
interface::{AppInterface, AppSettings, Interface, Options as InterfaceOptions},
Expand Down Expand Up @@ -82,6 +82,12 @@ pub fn command(options: Options) -> Result<()> {
);
(config, metadata, cli_options)
};
ensure_init(
&tauri_config,
config.app(),
config.project_dir(),
MobileTarget::Ios,
)?;

let env = env()?.explicit_env_vars(cli_options.vars);

Expand Down
60 changes: 58 additions & 2 deletions tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
// SPDX-License-Identifier: MIT

use crate::{
helpers::{app_paths::tauri_dir, config::Config as TauriConfig},
helpers::{
app_paths::tauri_dir,
config::{Config as TauriConfig, ConfigHandle},
},
interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions},
};
#[cfg(target_os = "macos")]
use anyhow::Context;
use anyhow::{bail, Result};
use heck::ToSnekCase;
use jsonrpsee::core::client::{Client, ClientBuilder, ClientT};
Expand Down Expand Up @@ -277,7 +282,13 @@ pub fn get_app(config: &TauriConfig, interface: &AppInterface) -> App {
})
}

fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
#[allow(unused_variables)]
fn ensure_init(
tauri_config: &ConfigHandle,
app: &App,
project_dir: PathBuf,
target: Target,
) -> Result<()> {
if !project_dir.exists() {
bail!(
"{} project directory {} doesn't exist. Please run `tauri {} init` and try again.",
Expand All @@ -286,6 +297,51 @@ fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
target.command_name(),
)
}

let tauri_config_guard = tauri_config.lock().unwrap();
let tauri_config_ = tauri_config_guard.as_ref().unwrap();

let mut project_outdated_reasons = Vec::new();

match target {
Target::Android => {
let java_folder = project_dir
.join("app/src/main/java")
.join(tauri_config_.identifier.replace('.', "/"));
if !java_folder.exists() {
project_outdated_reasons
.push("you have modified your \"identifier\" in the Tauri configuration");
}
}
#[cfg(target_os = "macos")]
Target::Ios => {
let project_yml = read_to_string(project_dir.join("project.yml"))
.context("missing project.yml file in the Xcode project directory")?;
if !project_yml.contains(&format!(
"PRODUCT_BUNDLE_IDENTIFIER: {}",
tauri_config_.identifier
)) {
project_outdated_reasons
.push("you have modified your \"identifier\" in the Tauri configuration");
}

println!("{}", app.lib_name());
if !project_yml.contains(&format!("framework: lib{}.a", app.lib_name())) {
project_outdated_reasons
.push("you have modified your [lib.name] or [package.name] in the Cargo.toml file");
}
}
}

if !project_outdated_reasons.is_empty() {
let reason = project_outdated_reasons.join(" and ");
bail!(
"{} project directory is outdated because {reason}. Please run `tauri {} init` and try again.",
target.ide_name(),
target.command_name(),
)
}

Ok(())
}

Expand Down

0 comments on commit ca68689

Please sign in to comment.