tauri-docs icon indicating copy to clipboard operation
tauri-docs copied to clipboard

Inlined plugins

Open ErickWei opened this issue 1 year ago • 8 comments

tauri version:2 I have a custom plugin(not a lib package,just a rs file with some commands), in the rs file,there is an init function like below:

// custom.rs
pub fn init() -> TauriPlugin<Wry> {
    Builder::new("custom")
        .invoke_handler(generate_handler![test_command])
        .build()
}

and inner the main.rs

// main.rs
fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_clipboard_manager::init())
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        // custom plugin
        .plugin(custom::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

but i can't invoke this command from frontend.


  await invoke(plugin::custom|test_command);

Error from frontend:Unhandled Promise Rejection: custom:test_command not allowed. Plugin not found.

and i can't add this custom command to capability too.

In the previous version tauri V1, I could invoke custom command using this way. so,what i should do for this in v2?

ErickWei avatar Sep 13 '24 04:09 ErickWei

@ErickWei Have you read this page?

I think, since it is just a rust file, and you've not "wrapped it" into a JS package, you have to invoke it like this:

import { invoke } from '@tauri-apps/api'
invoke('plugin:custom|test_command')

rmcsharry avatar Sep 13 '24 04:09 rmcsharry

@rmcsharry yes i did it like your reply,It worked fine in V1, but it won't work when I upgrade to V2-rc

import { invoke } from '@tauri-apps/api'
invoke('plugin:custom|test_command')

@ErickWei Have you read this page?

I think, since it is just a rust file, and you've not "wrapped it" into a JS package, you have to invoke it like this:

import { invoke } from '@tauri-apps/api'
invoke('plugin:custom|test_command')

ErickWei avatar Sep 13 '24 05:09 ErickWei

@ErickWei Apologies, I gave you the link to v1 docs.

Here is the v2 link.

// custom_commands.rs
#[tauri::command]
pub fn test_command() 
	// do whatever
}

// lib.rs
mod custom_commands;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![custom_commands::test_command])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

rmcsharry avatar Sep 13 '24 06:09 rmcsharry

Thanks @rmcsharry i think ur solution need to create a lib project of tauri plugin. but i don't want to do it like that way. i read amount of document and example project...but nothing is helpful

@ErickWei Apologies, I gave you the link to v1 docs.

Here is the v2 link.

// custom_commands.rs
#[tauri::command]
pub fn test_command() 
	// do whatever
}

// lib.rs
mod custom_commands;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![custom_commands::test_command])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

ErickWei avatar Sep 13 '24 07:09 ErickWei

The plugin documentation is not complete about this, thanks for your patience as we finalize last few pieces of v2 and then documentation will be updated.

In v2, inlined plugins have to be declared in the build script like this https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/build.rs#L12-L15 and here is the plugin code https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/src/menu_plugin.rs then you will have to add the permission to your capability file

amrbashir avatar Sep 13 '24 07:09 amrbashir

I will move the issue to tauri-docs as this is a documentation issue

amrbashir avatar Sep 13 '24 07:09 amrbashir

So do I have to declared command to the build.rs file? Is there any other more convenient way?

The way like v1 do is very convenient and easier to manage our code.

The plugin documentation is not complete about this, thanks for your patience as we finalize last few pieces of v2 and then documentation will be updated.

In v2, inlined plugins have to be declared in the build script like this https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/build.rs#L12-L15 and here is the plugin code https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/src/menu_plugin.rs then you will have to add the permission to your capability file

ErickWei avatar Sep 13 '24 08:09 ErickWei

and here is another report https://github.com/tauri-apps/tauri/issues/10075

ErickWei avatar Sep 13 '24 09:09 ErickWei