@@ -11,7 +11,7 @@ use anyhow::Context;
11
11
use clap:: Parser ;
12
12
use dialoguer:: Input ;
13
13
use handlebars:: { to_json, Handlebars } ;
14
- use heck:: { AsKebabCase , ToKebabCase , ToPascalCase , ToSnakeCase } ;
14
+ use heck:: { ToKebabCase , ToPascalCase , ToSnakeCase } ;
15
15
use include_dir:: { include_dir, Dir } ;
16
16
use log:: warn;
17
17
use std:: {
@@ -29,25 +29,34 @@ pub const TEMPLATE_DIR: Dir<'_> = include_dir!("templates/plugin");
29
29
#[ derive( Debug , Parser ) ]
30
30
#[ clap( about = "Initializes a Tauri plugin project" ) ]
31
31
pub 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 > ,
35
35
/// Initializes a Tauri plugin without the TypeScript API
36
36
#[ clap( long) ]
37
- no_api : bool ,
37
+ pub ( crate ) no_api : bool ,
38
38
/// Initializes a Tauri core plugin (internal usage)
39
39
#[ clap( long, hide( true ) ) ]
40
- tauri : bool ,
40
+ pub ( crate ) tauri : bool ,
41
41
/// Set target directory for init
42
42
#[ clap( short, long) ]
43
43
#[ clap( default_value_t = current_dir( ) . expect( "failed to read cwd" ) . display( ) . to_string( ) ) ]
44
- directory : String ,
44
+ pub ( crate ) directory : String ,
45
45
/// Path of the Tauri project to use (relative to the cwd)
46
46
#[ clap( short, long) ]
47
- tauri_path : Option < PathBuf > ,
47
+ pub ( crate ) tauri_path : Option < PathBuf > ,
48
48
/// Author name
49
49
#[ 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 ,
51
60
}
52
61
53
62
impl Options {
@@ -64,12 +73,15 @@ impl Options {
64
73
65
74
pub fn command ( mut options : Options ) -> Result < ( ) > {
66
75
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 ) ;
71
83
let metadata = crates_metadata ( ) ?;
72
- if template_target_path. exists ( ) {
84
+ if std :: fs :: read_dir ( & template_target_path) ? . count ( ) > 0 {
73
85
warn ! ( "Plugin dir ({:?}) not empty." , template_target_path) ;
74
86
} else {
75
87
let ( tauri_dep, tauri_example_dep, tauri_build_dep) =
@@ -101,7 +113,7 @@ pub fn command(mut options: Options) -> Result<()> {
101
113
handlebars. register_escape_fn ( handlebars:: no_escape) ;
102
114
103
115
let mut data = BTreeMap :: new ( ) ;
104
- plugin_name_data ( & mut data, & options . plugin_name ) ;
116
+ plugin_name_data ( & mut data, & plugin_name) ;
105
117
data. insert ( "tauri_dep" , to_json ( tauri_dep) ) ;
106
118
data. insert ( "tauri_example_dep" , to_json ( tauri_example_dep) ) ;
107
119
data. insert ( "tauri_build_dep" , to_json ( tauri_build_dep) ) ;
@@ -120,15 +132,20 @@ pub fn command(mut options: Options) -> Result<()> {
120
132
) ;
121
133
}
122
134
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 ( ) ;
130
143
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
+ } ;
132
149
133
150
let mut created_dirs = Vec :: new ( ) ;
134
151
template:: render_with_generator (
@@ -157,13 +174,18 @@ pub fn command(mut options: Options) -> Result<()> {
157
174
}
158
175
}
159
176
"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
+ }
166
187
}
188
+ "ios" if !( options. ios || options. mobile ) => return Ok ( None ) ,
167
189
"webview-dist" | "webview-src" | "package.json" => {
168
190
if options. no_api {
169
191
return Ok ( None ) ;
0 commit comments