Skip to content

Commit bc15b37

Browse files
committed
refactor(cli): enhance plugin subcommand, closes #7749
1 parent 713f84d commit bc15b37

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

.changes/cli-plugin-init.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'tauri-cli': 'patch'
3+
'@tauri-apps/cli': 'patch'
4+
---
5+
6+
The `tauri plugin` subcommand is receving a couple of consitency and quality of life improvements:
7+
8+
- Renamed `tauri plugin android/ios add` command to `tauri plugin android/ios init` to match the `tauri plugin init` command.
9+
- Removed the `-n/--name` argument from the `tauri plugin init`, `tauri plugin android/ios init`, and is now parsed from the first positional argument.
10+
- Changed `tauri plugin init` to NOT generate mobile projects by default, you can opt-in to generate them using `--android` and `--ios` flags or `--mobile` flag or initalize them later using `tauri plugin android/ios init`.

tooling/cli/src/plugin/android.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ pub struct Cli {
2828

2929
#[derive(Subcommand)]
3030
enum Commands {
31-
Add(AddOptions),
31+
Init(InitOptions),
3232
}
3333

3434
#[derive(Debug, Parser)]
35-
#[clap(about = "Adds the Android project to an existing Tauri plugin")]
36-
pub struct AddOptions {
35+
#[clap(about = "Initializes the Android project for an existing Tauri plugin")]
36+
pub struct InitOptions {
3737
/// Name of your Tauri plugin. Must match the current plugin's name.
38-
#[clap(short = 'n', long = "name")]
3938
plugin_name: String,
4039
/// The output directory.
4140
#[clap(short, long)]
@@ -45,7 +44,7 @@ pub struct AddOptions {
4544

4645
pub fn command(cli: Cli) -> Result<()> {
4746
match cli.command {
48-
Commands::Add(options) => {
47+
Commands::Init(options) => {
4948
let out_dir = PathBuf::from(options.out_dir);
5049
if out_dir.join("android").exists() {
5150
return Err(anyhow::anyhow!("android folder already exists"));

tooling/cli/src/plugin/init.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub const TEMPLATE_DIR: Dir<'_> = include_dir!("templates/plugin");
3030
#[clap(about = "Initializes a Tauri plugin project")]
3131
pub struct Options {
3232
/// Name of your Tauri plugin
33-
#[clap(short = 'n', long = "name")]
3433
plugin_name: String,
3534
/// Initializes a Tauri plugin without the TypeScript API
3635
#[clap(long)]
@@ -48,6 +47,15 @@ pub struct Options {
4847
/// Author name
4948
#[clap(short, long)]
5049
author: Option<String>,
50+
/// Whether to initialize an Android project for the plugin.
51+
#[clap(long)]
52+
android: bool,
53+
/// Whether to initialize an iOS project for the plugin.
54+
#[clap(long)]
55+
ios: bool,
56+
/// Whether to initialize Android and iOS projects for the plugin.
57+
#[clap(long)]
58+
mobile: bool,
5159
}
5260

5361
impl Options {
@@ -120,15 +128,20 @@ pub fn command(mut options: Options) -> Result<()> {
120128
);
121129
}
122130

123-
let plugin_id = request_input(
124-
"What should be the Android Package ID for your plugin?",
125-
Some(format!("com.plugin.{}", options.plugin_name)),
126-
false,
127-
false,
128-
)?
129-
.unwrap();
131+
let plugin_id = if options.android || options.mobile {
132+
let plugin_id = request_input(
133+
"What should be the Android Package ID for your plugin?",
134+
Some(format!("com.plugin.{}", options.plugin_name)),
135+
false,
136+
false,
137+
)?
138+
.unwrap();
130139

131-
data.insert("android_package_id", to_json(&plugin_id));
140+
data.insert("android_package_id", to_json(&plugin_id));
141+
Some(plugin_id)
142+
} else {
143+
None
144+
};
132145

133146
let mut created_dirs = Vec::new();
134147
template::render_with_generator(
@@ -157,13 +170,18 @@ pub fn command(mut options: Options) -> Result<()> {
157170
}
158171
}
159172
"android" => {
160-
return generate_android_out_file(
161-
&path,
162-
&template_target_path,
163-
&plugin_id.replace('.', "/"),
164-
&mut created_dirs,
165-
);
173+
if options.android || options.mobile {
174+
return generate_android_out_file(
175+
&path,
176+
&template_target_path,
177+
&plugin_id.as_ref().unwrap().replace('.', "/"),
178+
&mut created_dirs,
179+
);
180+
} else {
181+
return Ok(None);
182+
}
166183
}
184+
"ios" if !(options.ios || options.mobile) => return Ok(None),
167185
"webview-dist" | "webview-src" | "package.json" => {
168186
if options.no_api {
169187
return Ok(None);

tooling/cli/src/plugin/ios.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ pub struct Cli {
2929

3030
#[derive(Subcommand)]
3131
enum Commands {
32-
Add(AddOptions),
32+
Init(InitOptions),
3333
}
3434

3535
#[derive(Debug, Parser)]
36-
#[clap(about = "Adds the iOS project to an existing Tauri plugin")]
37-
pub struct AddOptions {
36+
#[clap(about = "Initializes the iOS project for an existing Tauri plugin")]
37+
pub struct InitOptions {
3838
/// Name of your Tauri plugin. Must match the current plugin's name.
39-
#[clap(short = 'n', long = "name")]
4039
plugin_name: String,
4140
/// The output directory.
4241
#[clap(short, long)]
@@ -46,7 +45,7 @@ pub struct AddOptions {
4645

4746
pub fn command(cli: Cli) -> Result<()> {
4847
match cli.command {
49-
Commands::Add(options) => {
48+
Commands::Init(options) => {
5049
let out_dir = PathBuf::from(options.out_dir);
5150
if out_dir.join("ios").exists() {
5251
return Err(anyhow::anyhow!("ios folder already exists"));

0 commit comments

Comments
 (0)