11---
22title : Shell
3- description : 访问系统 shell,使用默认应用程序管理文件和 URL,并生成子进程 。
3+ description : 访问系统 shell 来生成子进程 。
44plugin : shell
55---
66
@@ -9,18 +9,23 @@ import Compatibility from '@components/plugins/Compatibility.astro';
99
1010import { Steps , Tabs , TabItem } from ' @astrojs/starlight/components' ;
1111import CommandTabs from ' @components/CommandTabs.astro' ;
12+ import PluginPermissions from ' @components/PluginPermissions.astro' ;
1213
1314<PluginLinks plugin = { frontmatter .plugin } />
1415
15- 访问系统 shell。允许您生成子进程并使用其默认应用程序管理文件和 URL 。
16+ 访问系统 shell。允许你生成子进程 。
1617
1718## 支持的平台
1819
1920<Compatibility plugin = { frontmatter .plugin } />
2021
22+ ## 指定打开应用
23+
24+ 如果你正在查找 ` shell.open ` 接口的文档,请查看新的[ 指定打开应用插件] ( ../opener/ ) 。
25+
2126## 设置
2227
23- 请安装 shell 插件 。
28+ 请从安装 shell 插件开始 。
2429
2530<Tabs >
2631 <TabItem label = " 自动" >
@@ -32,38 +37,38 @@ import CommandTabs from '@components/CommandTabs.astro';
3237 npm = " npm run tauri add shell"
3338 yarn = " yarn run tauri add shell"
3439 pnpm = " pnpm tauri add shell"
40+ deno = " deno task tauri add shell"
3541 bun = " bun tauri add shell"
3642 cargo = " cargo tauri add shell"
3743 />
3844 </TabItem >
3945 <TabItem label = " 手动" >
4046 <Steps >
41- 1 . 在你的 ` Cargo.toml ` 文件中添加以下内容来安装 shell 插件 。
47+ 1 . 在 ` src-tauri ` 目录下,运行下面的命令。将此插件添加到项目的 ` Cargo.toml ` 文件的 ` dependencies ` 字段中 。
4248
43- ``` toml title="src-tauri/Cargo.toml"
44- [dependencies ]
45- tauri-plugin-shell = " 2.0.0"
46- # 或者使用 Git:
47- tauri-plugin-shell = { git = " https://github.com/tauri-apps/plugins-workspace" , branch = " v2" }
48- ```
49+ ``` sh frame=none
50+ cargo add tauri-plugin-shell
51+ ```
4952
5053 2 . 修改 ` lib.rs ` 来初始化插件。
5154
52- ``` rust title="src-tauri/src/lib.rs" ins={3}
53- fn run () {
54- tauri :: Builder :: default ()
55- . plugin (tauri_plugin_shell :: init ())
56- . run (tauri :: generate_context! ())
57- . expect (" error while running tauri application" );
58- }
59- ```
55+ ``` rust title="src-tauri/src/lib.rs" ins={4}
56+ #[cfg_attr(mobile, tauri:: mobile_entry_point)]
57+ pub fn run () {
58+ tauri :: Builder :: default ()
59+ . plugin (tauri_plugin_shell :: init ())
60+ . run (tauri :: generate_context! ())
61+ . expect (" error while running tauri application" );
62+ }
63+ ```
6064
6165 3 . 使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定。
6266
6367 <CommandTabs
6468 npm = " npm install @tauri-apps/plugin-shell"
6569 yarn = " yarn add @tauri-apps/plugin-shell"
6670 pnpm = " pnpm add @tauri-apps/plugin-shell"
71+ deno = " deno add npm:@tauri-apps/plugin-shell"
6772 bun = " bun add @tauri-apps/plugin-shell"
6873 />
6974 </Steps >
@@ -80,6 +85,8 @@ Shell 插件有 JavaScript 和 Rust 两种版本。
8085
8186``` javascript
8287import { Command } from ' @tauri-apps/plugin-shell' ;
88+ // 当设置 `"withGlobalTauri": true`, 你可以使用
89+ // const { Command } = window.__TAURI__.shell;
8390
8491let result = await Command .create (' exec-sh' , [
8592 ' -c' ,
@@ -96,17 +103,17 @@ use tauri_plugin_shell::ShellExt;
96103
97104let shell = app_handle . shell ();
98105let output = tauri :: async_runtime :: block_on (async move {
99- shell
100- . command (" echo" )
101- . args ([" Hello from Rust!" ])
102- . output ()
103- . await
104- . unwrap ()
106+ shell
107+ . command (" echo" )
108+ . args ([" Hello from Rust!" ])
109+ . output ()
110+ . await
111+ . unwrap ()
105112});
106113if output . status. success () {
107- println! (" Result: {:?}" , String :: from_utf8 (output . stdout));
114+ println! (" Result: {:?}" , String :: from_utf8 (output . stdout));
108115} else {
109- println! (" Exit with code: {}" , output . status. code (). unwrap ());
116+ println! (" Exit with code: {}" , output . status. code (). unwrap ());
110117}
111118```
112119
@@ -116,9 +123,9 @@ if output.status.success() {
116123
117124## 权限
118125
119- 默认情况下,所有插件命令都被阻止,无法访问。你必须在你的 ` capabilities ` 配置中定义一个权限列表 。
126+ 默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 ` capabilities ` 文件夹中的配置来启用它们 。
120127
121- 更多信息请参见 [ 访问控制列表 ] ( /zh-cn/reference/acl/ ) 。
128+ 参见 [ 能力概览 ] ( /zh-cn/security/capabilities/ ) 以获取更多信息,以及插件的 [ 分步导览 ] ( /zh-cn/learn/security/using-plugin-permissions/ ) 来调整插件权限 。
122129
123130``` json title="src-tauri/capabilities/default.json" ins={6-23}
124131{
@@ -147,13 +154,4 @@ if output.status.success() {
147154}
148155```
149156
150- | 权限 | 描述 |
151- | ------------------------- | ----------------------------------------------------- |
152- | ` shell:allow-execute ` | 在没有预先配置的作用域的情况下启用 execute 命令。 |
153- | ` shell:deny-execute ` | 拒绝没有任何预配置范围的 execute 命令。 |
154- | ` shell:allow-kill ` | 在没有预先配置的作用域的情况下启用 kill 命令。 |
155- | ` shell:deny-kill ` | 拒绝没有任何预配置范围的 kill 命令。 |
156- | ` shell:allow-open ` | 在没有预先配置的作用域的情况下启用 open 命令。 |
157- | ` shell:deny-open ` | 拒绝没有任何预配置范围的 open 命令。 |
158- | ` shell:allow-stdin-write ` | 在没有预先配置的作用域的情况下启用 stdin_write 命令。 |
159- | ` shell:deny-stdin-write ` | 拒绝没有任何预配置范围的 stdin_write 命令。 |
157+ <PluginPermissions plugin = { frontmatter .plugin } />
0 commit comments