feat(cli/add): add plugin init code to main.rs (#8490)

* feat(cli/add): add plugin to `main.rs`

closes #7696

* clippy

* fix change file

* adjust regex

* run cargo fmt

* check if already exists

* typo [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2024-02-01 13:05:45 +02:00 committed by GitHub
parent 30be0e3057
commit 7fcc0bcd34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 37 deletions

View File

@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:feat'
'@tauri-apps/cli': 'patch:feat'
---
Add plugin initialization rust code when using `tauri add`

View File

@ -4,6 +4,8 @@
use anyhow::Context;
use clap::Parser;
use colored::Colorize;
use regex::Regex;
use crate::{
helpers::{
@ -41,8 +43,10 @@ pub fn command(options: Options) -> Result<()> {
let mut plugins = plugins();
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
let tauri_dir = tauri_dir();
let mut cargo = Command::new("cargo");
cargo.current_dir(tauri_dir()).arg("add").arg(&crate_name);
cargo.current_dir(&tauri_dir).arg("add").arg(&crate_name);
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
cargo
@ -106,45 +110,67 @@ pub fn command(options: Options) -> Result<()> {
}
}
let rust_code = if metadata.builder {
if metadata.desktop_only {
format!(
r#"tauri::Builder::default()
.setup(|app| {{
#[cfg(desktop)]
app.handle().plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build());
Ok(())
}})
"#,
)
} else {
format!(
r#"tauri::Builder::default()
.setup(|app| {{
app.handle().plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build());
Ok(())
}})
"#,
)
}
} else if metadata.desktop_only {
format!(
r#"tauri::Builder::default()
.setup(|app| {{
#[cfg(desktop)]
app.handle().plugin(tauri_plugin_{plugin_snake_case}::init());
Ok(())
}})
"#,
)
// add plugin init code to main.rs or lib.rs
let plugin_init_fn = if metadata.builder {
"Builder::new().build()"
} else {
format!(
r#"tauri::Builder::default().plugin(tauri_plugin_{plugin_snake_case}::init())
"#,
)
"init()"
};
let plugin_init = format!(".plugin(tauri_plugin_{plugin_snake_case}::{plugin_init_fn})");
let re = Regex::new(r"(tauri\s*::\s*Builder\s*::\s*default\(\))(\s*)")?;
for file in [tauri_dir.join("src/main.rs"), tauri_dir.join("src/lib.rs")] {
let contents = std::fs::read_to_string(&file)?;
if contents.contains(&plugin_init) {
log::info!(
"Plugin initialization code already found on {}",
file.display()
);
return Ok(());
}
if re.is_match(&contents) {
let out = re.replace(&contents, format!("$1$2{plugin_init}$2"));
log::info!("Adding plugin to {}", file.display());
std::fs::write(file, out.as_bytes())?;
// run cargo fmt
log::info!("Running `cargo fmt`...");
let _ = Command::new("cargo")
.arg("fmt")
.current_dir(&tauri_dir)
.status();
return Ok(());
}
}
let builder_code = if metadata.builder {
format!(r#"+ .plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build())"#,)
} else {
format!(r#"+ .plugin(tauri_plugin_{plugin_snake_case}::init())"#)
};
println!("You must enable the plugin in your Rust code:\n\n{rust_code}");
let rust_code = format!(
r#" {}
{}
{}"#,
"tauri::Builder::default()".dimmed(),
builder_code.normal().green(),
r#".invoke_handler(tauri::generate_handler![])
.run(tauri::generate_context!())
.expect("error while running tauri application");"#
.dimmed(),
);
log::warn!(
"Couldn't find `{}` in `{}` or `{}`, you must enable the plugin in your Rust code manually:\n\n{}",
"tauri::Builder".cyan(),
"main.rs".cyan(),
"lib.rs".cyan(),
rust_code
);
Ok(())
}