@@ -11,7 +11,7 @@ use anyhow::Context;
1111use clap:: Parser ;
1212use dialoguer:: Input ;
1313use handlebars:: { to_json, Handlebars } ;
14- use heck:: { AsKebabCase , ToKebabCase , ToPascalCase , ToSnakeCase } ;
14+ use heck:: { ToKebabCase , ToPascalCase , ToSnakeCase } ;
1515use include_dir:: { include_dir, Dir } ;
1616use log:: warn;
1717use std:: {
@@ -29,25 +29,34 @@ pub const TEMPLATE_DIR: Dir<'_> = include_dir!("templates/plugin");
2929#[ derive( Debug , Parser ) ]
3030#[ clap( about = "Initializes a Tauri plugin project" ) ]
3131pub struct Options {
32- /// Name of your Tauri plugin
33- # [ clap ( short = 'n' , long = "name" ) ]
34- plugin_name : String ,
32+ /// Name of your Tauri plugin.
33+ /// If not specified, it will be infered from the current directory.
34+ pub ( crate ) plugin_name : Option < String > ,
3535 /// Initializes a Tauri plugin without the TypeScript API
3636 #[ clap( long) ]
37- no_api : bool ,
37+ pub ( crate ) no_api : bool ,
3838 /// Initializes a Tauri core plugin (internal usage)
3939 #[ clap( long, hide( true ) ) ]
40- tauri : bool ,
40+ pub ( crate ) tauri : bool ,
4141 /// Set target directory for init
4242 #[ clap( short, long) ]
4343 #[ clap( default_value_t = current_dir( ) . expect( "failed to read cwd" ) . display( ) . to_string( ) ) ]
44- directory : String ,
44+ pub ( crate ) directory : String ,
4545 /// Path of the Tauri project to use (relative to the cwd)
4646 #[ clap( short, long) ]
47- tauri_path : Option < PathBuf > ,
47+ pub ( crate ) tauri_path : Option < PathBuf > ,
4848 /// Author name
4949 #[ clap( short, long) ]
50- author : Option < String > ,
50+ pub ( crate ) author : Option < String > ,
51+ /// Whether to initialize an Android project for the plugin.
52+ #[ clap( long) ]
53+ pub ( crate ) android : bool ,
54+ /// Whether to initialize an iOS project for the plugin.
55+ #[ clap( long) ]
56+ pub ( crate ) ios : bool ,
57+ /// Whether to initialize Android and iOS projects for the plugin.
58+ #[ clap( long) ]
59+ pub ( crate ) mobile : bool ,
5160}
5261
5362impl Options {
@@ -64,12 +73,15 @@ impl Options {
6473
6574pub fn command ( mut options : Options ) -> Result < ( ) > {
6675 options. load ( ) ;
67- let template_target_path = PathBuf :: from ( options. directory ) . join ( format ! (
68- "tauri-plugin-{}" ,
69- AsKebabCase ( & options. plugin_name)
70- ) ) ;
76+
77+ let plugin_name = match options. plugin_name {
78+ None => super :: infer_plugin_name ( & options. directory ) ?,
79+ Some ( name) => name,
80+ } ;
81+
82+ let template_target_path = PathBuf :: from ( options. directory ) ;
7183 let metadata = crates_metadata ( ) ?;
72- if template_target_path. exists ( ) {
84+ if std :: fs :: read_dir ( & template_target_path) ? . count ( ) > 0 {
7385 warn ! ( "Plugin dir ({:?}) not empty." , template_target_path) ;
7486 } else {
7587 let ( tauri_dep, tauri_example_dep, tauri_build_dep) =
@@ -101,7 +113,7 @@ pub fn command(mut options: Options) -> Result<()> {
101113 handlebars. register_escape_fn ( handlebars:: no_escape) ;
102114
103115 let mut data = BTreeMap :: new ( ) ;
104- plugin_name_data ( & mut data, & options . plugin_name ) ;
116+ plugin_name_data ( & mut data, & plugin_name) ;
105117 data. insert ( "tauri_dep" , to_json ( tauri_dep) ) ;
106118 data. insert ( "tauri_example_dep" , to_json ( tauri_example_dep) ) ;
107119 data. insert ( "tauri_build_dep" , to_json ( tauri_build_dep) ) ;
@@ -120,15 +132,20 @@ pub fn command(mut options: Options) -> Result<()> {
120132 ) ;
121133 }
122134
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 ( ) ;
135+ let plugin_id = if options. android || options. mobile {
136+ let plugin_id = request_input (
137+ "What should be the Android Package ID for your plugin?" ,
138+ Some ( format ! ( "com.plugin.{}" , plugin_name) ) ,
139+ false ,
140+ false ,
141+ ) ?
142+ . unwrap ( ) ;
130143
131- data. insert ( "android_package_id" , to_json ( & plugin_id) ) ;
144+ data. insert ( "android_package_id" , to_json ( & plugin_id) ) ;
145+ Some ( plugin_id)
146+ } else {
147+ None
148+ } ;
132149
133150 let mut created_dirs = Vec :: new ( ) ;
134151 template:: render_with_generator (
@@ -157,13 +174,18 @@ pub fn command(mut options: Options) -> Result<()> {
157174 }
158175 }
159176 "android" => {
160- return generate_android_out_file (
161- & path,
162- & template_target_path,
163- & plugin_id. replace ( '.' , "/" ) ,
164- & mut created_dirs,
165- ) ;
177+ if options. android || options. mobile {
178+ return generate_android_out_file (
179+ & path,
180+ & template_target_path,
181+ & plugin_id. as_ref ( ) . unwrap ( ) . replace ( '.' , "/" ) ,
182+ & mut created_dirs,
183+ ) ;
184+ } else {
185+ return Ok ( None ) ;
186+ }
166187 }
188+ "ios" if !( options. ios || options. mobile ) => return Ok ( None ) ,
167189 "webview-dist" | "webview-src" | "package.json" => {
168190 if options. no_api {
169191 return Ok ( None ) ;
0 commit comments