|
4 | 4 |
|
5 | 5 | use anyhow::Context;
|
6 | 6 | use clap::Parser;
|
| 7 | +use colored::Colorize; |
| 8 | +use regex::Regex; |
7 | 9 |
|
8 | 10 | use crate::{
|
9 | 11 | helpers::{
|
@@ -41,8 +43,10 @@ pub fn command(options: Options) -> Result<()> {
|
41 | 43 | let mut plugins = plugins();
|
42 | 44 | let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
|
43 | 45 |
|
| 46 | + let tauri_dir = tauri_dir(); |
| 47 | + |
44 | 48 | let mut cargo = Command::new("cargo");
|
45 |
| - cargo.current_dir(tauri_dir()).arg("add").arg(&crate_name); |
| 49 | + cargo.current_dir(&tauri_dir).arg("add").arg(&crate_name); |
46 | 50 |
|
47 | 51 | if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
|
48 | 52 | cargo
|
@@ -106,45 +110,51 @@ pub fn command(options: Options) -> Result<()> {
|
106 | 110 | }
|
107 | 111 | }
|
108 | 112 |
|
109 |
| - let rust_code = if metadata.builder { |
110 |
| - if metadata.desktop_only { |
111 |
| - format!( |
112 |
| - r#"tauri::Builder::default() |
113 |
| - .setup(|app| {{ |
114 |
| - #[cfg(desktop)] |
115 |
| - app.handle().plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build()); |
116 |
| - Ok(()) |
117 |
| - }}) |
118 |
| - "#, |
119 |
| - ) |
120 |
| - } else { |
121 |
| - format!( |
122 |
| - r#"tauri::Builder::default() |
123 |
| - .setup(|app| {{ |
124 |
| - app.handle().plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build()); |
125 |
| - Ok(()) |
126 |
| - }}) |
127 |
| - "#, |
128 |
| - ) |
| 113 | + // add plugin init code to main.rs or lib.rs |
| 114 | + let re = Regex::new(r#"(tauri\s*::\s*Builder\s*::\s*default\(\))(\s*)\."#)?; |
| 115 | + for file in [tauri_dir.join("src/main.rs"), tauri_dir.join("src/lib.rs")] { |
| 116 | + let contents = std::fs::read_to_string(&file)?; |
| 117 | + if re.is_match(&contents) { |
| 118 | + let plugin_init = if metadata.builder { |
| 119 | + "Builder::new().build()" |
| 120 | + } else { |
| 121 | + "init()" |
| 122 | + }; |
| 123 | + let replacement = format!("$1$2.plugin(tauri_plugin_{plugin_snake_case}::{plugin_init})$2.",); |
| 124 | + let out = re.replace(&contents, replacement); |
| 125 | + |
| 126 | + log::info!("Adding plugin to {}", file.display()); |
| 127 | + std::fs::write(file, out.as_bytes())?; |
| 128 | + |
| 129 | + return Ok(()); |
129 | 130 | }
|
130 |
| - } else if metadata.desktop_only { |
131 |
| - format!( |
132 |
| - r#"tauri::Builder::default() |
133 |
| - .setup(|app| {{ |
134 |
| - #[cfg(desktop)] |
135 |
| - app.handle().plugin(tauri_plugin_{plugin_snake_case}::init()); |
136 |
| - Ok(()) |
137 |
| - }}) |
138 |
| - "#, |
139 |
| - ) |
| 131 | + } |
| 132 | + |
| 133 | + let builder_code = if metadata.builder { |
| 134 | + format!(r#"+ .plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build())"#,) |
140 | 135 | } else {
|
141 |
| - format!( |
142 |
| - r#"tauri::Builder::default().plugin(tauri_plugin_{plugin_snake_case}::init()) |
143 |
| - "#, |
144 |
| - ) |
| 136 | + format!(r#"+ .plugin(tauri_plugin_{plugin_snake_case}::init())"#) |
145 | 137 | };
|
146 | 138 |
|
147 |
| - println!("You must enable the plugin in your Rust code:\n\n{rust_code}"); |
| 139 | + let rust_code = format!( |
| 140 | + r#" {} |
| 141 | +{} |
| 142 | + {}"#, |
| 143 | + "tauri::Builder::default()".dimmed(), |
| 144 | + builder_code.normal().green(), |
| 145 | + r#".invoke_handler(tauri::generate_handler![]) |
| 146 | + .run(tauri::generate_context!()) |
| 147 | + .expect("error while running tauri application");"# |
| 148 | + .dimmed(), |
| 149 | + ); |
| 150 | + |
| 151 | + log::warn!( |
| 152 | + "Couldn't find `{}` in `{}` or `{}`, you must enable the plugin in your Rust code manually:\n\n{}", |
| 153 | + "tauri::Builder".cyan(), |
| 154 | + "main.rs".cyan(), |
| 155 | + "lib.rs".cyan(), |
| 156 | + rust_code |
| 157 | + ); |
148 | 158 |
|
149 | 159 | Ok(())
|
150 | 160 | }
|
|
0 commit comments