From 2ee91cff7b59b62469d8bfe89c8f2df85dfbba3e Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 28 Dec 2023 19:13:23 +0200 Subject: [PATCH 1/7] feat(cli/add): add plugin to `main.rs` closes #7696 --- .changes/cli-add-to-main-rs.md | 6 +++ tooling/cli/src/add.rs | 82 +++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 .changes/cli-add-to-main-rs.md diff --git a/.changes/cli-add-to-main-rs.md b/.changes/cli-add-to-main-rs.md new file mode 100644 index 000000000000..70354544d82c --- /dev/null +++ b/.changes/cli-add-to-main-rs.md @@ -0,0 +1,6 @@ +--- +'tauri-cli': 'patch:feat' +'@tauir-apps/cli': 'patch:feat' +--- + +Add plugin initalization rust code when using `tauri add` diff --git a/tooling/cli/src/add.rs b/tooling/cli/src/add.rs index db535a03940b..e317f0f13543 100644 --- a/tooling/cli/src/add.rs +++ b/tooling/cli/src/add.rs @@ -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,51 @@ 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 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 re.is_match(&contents) { + let plugin_init = if metadata.builder { + "Builder::new().build()" + } else { + "init()" + }; + let replacement = format!("$1$2.plugin(tauri_plugin_{plugin_snake_case}::{plugin_init})$2.",); + let out = re.replace(&contents, replacement); + + log::info!("Adding plugin to {}", file.display()); + std::fs::write(file, out.as_bytes())?; + + 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(()) } From f550ead9f264683e76ade165546f006304911115 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 28 Dec 2023 19:14:38 +0200 Subject: [PATCH 2/7] clippy --- tooling/cli/src/add.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/cli/src/add.rs b/tooling/cli/src/add.rs index e317f0f13543..830234a7a412 100644 --- a/tooling/cli/src/add.rs +++ b/tooling/cli/src/add.rs @@ -111,7 +111,7 @@ pub fn command(options: Options) -> Result<()> { } // add plugin init code to main.rs or lib.rs - let re = Regex::new(r#"(tauri\s*::\s*Builder\s*::\s*default\(\))(\s*)\."#)?; + 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 re.is_match(&contents) { From 5f969273c12f6539d95a4cfb8fd58ac59b8a51aa Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 28 Dec 2023 19:14:54 +0200 Subject: [PATCH 3/7] fix change file --- .changes/cli-add-to-main-rs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/cli-add-to-main-rs.md b/.changes/cli-add-to-main-rs.md index 70354544d82c..bc7080a99e12 100644 --- a/.changes/cli-add-to-main-rs.md +++ b/.changes/cli-add-to-main-rs.md @@ -1,6 +1,6 @@ --- 'tauri-cli': 'patch:feat' -'@tauir-apps/cli': 'patch:feat' +'@tauri-apps/cli': 'patch:feat' --- Add plugin initalization rust code when using `tauri add` From 8fb6afe565b5711b6f5fe75a82a0b9c63091aa10 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 31 Jan 2024 16:24:06 -0300 Subject: [PATCH 4/7] adjust regex --- tooling/cli/src/add.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tooling/cli/src/add.rs b/tooling/cli/src/add.rs index 830234a7a412..13e23ae1e0f2 100644 --- a/tooling/cli/src/add.rs +++ b/tooling/cli/src/add.rs @@ -111,7 +111,7 @@ pub fn command(options: Options) -> Result<()> { } // add plugin init code to main.rs or lib.rs - let re = Regex::new(r"(tauri\s*::\s*Builder\s*::\s*default\(\))(\s*)\.")?; + 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 re.is_match(&contents) { @@ -120,7 +120,7 @@ pub fn command(options: Options) -> Result<()> { } else { "init()" }; - let replacement = format!("$1$2.plugin(tauri_plugin_{plugin_snake_case}::{plugin_init})$2.",); + let replacement = format!("$1$2.plugin(tauri_plugin_{plugin_snake_case}::{plugin_init})$2"); let out = re.replace(&contents, replacement); log::info!("Adding plugin to {}", file.display()); From 68cfbabd854f8b46689a7df110bc9f201f087282 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 31 Jan 2024 16:29:07 -0300 Subject: [PATCH 5/7] run cargo fmt --- tooling/cli/src/add.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tooling/cli/src/add.rs b/tooling/cli/src/add.rs index 13e23ae1e0f2..b61751b7dcee 100644 --- a/tooling/cli/src/add.rs +++ b/tooling/cli/src/add.rs @@ -126,6 +126,13 @@ pub fn command(options: Options) -> Result<()> { 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(()); } } From 344ea157dbc7a4f625e8bf0a16c1733e83a75179 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 31 Jan 2024 16:33:03 -0300 Subject: [PATCH 6/7] check if already exists --- tooling/cli/src/add.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tooling/cli/src/add.rs b/tooling/cli/src/add.rs index b61751b7dcee..4a8736ff059a 100644 --- a/tooling/cli/src/add.rs +++ b/tooling/cli/src/add.rs @@ -111,17 +111,26 @@ pub fn command(options: Options) -> Result<()> { } // 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 plugin_init = if metadata.builder { - "Builder::new().build()" - } else { - "init()" - }; - let replacement = format!("$1$2.plugin(tauri_plugin_{plugin_snake_case}::{plugin_init})$2"); - let out = re.replace(&contents, replacement); + 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())?; From b2c2765a535c60dd379c9e39af3e4d80dae4a23d Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 1 Feb 2024 07:57:35 -0300 Subject: [PATCH 7/7] typo [skip ci] --- .changes/cli-add-to-main-rs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/cli-add-to-main-rs.md b/.changes/cli-add-to-main-rs.md index bc7080a99e12..91b83091f94d 100644 --- a/.changes/cli-add-to-main-rs.md +++ b/.changes/cli-add-to-main-rs.md @@ -3,4 +3,4 @@ '@tauri-apps/cli': 'patch:feat' --- -Add plugin initalization rust code when using `tauri add` +Add plugin initialization rust code when using `tauri add`