Skip to content

Commit

Permalink
feat(cli/add): add plugin init code to main.rs (#8490)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
amrbashir and lucasfernog authored Feb 1, 2024
1 parent 30be0e3 commit 7fcc0bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-add-to-main-rs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:feat'
'@tauri-apps/cli': 'patch:feat'
---

Add plugin initialization rust code when using `tauri add`
98 changes: 62 additions & 36 deletions tooling/cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use anyhow::Context;
use clap::Parser;
use colored::Colorize;
use regex::Regex;

use crate::{
helpers::{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}})
"#,
)
// add plugin init code to main.rs or lib.rs
let plugin_init_fn = if metadata.builder {
"Builder::new().build()"
} else {
"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(());
}
} else if metadata.desktop_only {
format!(
r#"tauri::Builder::default()
.setup(|app| {{
#[cfg(desktop)]
app.handle().plugin(tauri_plugin_{plugin_snake_case}::init());
Ok(())
}})
"#,
)
}

let builder_code = if metadata.builder {
format!(r#"+ .plugin(tauri_plugin_{plugin_snake_case}::Builder::new().build())"#,)
} else {
format!(
r#"tauri::Builder::default().plugin(tauri_plugin_{plugin_snake_case}::init())
"#,
)
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(())
}
Expand Down

0 comments on commit 7fcc0bc

Please sign in to comment.