generate_command_file

Function generate_command_file 

Source
pub fn generate_command_file(options: TauriHelperOptions)
Expand description

Scans the crate for functions annotated with #[tauri::command] and optionally #[auto_collect_command], then generates a file containing a list of these functions in the tauri_commands_list folder.

This function is intended to be used in a build.rs script to automate the process of collecting Tauri commands during the build process. It should be called before invoking tauri_build::build() to ensure the command list is available for the Tauri application.

§Usage

Add the following to your build.rs file:

fn main() {
    // Generate the command file for Tauri
    tauri_helper::generate_command_file(tauri_helper::TauriHelperOptions::default());

    // Build the Tauri application
    tauri_build::build();
}

§Annotations

By default, this function looks for functions annotated with both #[tauri::command] and #[auto_collect_command]. For example:

#[tauri::command]
#[auto_collect_command]
fn my_command() {
    println!("Some Command")
}

These functions will be automatically collected and written to the tauri_commands_list folder.

If the collect_all option is set to true, the function will collect all #[tauri::command] functions, regardless of whether they have the #[auto_collect_command] attribute. However, this behavior is not recommended unless explicitly needed.

§Output

The generated file will be placed in the tauri_commands_list folder (relative to the crate root) inside of the target folder. The file will contain a list of all collected commands, which can be used by the Tauri application to register commands.

§Options

The behavior of this function can be customized using the TauriHelperOptions struct:

  • collect_all: When true, collects all #[tauri::command] functions, even if they lack the #[auto_collect_command] attribute. When false (default), only functions with both #[tauri::command] and #[auto_collect_command] are collected.

    Recommendation: Keep this option set to false to ensure explicit control over which commands are included in your Tauri application.

§Notes

  • This function should only be called once per build, typically in the build.rs script.
  • More options are coming such as a list of explicit files that need to be scanned only, if you have any more ideas, please open an issue on Github.

§Example

#[tauri::command]
#[auto_collect_command]
fn greet(name: String) -> String {
    format!("Hello, {}!", name)
}

#[tauri::command]
fn calculate_sum(a: i32, b: i32) -> i32 {
    a + b
}

With collect_all set to false (default), only greet will be collected. With collect_all set to true, both greet and calculate_sum will be collected.

§Panics

This function will panic if:

  • The tauri_commands_list folder cannot be created or written to.
  • No functions matching the criteria are found.

§Errors

If the function encounters an error during file generation, it will log the error and exit the build process with a non-zero status code.