Skip to content

Commit 2ee91cf

Browse files
committed
feat(cli/add): add plugin to main.rs
closes #7696
1 parent 27bad32 commit 2ee91cf

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

.changes/cli-add-to-main-rs.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri-cli': 'patch:feat'
3+
'@tauir-apps/cli': 'patch:feat'
4+
---
5+
6+
Add plugin initalization rust code when using `tauri add`

tooling/cli/src/add.rs

+46-36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use anyhow::Context;
66
use clap::Parser;
7+
use colored::Colorize;
8+
use regex::Regex;
79

810
use crate::{
911
helpers::{
@@ -41,8 +43,10 @@ pub fn command(options: Options) -> Result<()> {
4143
let mut plugins = plugins();
4244
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
4345

46+
let tauri_dir = tauri_dir();
47+
4448
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);
4650

4751
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
4852
cargo
@@ -106,45 +110,51 @@ pub fn command(options: Options) -> Result<()> {
106110
}
107111
}
108112

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(());
129130
}
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())"#,)
140135
} 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())"#)
145137
};
146138

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+
);
148158

149159
Ok(())
150160
}

0 commit comments

Comments
 (0)