1+ use serde_json:: json;
2+ use std:: collections:: HashMap ;
13use std:: fs;
24use std:: path:: Path ;
3- use std:: collections:: HashMap ;
4- use serde_json:: json;
55
66/// Scan a plugin directory to extract command information
77pub fn scan_plugin ( plugin_path : & Path ) -> Option < PluginInfo > {
88 let plugin_name = plugin_path. file_name ( ) ?. to_str ( ) ?;
99 if !plugin_name. starts_with ( "tauri-plugin-" ) {
1010 return None ;
1111 }
12-
12+
1313 let plugin_id = plugin_name. trim_start_matches ( "tauri-plugin-" ) ;
14-
14+
1515 // Read build.rs to find COMMANDS array
1616 let build_rs_path = plugin_path. join ( "build.rs" ) ;
1717 let commands = if build_rs_path. exists ( ) {
1818 extract_commands_from_build_rs ( & build_rs_path) ?
1919 } else {
2020 vec ! [ ]
2121 } ;
22-
22+
2323 // Read permissions to get descriptions
2424 let permissions_dir = plugin_path. join ( "permissions" ) ;
2525 let command_metadata = if permissions_dir. exists ( ) {
2626 scan_permissions_dir ( & permissions_dir, & commands)
2727 } else {
2828 HashMap :: new ( )
2929 } ;
30-
30+
3131 Some ( PluginInfo {
3232 id : plugin_id. to_string ( ) ,
3333 name : humanize_plugin_name ( plugin_id) ,
34- commands : commands. into_iter ( ) . map ( |cmd| {
35- let metadata = command_metadata. get ( & cmd) ;
36- CommandInfo {
37- name : cmd. clone ( ) ,
38- description : metadata
39- . and_then ( |m| m. get ( "description" ) )
40- . and_then ( |d| d. as_str ( ) )
41- . unwrap_or ( "No description available" )
42- . to_string ( ) ,
43- permission : format ! ( "plugin:{}:{}" , plugin_id, cmd) ,
44- }
45- } ) . collect ( ) ,
34+ commands : commands
35+ . into_iter ( )
36+ . map ( |cmd| {
37+ let metadata = command_metadata. get ( & cmd) ;
38+ CommandInfo {
39+ name : cmd. clone ( ) ,
40+ description : metadata
41+ . and_then ( |m| m. get ( "description" ) )
42+ . and_then ( |d| d. as_str ( ) )
43+ . unwrap_or ( "No description available" )
44+ . to_string ( ) ,
45+ permission : format ! ( "plugin:{}:{}" , plugin_id, cmd) ,
46+ }
47+ } )
48+ . collect ( ) ,
4649 } )
4750}
4851
4952/// Extract COMMANDS array from build.rs
5053fn extract_commands_from_build_rs ( build_rs_path : & Path ) -> Option < Vec < String > > {
5154 let content = fs:: read_to_string ( build_rs_path) . ok ( ) ?;
52-
55+
5356 // Find COMMANDS array
5457 let commands_start = content. find ( "const COMMANDS: &[&str] = &[" ) ?;
5558 let array_start = commands_start + "const COMMANDS: &[&str] = &[" . len ( ) ;
5659 let array_end = content[ array_start..] . find ( ']' ) ?;
5760 let commands_str = & content[ array_start..array_start + array_end] ;
58-
61+
5962 // Parse command names, handling comments properly
6063 let commands: Vec < String > = commands_str
6164 . lines ( )
@@ -68,22 +71,25 @@ fn extract_commands_from_build_rs(build_rs_path: &Path) -> Option<Vec<String>> {
6871 } else {
6972 line
7073 } ;
71-
74+
7275 // Extract quoted strings from the line
7376 line. split ( ',' )
7477 . map ( |s| s. trim ( ) . trim_matches ( '"' ) . to_string ( ) )
7578 . filter ( |s| !s. is_empty ( ) )
7679 . collect :: < Vec < _ > > ( )
7780 } )
7881 . collect ( ) ;
79-
82+
8083 Some ( commands)
8184}
8285
8386/// Scan permissions directory for command metadata
84- fn scan_permissions_dir ( permissions_dir : & Path , commands : & [ String ] ) -> HashMap < String , serde_json:: Value > {
87+ fn scan_permissions_dir (
88+ permissions_dir : & Path ,
89+ commands : & [ String ] ,
90+ ) -> HashMap < String , serde_json:: Value > {
8591 let mut metadata = HashMap :: new ( ) ;
86-
92+
8793 // Check autogenerated commands directory
8894 let commands_dir = permissions_dir. join ( "autogenerated" ) . join ( "commands" ) ;
8995 if commands_dir. exists ( ) {
@@ -101,7 +107,7 @@ fn scan_permissions_dir(permissions_dir: &Path, commands: &[String]) -> HashMap<
101107 }
102108 }
103109 }
104-
110+
105111 metadata
106112}
107113
@@ -138,7 +144,7 @@ pub struct CommandInfo {
138144pub fn scan_all_plugins ( project_root : & Path ) -> Vec < PluginInfo > {
139145 let plugins_dir = project_root. join ( "plugins" ) ;
140146 let mut plugins = Vec :: new ( ) ;
141-
147+
142148 if let Ok ( entries) = fs:: read_dir ( & plugins_dir) {
143149 for entry in entries. flatten ( ) {
144150 if entry. file_type ( ) . map ( |t| t. is_dir ( ) ) . unwrap_or ( false ) {
@@ -148,14 +154,14 @@ pub fn scan_all_plugins(project_root: &Path) -> Vec<PluginInfo> {
148154 }
149155 }
150156 }
151-
157+
152158 plugins
153159}
154160
155161/// Generate Rust code for plugin metadata
156162pub fn generate_plugin_metadata_code ( plugins : & [ PluginInfo ] ) -> String {
157163 let mut code = String :: from (
158- r#"// This file is auto-generated by build.rs
164+ r#"// This file is auto-generated by build.rs
159165// Do not edit manually
160166
161167use crate::plugin_metadata::{PluginMetadata, CommandMetadata, PluginRegistry};
@@ -164,17 +170,19 @@ use serde_json::json;
164170pub fn get_plugin_metadata_registry() -> PluginRegistry {
165171 let mut registry = PluginRegistry::new();
166172
167- "# ) ;
173+ "# ,
174+ ) ;
168175
169176 for plugin in plugins {
170177 code. push_str ( & format ! (
171- r#" {{
178+ r#" {{
172179 let mut commands = vec![];
173- "# ) ) ;
180+ "#
181+ ) ) ;
174182
175183 for cmd in & plugin. commands {
176184 code. push_str ( & format ! (
177- r#" commands.push(CommandMetadata {{
185+ r#" commands.push(CommandMetadata {{
178186 name: "{}".to_string(),
179187 description: "{}".to_string(),
180188 params_schema: json!({{
@@ -187,11 +195,15 @@ r#" commands.push(CommandMetadata {{
187195 destructive: false,
188196 examples: vec![],
189197 }});
190- "# , cmd. name, cmd. description. replace( '"' , r#"\""# ) , cmd. permission) ) ;
198+ "# ,
199+ cmd. name,
200+ cmd. description. replace( '"' , r#"\""# ) ,
201+ cmd. permission
202+ ) ) ;
191203 }
192204
193205 code. push_str ( & format ! (
194- r#"
206+ r#"
195207 registry.add_plugin(PluginMetadata {{
196208 id: "{}".to_string(),
197209 name: "{}".to_string(),
@@ -200,12 +212,16 @@ r#"
200212 }});
201213 }}
202214
203- "# , plugin. id, plugin. name, plugin. name) ) ;
215+ "# ,
216+ plugin. id, plugin. name, plugin. name
217+ ) ) ;
204218 }
205219
206- code. push_str ( r#" registry
220+ code. push_str (
221+ r#" registry
207222}
208- "# ) ;
223+ "# ,
224+ ) ;
209225
210226 code
211- }
227+ }
0 commit comments