1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! A small utility that generates a typescript declaration file for the [`invoke`] function
//! from functions found in code by Tauri [commands].
//! Thanks to this, there is no mistaking the name of the command.
//!
//! # Example
//!
//! **main.rs:**
//!
//! ```rust
//! fn main() {
//! tauri::Builder::default()
//! .invoke_handler(generate_handler![get_weather, get_config])
//! .run(tauri::generate_context!())
//! .expect("error while running tauri application");
//! }
//!
//! #[tauri::command]
//! fn get_weather() -> String {
//! "sunny".to_string()
//! }
//! // or
//! use tauri::command;
//! #[command]
//! fn get_config() -> String {
//! "config".to_string()
//! }
//! ```
//!
//! **build.rs:**
//!
//! ```rust
//! fn main() {
//! tauri_named_invoke::build("ui").unwrap();
//! tauri_build::build();
//! }
//! ```
//!
//! The file will be generated at the following path:
//!
//! ```shell
//! project root
//! ├── ui
//! │ └── invoke.d.ts
//! ├── src
//! │ └── main.rs
//! └── Cargo.toml
//! ```
//!
//! The generated file will contain:
//!
//! ```typescript
//! import * as tauri from '@tauri-apps/api/tauri';
//! declare module '@tauri-apps/api' {
//! type Commands =
//! 'get_weather'
//! | 'get_config';
//! function invoke<T>(cmd: Commands, args?: InvokeArgs): Promise<T>;
//! }
//! ```
//!
//! [`invoke`]: https://tauri.app/v1/api/js/tauri/#invoke
//! [commands]: https://docs.rs/tauri/1.6.1/tauri/command/index.html
use ;
use glob;
use Regex;
/// Generates an `invoke.d.ts` file declaring [`invoke`] function values composed
/// of function names labeled with the [`tauri::command`] attribute.
///
/// * path - The path to the directory where the `invoke.d.ts` file will be generated.
///
/// # Example
///
/// ```rust
/// fn main() {
/// tauri_named_invoke::build("ui").unwrap();
/// }
/// ```
///
/// The file will be generated at the following path:
///
/// ```shell
/// project root
/// ├── ui
/// │ └── invoke.d.ts
/// ├── src
/// │ └── main.rs
/// └── Cargo.toml
/// ```
///
/// [`invoke`]: https://tauri.app/v1/api/js/tauri/#invoke
/// [`tauri::command`]: https://docs.rs/tauri/1.6.1/tauri/command/index.html