Skip to content

Commit ca68689

Browse files
authored
feat(cli): check if project identifier or lib name changed (#10479)
* 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]
1 parent 7e810cb commit ca68689

File tree

10 files changed

+113
-10
lines changed

10 files changed

+113
-10
lines changed

.changes/cli-mobile-checks.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:enhance
3+
"@tauri-apps/cli": patch:enhance
4+
---
5+
6+
Check if identifier or lib name changed when running mobile commands.

tooling/cli/src/mobile/android/android_studio_script.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ pub fn command(options: Options) -> Result<()> {
5454
);
5555
(config, metadata, cli_options)
5656
};
57-
ensure_init(config.project_dir(), MobileTarget::Android)?;
57+
ensure_init(
58+
&tauri_config,
59+
config.app(),
60+
config.project_dir(),
61+
MobileTarget::Android,
62+
)?;
5863

5964
let env = env()?;
6065

tooling/cli/src/mobile/android/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
131131
let tauri_path = tauri_dir();
132132
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
133133

134-
ensure_init(config.project_dir(), MobileTarget::Android)?;
134+
ensure_init(
135+
&tauri_config,
136+
config.app(),
137+
config.project_dir(),
138+
MobileTarget::Android,
139+
)?;
135140

136141
let mut env = env()?;
137142
configure_cargo(&app, Some((&mut env, &config)))?;

tooling/cli/src/mobile/android/dev.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
145145
let tauri_path = tauri_dir();
146146
set_current_dir(tauri_path).with_context(|| "failed to change current working directory")?;
147147

148-
ensure_init(config.project_dir(), MobileTarget::Android)?;
148+
ensure_init(
149+
&tauri_config,
150+
config.app(),
151+
config.project_dir(),
152+
MobileTarget::Android,
153+
)?;
149154
run_dev(
150155
interface,
151156
options,

tooling/cli/src/mobile/android/open.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ pub fn command() -> Result<()> {
2424
&Default::default(),
2525
)
2626
};
27-
ensure_init(config.project_dir(), MobileTarget::Android)?;
27+
ensure_init(
28+
&tauri_config,
29+
config.app(),
30+
config.project_dir(),
31+
MobileTarget::Android,
32+
)?;
2833
inject_assets(&config, tauri_config.lock().unwrap().as_ref().unwrap())?;
2934
let env = env()?;
3035
os::open_file_with("Android Studio", config.project_dir(), &env.base).map_err(Into::into)

tooling/cli/src/mobile/ios/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<()> {
156156
let tauri_path = tauri_dir();
157157
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
158158

159-
ensure_init(config.project_dir(), MobileTarget::Ios)?;
159+
ensure_init(
160+
&tauri_config,
161+
config.app(),
162+
config.project_dir(),
163+
MobileTarget::Ios,
164+
)?;
160165
inject_assets(&config)?;
161166

162167
let info_plist_path = config

tooling/cli/src/mobile/ios/dev.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ fn run_command(options: Options, noise_level: NoiseLevel) -> Result<()> {
168168
let tauri_path = tauri_dir();
169169
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
170170

171-
ensure_init(config.project_dir(), MobileTarget::Ios)?;
171+
ensure_init(
172+
&tauri_config,
173+
config.app(),
174+
config.project_dir(),
175+
MobileTarget::Ios,
176+
)?;
172177
inject_assets(&config)?;
173178

174179
let info_plist_path = config

tooling/cli/src/mobile/ios/open.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ pub fn command() -> Result<()> {
2525
)
2626
};
2727

28-
ensure_init(config.project_dir(), MobileTarget::Ios)?;
28+
ensure_init(
29+
&tauri_config,
30+
config.app(),
31+
config.project_dir(),
32+
MobileTarget::Ios,
33+
)?;
2934
inject_assets(&config)?;
3035
let env = env()?;
3136
os::open_file_with("Xcode", config.project_dir(), &env).map_err(Into::into)

tooling/cli/src/mobile/ios/xcode_script.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use super::{env, get_app, get_config, read_options};
5+
use super::{ensure_init, env, get_app, get_config, read_options, MobileTarget};
66
use crate::{
77
helpers::config::get as get_tauri_config,
88
interface::{AppInterface, AppSettings, Interface, Options as InterfaceOptions},
@@ -82,6 +82,12 @@ pub fn command(options: Options) -> Result<()> {
8282
);
8383
(config, metadata, cli_options)
8484
};
85+
ensure_init(
86+
&tauri_config,
87+
config.app(),
88+
config.project_dir(),
89+
MobileTarget::Ios,
90+
)?;
8591

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

tooling/cli/src/mobile/mod.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
// SPDX-License-Identifier: MIT
44

55
use crate::{
6-
helpers::{app_paths::tauri_dir, config::Config as TauriConfig},
6+
helpers::{
7+
app_paths::tauri_dir,
8+
config::{Config as TauriConfig, ConfigHandle},
9+
},
710
interface::{AppInterface, AppSettings, DevProcess, Interface, Options as InterfaceOptions},
811
};
12+
#[cfg(target_os = "macos")]
13+
use anyhow::Context;
914
use anyhow::{bail, Result};
1015
use heck::ToSnekCase;
1116
use jsonrpsee::core::client::{Client, ClientBuilder, ClientT};
@@ -277,7 +282,13 @@ pub fn get_app(config: &TauriConfig, interface: &AppInterface) -> App {
277282
})
278283
}
279284

280-
fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
285+
#[allow(unused_variables)]
286+
fn ensure_init(
287+
tauri_config: &ConfigHandle,
288+
app: &App,
289+
project_dir: PathBuf,
290+
target: Target,
291+
) -> Result<()> {
281292
if !project_dir.exists() {
282293
bail!(
283294
"{} project directory {} doesn't exist. Please run `tauri {} init` and try again.",
@@ -286,6 +297,51 @@ fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
286297
target.command_name(),
287298
)
288299
}
300+
301+
let tauri_config_guard = tauri_config.lock().unwrap();
302+
let tauri_config_ = tauri_config_guard.as_ref().unwrap();
303+
304+
let mut project_outdated_reasons = Vec::new();
305+
306+
match target {
307+
Target::Android => {
308+
let java_folder = project_dir
309+
.join("app/src/main/java")
310+
.join(tauri_config_.identifier.replace('.', "/"));
311+
if !java_folder.exists() {
312+
project_outdated_reasons
313+
.push("you have modified your \"identifier\" in the Tauri configuration");
314+
}
315+
}
316+
#[cfg(target_os = "macos")]
317+
Target::Ios => {
318+
let project_yml = read_to_string(project_dir.join("project.yml"))
319+
.context("missing project.yml file in the Xcode project directory")?;
320+
if !project_yml.contains(&format!(
321+
"PRODUCT_BUNDLE_IDENTIFIER: {}",
322+
tauri_config_.identifier
323+
)) {
324+
project_outdated_reasons
325+
.push("you have modified your \"identifier\" in the Tauri configuration");
326+
}
327+
328+
println!("{}", app.lib_name());
329+
if !project_yml.contains(&format!("framework: lib{}.a", app.lib_name())) {
330+
project_outdated_reasons
331+
.push("you have modified your [lib.name] or [package.name] in the Cargo.toml file");
332+
}
333+
}
334+
}
335+
336+
if !project_outdated_reasons.is_empty() {
337+
let reason = project_outdated_reasons.join(" and ");
338+
bail!(
339+
"{} project directory is outdated because {reason}. Please run `tauri {} init` and try again.",
340+
target.ide_name(),
341+
target.command_name(),
342+
)
343+
}
344+
289345
Ok(())
290346
}
291347

0 commit comments

Comments
 (0)