1
1
---
2
2
title : Shell
3
- description : 访问系统 shell,使用默认应用程序管理文件和 URL,并生成子进程 。
3
+ description : 访问系统 shell 来生成子进程 。
4
4
plugin : shell
5
5
---
6
6
@@ -9,18 +9,23 @@ import Compatibility from '@components/plugins/Compatibility.astro';
9
9
10
10
import { Steps , Tabs , TabItem } from ' @astrojs/starlight/components' ;
11
11
import CommandTabs from ' @components/CommandTabs.astro' ;
12
+ import PluginPermissions from ' @components/PluginPermissions.astro' ;
12
13
13
14
<PluginLinks plugin = { frontmatter .plugin } />
14
15
15
- 访问系统 shell。允许您生成子进程并使用其默认应用程序管理文件和 URL 。
16
+ 访问系统 shell。允许你生成子进程 。
16
17
17
18
## 支持的平台
18
19
19
20
<Compatibility plugin = { frontmatter .plugin } />
20
21
22
+ ## 指定打开应用
23
+
24
+ 如果你正在查找 ` shell.open ` 接口的文档,请查看新的[ 指定打开应用插件] ( ../opener/ ) 。
25
+
21
26
## 设置
22
27
23
- 请安装 shell 插件 。
28
+ 请从安装 shell 插件开始 。
24
29
25
30
<Tabs >
26
31
<TabItem label = " 自动" >
@@ -32,38 +37,38 @@ import CommandTabs from '@components/CommandTabs.astro';
32
37
npm = " npm run tauri add shell"
33
38
yarn = " yarn run tauri add shell"
34
39
pnpm = " pnpm tauri add shell"
40
+ deno = " deno task tauri add shell"
35
41
bun = " bun tauri add shell"
36
42
cargo = " cargo tauri add shell"
37
43
/>
38
44
</TabItem >
39
45
<TabItem label = " 手动" >
40
46
<Steps >
41
- 1 . 在你的 ` Cargo.toml ` 文件中添加以下内容来安装 shell 插件 。
47
+ 1 . 在 ` src-tauri ` 目录下,运行下面的命令。将此插件添加到项目的 ` Cargo.toml ` 文件的 ` dependencies ` 字段中 。
42
48
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
+ ```
49
52
50
53
2 . 修改 ` lib.rs ` 来初始化插件。
51
54
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
+ ```
60
64
61
65
3 . 使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定。
62
66
63
67
<CommandTabs
64
68
npm = " npm install @tauri-apps/plugin-shell"
65
69
yarn = " yarn add @tauri-apps/plugin-shell"
66
70
pnpm = " pnpm add @tauri-apps/plugin-shell"
71
+ deno = " deno add npm:@tauri-apps/plugin-shell"
67
72
bun = " bun add @tauri-apps/plugin-shell"
68
73
/>
69
74
</Steps >
@@ -80,6 +85,8 @@ Shell 插件有 JavaScript 和 Rust 两种版本。
80
85
81
86
``` javascript
82
87
import { Command } from ' @tauri-apps/plugin-shell' ;
88
+ // 当设置 `"withGlobalTauri": true`, 你可以使用
89
+ // const { Command } = window.__TAURI__.shell;
83
90
84
91
let result = await Command .create (' exec-sh' , [
85
92
' -c' ,
@@ -96,17 +103,17 @@ use tauri_plugin_shell::ShellExt;
96
103
97
104
let shell = app_handle . shell ();
98
105
let 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 ()
105
112
});
106
113
if output . status. success () {
107
- println! (" Result: {:?}" , String :: from_utf8 (output . stdout));
114
+ println! (" Result: {:?}" , String :: from_utf8 (output . stdout));
108
115
} else {
109
- println! (" Exit with code: {}" , output . status. code (). unwrap ());
116
+ println! (" Exit with code: {}" , output . status. code (). unwrap ());
110
117
}
111
118
```
112
119
@@ -116,9 +123,9 @@ if output.status.success() {
116
123
117
124
## 权限
118
125
119
- 默认情况下,所有插件命令都被阻止,无法访问。你必须在你的 ` capabilities ` 配置中定义一个权限列表 。
126
+ 默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 ` capabilities ` 文件夹中的配置来启用它们 。
120
127
121
- 更多信息请参见 [ 访问控制列表 ] ( /zh-cn/reference/acl/ ) 。
128
+ 参见 [ 能力概览 ] ( /zh-cn/security/capabilities/ ) 以获取更多信息,以及插件的 [ 分步导览 ] ( /zh-cn/learn/security/using-plugin-permissions/ ) 来调整插件权限 。
122
129
123
130
``` json title="src-tauri/capabilities/default.json" ins={6-23}
124
131
{
@@ -147,13 +154,4 @@ if output.status.success() {
147
154
}
148
155
```
149
156
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