mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-19 16:41:34 +03:00
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:
parent
30be0e3057
commit
7fcc0bcd34
6
.changes/cli-add-to-main-rs.md
Normal file
6
.changes/cli-add-to-main-rs.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
'tauri-cli': 'patch:feat'
|
||||||
|
'@tauri-apps/cli': 'patch:feat'
|
||||||
|
---
|
||||||
|
|
||||||
|
Add plugin initialization rust code when using `tauri add`
|
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use colored::Colorize;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
helpers::{
|
helpers::{
|
||||||
@ -41,8 +43,10 @@ pub fn command(options: Options) -> Result<()> {
|
|||||||
let mut plugins = plugins();
|
let mut plugins = plugins();
|
||||||
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
|
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
|
||||||
|
|
||||||
|
let tauri_dir = tauri_dir();
|
||||||
|
|
||||||
let mut cargo = Command::new("cargo");
|
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() {
|
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
|
||||||
cargo
|
cargo
|
||||||
@ -106,45 +110,67 @@ pub fn command(options: Options) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let rust_code = if metadata.builder {
|
// add plugin init code to main.rs or lib.rs
|
||||||
if metadata.desktop_only {
|
let plugin_init_fn = if metadata.builder {
|
||||||
format!(
|
"Builder::new().build()"
|
||||||
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(())
|
|
||||||
}})
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
format!(
|
"init()"
|
||||||
r#"tauri::Builder::default().plugin(tauri_plugin_{plugin_snake_case}::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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user