Skip to content

Commit efa82f5

Browse files
authored
fix(cli): correct plugin scaffold names and ios init snippet (#16125)
1 parent db6804d commit efa82f5

4 files changed

Lines changed: 130 additions & 26 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
Fix plugin scaffolding for names like `my-plugin`: the default Android package ID now uses a snake_case segment (`com.plugin.my_plugin`) and user-provided IDs are validated, iOS Xcode folders use the same kebab-case name as the project file, and the `plugin ios init`/`plugin android init` code snippets now match the generated bindings and the `setup(|app, api| ..)` API. `plugin init` into a non-empty directory no longer creates a `permissions` folder after skipping the template.

‎crates/tauri-cli/src/plugin/android.rs‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use crate::{
6-
Result,
7-
error::Context,
8-
helpers::{prompts, template},
9-
};
5+
use crate::{Result, error::Context, helpers::template};
106
use clap::{Parser, Subcommand};
117
use handlebars::Handlebars;
8+
use heck::ToKebabCase;
129

1310
use std::{
1411
collections::BTreeMap,
@@ -62,13 +59,7 @@ pub fn command(cli: Cli) -> Result<()> {
6259
crate::error::bail!("Android folder already exists");
6360
}
6461

65-
let plugin_id = prompts::input(
66-
"What should be the Android Package ID for your plugin?",
67-
Some(format!("com.plugin.{plugin_name}")),
68-
false,
69-
false,
70-
)?
71-
.unwrap();
62+
let plugin_id = super::init::prompt_android_package_id(&plugin_name)?;
7263

7364
let handlebars = Handlebars::new();
7465

@@ -114,10 +105,11 @@ tauri-build = "{}"
114105
.unwrap()
115106
.contents_utf8()
116107
.unwrap();
108+
let builder_name = plugin_name.to_kebab_case();
117109
let init_fn = format!(
118110
r#"
119111
pub fn init<R: Runtime>() -> TauriPlugin<R> {{
120-
Builder::new("{plugin_name}")
112+
Builder::new("{builder_name}")
121113
.setup(|app, api| {{
122114
#[cfg(target_os = "android")]
123115
let handle = api.register_android_plugin("{plugin_id}", "ExamplePlugin")?;

‎crates/tauri-cli/src/plugin/init.rs‎

Lines changed: 110 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub fn command(mut options: Options) -> Result<()> {
9898
> 0
9999
{
100100
log::warn!("Plugin dir ({:?}) not empty.", template_target_path);
101+
return Ok(());
101102
} else {
102103
let (tauri_dep, tauri_example_dep, tauri_build_dep, tauri_plugin_dep) =
103104
if let Some(tauri_path) = options.tauri_path {
@@ -160,13 +161,7 @@ pub fn command(mut options: Options) -> Result<()> {
160161
}
161162

162163
let plugin_id = if options.android || options.mobile {
163-
let plugin_id = prompts::input(
164-
"What should be the Android Package ID for your plugin?",
165-
Some(format!("com.plugin.{plugin_name}")),
166-
false,
167-
false,
168-
)?
169-
.unwrap();
164+
let plugin_id = prompt_android_package_id(&plugin_name)?;
170165

171166
data.insert("android_package_id", to_json(&plugin_id));
172167
Some(plugin_id)
@@ -220,7 +215,8 @@ pub fn command(mut options: Options) -> Result<()> {
220215
"ios-xcode" if !matches!(ios_framework, PluginIosFramework::Xcode) => return Ok(None),
221216
"ios-spm" | "ios-xcode" => {
222217
let folder_name = components.next().unwrap().as_os_str().to_string_lossy();
223-
let new_folder_name = folder_name.replace("{{ plugin_name }}", &plugin_name);
218+
let new_folder_name =
219+
folder_name.replace("{{ plugin_name }}", &plugin_name.to_kebab_case());
224220
let new_folder_name = OsString::from(&new_folder_name);
225221

226222
path = [
@@ -283,6 +279,96 @@ pub fn plugin_name_data(data: &mut BTreeMap<&'static str, serde_json::Value>, pl
283279
);
284280
}
285281

282+
/// Prompts for the Android package ID of the plugin,
283+
/// defaulting to `com.plugin.<plugin_name_snake_case>`.
284+
pub fn prompt_android_package_id(plugin_name: &str) -> Result<String> {
285+
let plugin_id = prompts::input(
286+
"What should be the Android Package ID for your plugin?",
287+
Some(format!("com.plugin.{}", plugin_name.to_snake_case())),
288+
false,
289+
false,
290+
)?
291+
.unwrap();
292+
validate_android_package_id(&plugin_id)?;
293+
Ok(plugin_id)
294+
}
295+
296+
// Keywords that cannot be used as Java identifiers.
297+
const JAVA_KEYWORDS: &[&str] = &[
298+
"abstract",
299+
"assert",
300+
"boolean",
301+
"break",
302+
"byte",
303+
"case",
304+
"catch",
305+
"char",
306+
"class",
307+
"const",
308+
"continue",
309+
"default",
310+
"do",
311+
"double",
312+
"else",
313+
"enum",
314+
"extends",
315+
"false",
316+
"final",
317+
"finally",
318+
"float",
319+
"for",
320+
"goto",
321+
"if",
322+
"implements",
323+
"import",
324+
"instanceof",
325+
"int",
326+
"interface",
327+
"long",
328+
"native",
329+
"new",
330+
"null",
331+
"package",
332+
"private",
333+
"protected",
334+
"public",
335+
"return",
336+
"short",
337+
"static",
338+
"strictfp",
339+
"super",
340+
"switch",
341+
"synchronized",
342+
"this",
343+
"throw",
344+
"throws",
345+
"transient",
346+
"true",
347+
"try",
348+
"void",
349+
"volatile",
350+
"while",
351+
"_",
352+
];
353+
354+
/// Validates that every segment of the Android package ID is a valid Java identifier.
355+
fn validate_android_package_id(package_id: &str) -> Result<()> {
356+
for segment in package_id.split('.') {
357+
let mut chars = segment.chars();
358+
let valid = chars
359+
.next()
360+
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
361+
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
362+
&& !JAVA_KEYWORDS.contains(&segment);
363+
if !valid {
364+
crate::error::bail!(
365+
"invalid Android package ID `{package_id}`: `{segment}` is not a valid Java identifier; each dot-separated segment must start with a letter or `_` and contain only letters, digits and `_`"
366+
);
367+
}
368+
}
369+
Ok(())
370+
}
371+
286372
pub fn crates_metadata() -> Result<VersionMetadata> {
287373
serde_json::from_str::<VersionMetadata>(include_str!("../../metadata-v2.json"))
288374
.context("failed to parse Tauri version metadata")
@@ -327,3 +413,19 @@ pub fn generate_android_out_file(
327413
Ok(None)
328414
}
329415
}
416+
417+
#[cfg(test)]
418+
mod tests {
419+
use super::*;
420+
421+
#[test]
422+
fn android_package_id_validation() {
423+
assert!(validate_android_package_id("com.plugin.my_plugin").is_ok());
424+
assert!(validate_android_package_id("com.plugin._private2").is_ok());
425+
assert!(validate_android_package_id("com.plugin.my-plugin").is_err());
426+
assert!(validate_android_package_id("com.plugin.2d").is_err());
427+
assert!(validate_android_package_id("com..plugin").is_err());
428+
assert!(validate_android_package_id("com.plugin.class").is_err());
429+
assert!(validate_android_package_id("").is_err());
430+
}
431+
}

‎crates/tauri-cli/src/plugin/ios.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::PluginIosFramework;
66
use crate::{Result, error::Context, helpers::template};
77
use clap::{Parser, Subcommand};
88
use handlebars::Handlebars;
9+
use heck::{ToKebabCase, ToSnakeCase};
910

1011
use std::{
1112
collections::BTreeMap,
@@ -86,7 +87,8 @@ pub fn command(cli: Cli) -> Result<()> {
8687
if let Component::Normal(component) = root {
8788
if component == ios_folder_name {
8889
let folder_name = components.next().unwrap().as_os_str().to_string_lossy();
89-
let new_folder_name = folder_name.replace("{{ plugin_name }}", &plugin_name);
90+
let new_folder_name =
91+
folder_name.replace("{{ plugin_name }}", &plugin_name.to_kebab_case());
9092
let new_folder_name = OsString::from(&new_folder_name);
9193

9294
let path = [
@@ -125,16 +127,18 @@ tauri-build = "{}"
125127
.unwrap()
126128
.contents_utf8()
127129
.unwrap();
130+
let builder_name = plugin_name.to_kebab_case();
131+
let binding_name = plugin_name.to_snake_case();
128132
let init_fn = format!(
129133
r#"
130134
#[cfg(target_os = "ios")]
131-
tauri::ios_plugin_binding!(init_plugin_{plugin_name});
135+
tauri::ios_plugin_binding!(init_plugin_{binding_name});
132136
133137
pub fn init<R: Runtime>() -> TauriPlugin<R> {{
134-
Builder::new("{plugin_name}")
135-
.setup(|app| {{
138+
Builder::new("{builder_name}")
139+
.setup(|app, api| {{
136140
#[cfg(target_os = "ios")]
137-
app.register_ios_plugin(init_plugin_{plugin_name})?;
141+
api.register_ios_plugin(init_plugin_{binding_name})?;
138142
Ok(())
139143
}})
140144
.build()

0 commit comments

Comments
 (0)