Skip to content

Commit 6e4b21f

Browse files
qwer252dreyfus92
andauthored
i18n(zh-cn): translate 2 files (#3355)
Co-authored-by: Paul Valladares <[email protected]>
1 parent 4ff87a6 commit 6e4b21f

File tree

2 files changed

+376
-0
lines changed

2 files changed

+376
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
---
2+
title: 配置文件
3+
i18nReady: true
4+
---
5+
6+
import CommandTabs from '@components/CommandTabs.astro';
7+
8+
Tauri 是一个用于构建应用程序的工具包,因此可以使用许多文件来配置项目设置。你可能会遇到的一些常见文件有 `tauri.conf.json``package.json``Cargo.toml` 。我们将在本页面简要介绍每个文件,以帮助你确定需要修改哪些文件。
9+
10+
## Tauri 配置
11+
12+
Tauri 配置用于定义 Web 应用程序的来源、描述应用程序的元数据、配置打包方式、设置插件配置,并通过配置窗口、托盘图标、菜单等修改运行时行为。
13+
14+
Tauri 运行时和 Tauri CLI 会使用该文件。你可以配置构建设置(例如[`tauri build` 前运行][before-build-command][`tauri dev` 前运行][before-dev-command]的命令),设置应用的[名称](/reference/config/#productname)[版本](/reference/config/#version)[控制 Tauri 运行时][appconfig]以及[配置插件][configure plugins]
15+
16+
:::tip
17+
你可以在[配置参考]中找到所有选项。
18+
:::
19+
20+
### 支持的格式
21+
22+
Tauri 配置的默认格式为 JSON。你可以通过在 `Cargo.toml` 中的 `tauri``tauri-build` 依赖项中分别添加 `config-json5``config-toml` 功能标志来启用 JSON5 或 TOML 格式。
23+
24+
```toml title=Cargo.toml
25+
[build-dependencies]
26+
tauri-build = { version = "2.0.0", features = [ "config-json5" ] }
27+
28+
[dependencies]
29+
tauri = { version = "2.0.0", features = [ "config-json5" ] }
30+
```
31+
32+
所有格式的结构和值都是相同的,但是格式应与相应文件的格式一致:
33+
34+
```json5 title=tauri.conf.json or tauri.conf.json5
35+
{
36+
build: {
37+
devUrl: 'http://localhost:3000',
38+
// 启动开发服务器
39+
beforeDevCommand: 'npm run dev',
40+
},
41+
bundle: {
42+
active: true,
43+
icon: ['icons/app.png'],
44+
},
45+
app: {
46+
windows: [
47+
{
48+
title: 'MyApp',
49+
},
50+
],
51+
},
52+
plugins: {
53+
updater: {
54+
pubkey: 'updater pub key',
55+
endpoints: ['https://my.app.updater/{{target}}/{{current_version}}'],
56+
},
57+
},
58+
}
59+
```
60+
61+
```toml title=Tauri.toml
62+
[build]
63+
dev-url = "http://localhost:3000"
64+
# 启动开发服务器
65+
before-dev-command = "npm run dev"
66+
67+
[bundle]
68+
active = true
69+
icon = ["icons/app.png"]
70+
71+
[[app.windows]]
72+
title = "MyApp"
73+
74+
[plugins.updater]
75+
pubkey = "updater pub key"
76+
endpoints = ["https://my.app.updater/{{target}}/{{current_version}}"]
77+
```
78+
79+
请注意,JSON5 和 TOML 支持注释,并且 TOML 可以使用短横线连接命名法作为更符合惯用的配置名称。
80+
81+
### 平台特定配置
82+
83+
除了默认配置文件之外,Tauri 还可以从以下位置读取特定于平台的配置:
84+
85+
- `tauri.linux.conf.json``Tauri.linux.toml` (适用于 Linux)
86+
- `tauri.windows.conf.json``Tauri.windows.toml` (适用于 Windows)
87+
- `tauri.macos.conf.json``Tauri.macos.toml` (适用于 macOS)
88+
- `tauri.android.conf.json``Tauri.android.toml` (适用于 Android)
89+
- `tauri.ios.conf.json``Tauri.ios.toml` (适用于 iOS)
90+
91+
特定于平台的配置文件按照 [JSON Merge Patch(RFC 7396)] 规范与主配置对象合并。
92+
93+
例如,给定以下基本 `tauri.conf.json`
94+
95+
```json title=tauri.conf.json
96+
{
97+
"productName": "MyApp",
98+
"bundle": {
99+
"resources": ["./resources"]
100+
},
101+
"plugins": {
102+
"deep-link": {}
103+
}
104+
}
105+
```
106+
107+
以及给定的 `tauri.linux.conf.json`
108+
109+
```json title=tauri.linux.conf.json
110+
{
111+
"productName": "my-app",
112+
"bundle": {
113+
"resources": ["./linux-assets"]
114+
},
115+
"plugins": {
116+
"cli": {
117+
"description": "My app",
118+
"subcommands": {
119+
"update": {}
120+
}
121+
},
122+
"deep-link": {}
123+
}
124+
}
125+
```
126+
127+
Linux 的解析配置将是以下对象:
128+
129+
```json
130+
{
131+
"productName": "my-app",
132+
"bundle": {
133+
"resources": ["./linux-assets"]
134+
},
135+
"plugins": {
136+
"cli": {
137+
"description": "My app",
138+
"subcommands": {
139+
"update": {}
140+
}
141+
},
142+
"deep-link": {}
143+
}
144+
}
145+
```
146+
147+
此外,你还可以通过 CLI 提供要合并的配置,请参阅以下部分了解更多信息。
148+
149+
### 扩展配置
150+
151+
Tauri CLI 允许你在运行 `dev``android dev``ios dev``build``android build``ios build``bundle` 命令之一时扩展 Tauri 配置。扩展配置可以通过 `--config` 参数以原始 JSON 字符串或 JSON 文件路径的形式提供。Tauri 使用 [JSON Merge Patch (RFC 7396)] 规范将提供的配置值与最初解析的配置对象合并。
152+
153+
此机制可用于定义应用程序的多种风格,在配置应用程序包时具有更大的灵活性。
154+
155+
例如,要分发完全隔离的*测试版*应用程序,你可以使用此功能配置单独的应用程序名称和标识符:
156+
157+
```json title=src-tauri/tauri.beta.conf.json
158+
{
159+
"productName": "My App Beta",
160+
"identifier": "com.myorg.myappbeta"
161+
}
162+
```
163+
164+
为了分发这个单独的*测试版*应用程序,你需要在构建它时提供此配置文件:
165+
166+
<CommandTabs
167+
npm="npm run tauri build -- --config src-tauri/tauri.beta.conf.json"
168+
yarn="yarn tauri build --config src-tauri/tauri.beta.conf.json"
169+
pnpm="pnpm tauri build --config src-tauri/tauri.beta.conf.json"
170+
deno="deno task tauri build --config src-tauri/tauri.beta.conf.json"
171+
bun="bun tauri build --config src-tauri/tauri.beta.conf.json"
172+
cargo="cargo tauri build --config src-tauri/tauri.beta.conf.json"
173+
/>
174+
175+
## `Cargo.toml`
176+
177+
Cargo 的清单文件用于声明你的应用所依赖的 Rust 包、应用的元数据以及其他与 Rust 相关的功能。如果你不打算使用 Rust 为你的应用进行后端开发,那么你可能不会对其进行太多修改,但了解它的存在及其功能非常重要。
178+
179+
以下是一个 Tauri 项目的 Cargo.toml 文件示例:
180+
181+
```toml title=Cargo.toml
182+
[package]
183+
name = "app"
184+
version = "0.1.0"
185+
description = "A Tauri App"
186+
authors = ["you"]
187+
license = ""
188+
repository = ""
189+
default-run = "app"
190+
edition = "2021"
191+
rust-version = "1.57"
192+
193+
[build-dependencies]
194+
tauri-build = { version = "2.0.0" }
195+
196+
[dependencies]
197+
serde_json = "1.0"
198+
serde = { version = "1.0", features = ["derive"] }
199+
tauri = { version = "2.0.0", features = [ ] }
200+
```
201+
202+
最需要重视的部分是 `tauri-build``tauri` 依赖项。通常,它们都必须与 Tauri CLI 一样处于最新的次要版本,但这并非严格要求。如果你在尝试运行应用时遇到问题,请检查所有 Tauri 版本( `tauri``tauri-cli` )是否处于其各自次要版本的最新版本。
203+
204+
Cargo 版本号使用[语义化版本控制 (Semantic Versioning)] 。在 `src-tauri` 文件夹中运行 `cargo update` 将获取所有依赖项的最新可用语义兼容版本。例如,如果你指定 `2.0.0` 作为 `tauri-build` 的版本,Cargo 将检测并下载 `2.0.0.0` 版本,因为它是最新的可用语义兼容版本。每当引入重大变更时,Tauri 都会更新主版本号,这意味着你始终能够安全地升级到最新的次版本和补丁版本,而不必担心代码被破坏。
205+
206+
如果你想使用特定的 crate 版本,你可以通过在依赖项的版本号前面添加 `=` 来使用精确的版本:
207+
208+
```
209+
tauri-build = { version = "=2.0.0" }
210+
```
211+
212+
另外需要注意的是 `tauri` 依赖项中的 `features=[]` 部分。运行 `tauri dev``tauri build` 会根据你的 Tauri 配置自动管理项目中需要启用的功能。有关 `tauri` 功能开关的更多信息,请参阅[文档][tauri Cargo features]
213+
214+
构建应用程序时会生成一个 `Cargo.lock `文件。此文件主要用于确保在开发过程中跨机器使用相同的依赖项(类似于 Node.js 中的 `yarn.lock``pnpm-lock.yaml``package-lock.json` )。建议将此文件提交到你的源码仓库,以获得一致的构建。
215+
216+
要了解有关 Cargo 清单文件的更多信息,请参阅[官方文档][cargo-manifest]
217+
218+
## `package.json`
219+
220+
这是 Node.js 使用的包文件。如果你的 Tauri 应用的前端是使用基于 Node.js 的技术(例如 `npm``yarn``pnpm` )开发的,那么此文件用于配置前端依赖项和脚本。
221+
222+
Tauri 项目的 `package.json` 文件基本示例可能看起来像这样:
223+
224+
```json title=package.json
225+
{
226+
"scripts": {
227+
"dev": "command to start your app development mode",
228+
"build": "command to build your app frontend",
229+
"tauri": "tauri"
230+
},
231+
"dependencies": {
232+
"@tauri-apps/api": "^2.0.0.0",
233+
"@tauri-apps/cli": "^2.0.0.0"
234+
}
235+
}
236+
```
237+
238+
通常使用 `"scripts"` 部分来存储用于启动和构建 Tauri 应用程序前端的命令。上面的 `package.json` 文件指定了 `dev` 命令(你可以使用 `yarn dev``npm run dev` 来运行它来启动前端框架),以及 `build` 命令(你可以使用 `yarn build``npm run build` 来运行它来构建前端的 Web 资源,这些资源将被 Tauri 添加到生产环境中)。使用这些脚本最便捷的方式是通过 Tauri 配置的 [beforeDevCommand][before-dev-command][beforeBuildCommand][before-build-command] 钩子将它们连接到 Tauri CLI:
239+
240+
```json title=tauri.conf.json
241+
{
242+
"build": {
243+
"beforeDevCommand": "yarn dev",
244+
"beforeBuildCommand": "yarn build"
245+
}
246+
}
247+
```
248+
249+
:::note
250+
仅在使用 `npm` 时才需要 `"tauri"` 脚本
251+
:::
252+
253+
依赖项对象指明在运行 `yarn``pnpm install``npm install` (在本例中为 Tauri CLI 和 API)时 Node.js 应该下载哪些依赖项。
254+
255+
除了 `package.json` 文件之外,你还可以看到 `yarn.lock``pnpm-lock.yaml``package-lock.json` 文件。这些文件有助于确保你稍后下载依赖项时获得与开发期间使用的完全相同的版本(类似于 Rust 中的 `Cargo.lock` )。
256+
257+
要了解有关 `package.json` 文件格式的更多信息,请参阅[官方文档][npm-package]
258+
259+
[配置参考]: /reference/config/
260+
[before-dev-command]: /reference/config/#beforedevcommand-1
261+
[before-build-command]: /reference/config/#beforebuildcommand
262+
[appconfig]: /reference/config/#appconfig
263+
[configure plugins]: /reference/config/#plugins
264+
[语义化版本控制 (Semantic Versioning)]: https://semver.org
265+
[cargo-manifest]: https://doc.rust-lang.org/cargo/reference/manifest.html
266+
[npm-package]: https://docs.npmjs.com/cli/v8/configuring-npm/package-json
267+
[tauri Cargo features]: https://docs.rs/tauri/2.0.0/tauri/#cargo-features
268+
[JSON Merge Patch (RFC 7396)]: https://datatracker.ietf.org/doc/html/rfc7396
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 命令作用域
3+
sidebar:
4+
order: 3
5+
i18nReady: true
6+
---
7+
8+
作用域(scope)是定义 Tauri 命令允许(禁止)行为的细致的方法。
9+
10+
作用域分为 `允许(allow)` 作用域或 `禁止(deny)` 作用域,其中禁止作用域始终优先于允许作用域。
11+
12+
作用域类型必须是任意 [`serde`](https://docs.rs/serde/latest/serde/) 可序列化类型。这些类型通常特定于插件。对于在 Tauri 应用程序中实现的作用域命令,作用域类型需要在应用程序中定义,然后在命令实现中强制执行。
13+
14+
例如, [`Fs`](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/fs) 插件可以让你使用作用域来允许或拒绝某些目录和文件,[`http`](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/http) 插件使用作用域来筛选允许访问的 URL。
15+
16+
作用域传递给命令,并由命令本身实现处理或正确执行。
17+
18+
:::caution
19+
20+
命令开发人员需要确保不存在任何可能的作用域绕过。作用域验证的实施应接受审核,以确保其正确性。
21+
22+
:::
23+
24+
## 示例
25+
26+
这些示例取自 [`Fs`](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/fs) 插件权限:
27+
28+
此插件中所有命令的作用域类型都是字符串,其中包含一个 [`glob`](https://docs.rs/glob/latest/glob/) 兼容路径。
29+
30+
```toml title="plugins/fs/permissions/autogenerated/base-directories/applocaldata.toml"
31+
[[permission]]
32+
identifier = "scope-applocaldata-recursive"
33+
description = '''
34+
这个作用域递归访问整个 `$APPLOCALDATA` 文件夹,
35+
包括子文件夹和文件。
36+
'''
37+
38+
[[permission.scope.allow]]
39+
path = "$APPLOCALDATA/**"
40+
```
41+
42+
```toml title="plugins/fs/permissions/deny-webview-data.toml"
43+
[[permission]]
44+
identifier = "deny-webview-data-linux"
45+
description = '''
46+
这将禁止对 Linux 上 `$APPLOCALDATA` 文件夹的访问,
47+
因为那里存储了 webview 数据和配置信息。
48+
允许对其访问可能会导致敏感信息泄漏,应谨慎考虑。
49+
'''
50+
platforms = ["linux"]
51+
52+
[[scope.deny]]
53+
path = "$APPLOCALDATA/**"
54+
55+
[[permission]]
56+
identifier = "deny-webview-data-windows"
57+
description = '''
58+
这将禁止对 windows 上 `$APPLOCALDATA/EBWebView`
59+
文件夹的访问,因为那里存储了 webview 数据和配置信息。
60+
允许对其访问可能会导致敏感信息泄漏,应谨慎考虑。
61+
'''
62+
platforms = ["windows"]
63+
64+
[[scope.deny]]
65+
path = "$APPLOCALDATA/EBWebView/**"
66+
```
67+
68+
上述作用域可用于允许访问 `APPLOCALDATA` 文件夹,同时阻止访问 Windows 上的包含敏感 webview 数据的 `EBWebView` 子文件夹。
69+
70+
这些可以合并成一个集合,从而减少重复配置,并使任何查看应用程序配置的人都更容易理解。
71+
72+
首先,将拒绝作用域合并到 `deny-default` 中:
73+
74+
```toml title="plugins/fs/permissions/deny-default.toml"
75+
[[set]]
76+
identifier = "deny-default"
77+
description = '''
78+
默认情况下这将禁止访问 Tauri 相关的危险文件和文件夹。
79+
'''
80+
permissions = ["deny-webview-data-linux", "deny-webview-data-windows"]
81+
```
82+
83+
随后,将拒绝和允许作用域合并:
84+
85+
```toml
86+
[[set]]
87+
identifier = "scope-applocaldata-reasonable"
88+
description = '''
89+
此作用域集合允许非 Linux 系统下访问 `APPLOCALDATA` 文件夹及其子文件夹,
90+
而在 Windows 上默认拒绝访问危险的 Tauri 相关文件和文件夹。
91+
'''
92+
permissions = ["scope-applocaldata-recursive", "deny-default"]
93+
```
94+
95+
这些作用域可以通过扩展插件的全局作用域用于所有命令,或者当它们与权限内启用的命令结合使用时仅用于选定的命令。
96+
97+
`APPLOCALDATA` 中的文件进行合理的只读文件访问可能如下所示:
98+
99+
```toml
100+
[[set]]
101+
identifier = "read-files-applocaldata"
102+
description = '''
103+
此作用域集合允许非 Linux 系统下访问 `APPLOCALDATA` 文件夹及其子文件夹,
104+
而在 Windows 上默认拒绝访问危险的 Tauri 相关文件和文件夹。
105+
permissions = ["scope-applocaldata-reasonable", "allow-read-file"]
106+
```
107+
108+
这些示例仅突出了作用域本身的功能。每个插件或应用程序开发者都需要根据自己的用例考虑合理的作用域组合。

0 commit comments

Comments
 (0)