Shell
访问系统 shell。允许您生成子进程并使用其默认应用程序管理文件和 URL。
支持的平台
- Windows
- Linux
- macOS
设置
这个插件要求 Rust 版本至少是 1.75
请安装 shell 插件。
使用项目的包管理器来添加依赖。
npm run tauri add shell
yarn run tauri add shell
pnpm tauri add shell
bun tauri add shell
cargo tauri add shell
-
在你的
Cargo.toml
文件中添加以下内容来安装 shell 插件。src-tauri/Cargo.toml [dependencies]tauri-plugin-shell = "2.0.0-beta"# 或者使用 Git:tauri-plugin-shell = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } -
修改
lib.rs
来初始化插件。src-tauri/src/lib.rs fn run() {tauri::Builder::default().plugin(tauri_plugin_shell::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定。
npm install @tauri-apps/plugin-shellyarn add @tauri-apps/plugin-shellpnpm add @tauri-apps/plugin-shellbun add @tauri-apps/plugin-shell
用法
Shell 插件有 JavaScript 和 Rust 两种版本。
import { Command } from '@tauri-apps/plugin-shell';
let result = await Command.create('exec-sh', [ '-c', "echo 'Hello World!'",]).execute();console.log(result);
use tauri_plugin_shell::ShellExt;
let shell = app_handle.shell();let output = tauri::async_runtime::block_on(async move { shell .command("echo") .args(["Hello from Rust!"]) .output() .await .unwrap()});if output.status.success() { println!("Result: {:?}", String::from_utf8(output.stdout));} else { println!("Exit with code: {}", output.status.code().unwrap());}
权限
默认情况下,所有插件命令都被阻止,无法访问。你必须在你的 capabilities
配置中定义一个权限列表。
更多信息请参见访问控制列表。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "shell:allow-execute", "allow": [ { "name": "exec-sh", "cmd": "sh", "args": [ "-c", { "validator": "\\S+" } ], "sidecar": false } ] } ]}
权限 | 描述 |
---|---|
shell:allow-execute | 在没有预先配置的作用域的情况下启用 execute 命令。 |
shell:deny-execute | 拒绝没有任何预配置范围的 execute 命令。 |
shell:allow-kill | 在没有预先配置的作用域的情况下启用 kill 命令。 |
shell:deny-kill | 拒绝没有任何预配置范围的 kill 命令。 |
shell:allow-open | 在没有预先配置的作用域的情况下启用 open 命令。 |
shell:deny-open | 拒绝没有任何预配置范围的 open 命令。 |
shell:allow-stdin-write | 在没有预先配置的作用域的情况下启用 stdin_write 命令。 |
shell:deny-stdin-write | 拒绝没有任何预配置范围的 stdin_write 命令。 |
© 2024 Tauri Contributors. CC-BY / MIT