Tauri cli详解

发布时间 2023-09-01 20:35:33作者: eagle.supper

1. 前言

这是一份Tauri cli中文版详细说明文档, 由philoenglish.com团队翻译.
原文位于鹏叔的技术博客空间 - Tauri cli详解, 要获得最近更新及反馈问题请访问原文!

2. info

infor命令用于显示有关Tauri依赖项和项目配置的信息

用法: cargo-tauri info [OPTIONS]


# for npm
npm run tauri info
# for yarn
yarn tauri info
# for pnpm
pnpm tauri info
# for Cargo
cargo tauri info

选项:

      --interactive  交互模式用于自动修复
  -v, --verbose...   开启详细日志
  -h, --help         打印帮助信息
  -V, --version      查询版本号

它可以用来显示了开发环境、Rust、Node.js 及其版本以及一些相关配置的简明信息列表。

当您需要快速概览应用程序时,此命令非常有用。当您请求帮助时,与我们分享此报告可能会很有用。

示例1: 查看项目配置信息


$ npm run tauri info

> xxx-web@1.0.0 tauri
> tauri info


[✔] Environment
    - OS: Windows 10.0.22000 X64
    ✔ WebView2: 114.0.1823.51
    ✔ MSVC: Visual Studio Build Tools 2017
    ✔ rustc: 1.68.0 (2c8cc3432 2023-03-06)
    ✔ Cargo: 1.68.0 (115f34552 2023-02-26)
    ✔ rustup: 1.25.2 (17db695f1 2023-02-01)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.16.1
    - npm: 9.5.1

[-] Packages
    - tauri [RUST]: 1.4.1
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: not installed!
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist/
    - devPath: http://localhost:4200/
    - framework: Angular
    - bundler: Webpack

示例2: 查看版本

$ npm run tauri info -- --version

> xxxx-web@1.0.0 tauri
> tauri info --version

tauri-cli-info 1.4.0

3. init

初始化Tauri项目

用法: cargo-tauri init [OPTIONS]


# for npm
npm run tauri init
# for yarn
yarn tauri init
# for pnpm
pnpm tauri init
# for Cargo
cargo tauri init

选项:

Options:
      --ci
          跳过输入提示
  -v, --verbose...
          开启详细日志
  -f, --force
          强制覆盖src-tauri文件夹
  -l, --log
          开启日志记录
  -d, --directory <DIRECTORY>
          设置init的目标目录 [默认值: /home/runner/work/tauri-docs/tauri-docs]
  -t, --tauri-path <TAURI_PATH>
          Tauri项目的保存路径(相对于当前工作目录)
  -A, --app-name <APP_NAME>
          Tauri应用程序的名称
  -W, --window-title <WINDOW_TITLE>
          Tauri应用程序的窗口标题
  -D, --dist-dir <DIST_DIR>
          Web资源文件的位置,相对于<project-dir>/src-tauri
  -P, --dev-path <DEV_PATH>
          开发服务器的Url
      --before-dev-command <BEFORE_DEV_COMMAND>
          在“tauri dev”启动之前运行的shell命令
      --before-build-command <BEFORE_BUILD_COMMAND>
          在“tauri build”开始之前运行shell命令
  -h, --help
          在控制台打印帮助信息
  -V, --version
          在控制台打印版本信息

示例: 打印版本信息


$npm run tauri init -- -V

> demo-web@1.0.0 tauri
> tauri init -V

tauri-cli-init 1.4.0

4. plugin init

初始化Tauri插件项目

用法: cargo-tauri plugin init [OPTIONS] --name <PLUGIN_NAME>


# for npm
npm run tauri plugin init
# for yarn
yarn tauri plugin init
# for pnpm
pnpm tauri plugin init
# for cargo
cargo tauri plugin init

选项:


Options:
  -n, --name <PLUGIN_NAME>       Tauri插件的名称
  -v, --verbose...               开启详细日志
      --api                      使用TypeScript API初始化Tauri插件
  -d, --directory <DIRECTORY>    设置init的目标目录 [默认: /home/runner/work/tauri-docs/tauri-docs]
  -t, --tauri-path <TAURI_PATH>  Tauri项目的路径 (相对于当前工作目录)
  -a, --author <AUTHOR>          作者名称
  -h, --help                     打印帮助信息
  -V, --version                  打印版本信息

示例: 打印版本信息

$npm run tauri plugin init -- -V

> demo-web@1.0.0 tauri
> tauri plugin init -V

tauri-cli-plugin-init 1.4.0

5. dev

用法: cargo-tauri dev [OPTIONS] [ARGS]...

# for npm
npm run tauri dev
# for yarn
yarn tauri dev
# for pnpm
pnpm tauri dev
# for cargo
cargo tauri dev

参数和选项:


参数:
  [ARGS]...  传递给运行程序的命令行参数。将“--”之后的参数传递给应用程序

Options:
  -r, --runner <RUNNER>           用于运行应用程序的二进制文件
  -v, --verbose...                开启详细日志
  -t, --target <TARGET>           针对triple目标进行构建 
  -f, --features [<FEATURES>...]  要激活的Cargo功能列表
  -e, --exit-on-panic             panic时退出
  -c, --config <CONFIG>           要与tauri.conf.JSON合并的JSON字符串或JSON文件路径
      --release                   在release模式下运行代码
      --no-watch                  禁用文件监视程序
      --no-dev-server             禁用静态文件的dev server
      --port <PORT>               为静态文件指定开发服务器的端口。默认值为1430也可以使用`TAURI_DEV_SERVER_PORT`环境变量
  -h, --help                      打印帮助信息
  -V, --version                   打印版本信息

此命令将在开发模式下打开WebView。它利用了src-tauri/tauri.conf.json文件中的build.devPath属性。
如果您在build.beforeDevCommand属性中输入了一个命令,那么这个命令将在dev命令之前执行。

TROUBLESHOOTING:
如果您没有使用build.beforeDevCommand,请确保您的build.devPath是正确的,如果使用的是开发服务器,请确保它在使用此命令之前已启动。

6. build

Tauri build

用法: cargo-tauri build [OPTIONS] [ARGS]...


# for npm
npm run tauri build
# for yarn
yarn tauri build
# for pnpm
pnpm tauri build
# for cargo
cargo tauri build

参数和选项:


参数:
  [ARGS]...
          传递给Runner的命令行参数

Options:
  -r, --runner <RUNNER>
          用于构建应用程序的二进制文件,默认为`cargo`

  -v, --verbose...
          开启详细日志

  -d, --debug
          调试标志

  -t, --target <TARGET>
          构建triple目标。
          对于通用macOS应用程序,它必须是“$rustc--print target list”或“universal apple darwin”输出的值之一。
          请注意,编译通用macOS应用程序需要同时安装“aarch64-apple-darwin”和“x86_64-apple-darwin”目标。

  -f, --features [<FEATURES>...]
          空格或逗号分隔的要激活的功能列表

  -b, --bundles [<BUNDLES>...]
          空格或逗号分隔的要打包的bundle列表。
          
          每个bundle必须是MacOS上的“deb”、“appimage”、“msi”、“app”或“dmg”中的一个,以及所有平台上的“updater”。如果指定了“none”,则将跳过绑定程序。
          
          请注意,“updater”bundle不是自动添加的,因此如果启用了updater,则必须指定它。

  -c, --config <CONFIG>
          要与tauri.conf.JSON合并的JSON字符串或JSON文件路径

      --ci
          跳过输入提示

  -h, --help
          打印帮助信息

  -V, --version
          打印版本信息

如果您使用--debug标志,此命令将打包您的应用程序,无论是在生产模式还是调试模式。它利用了src-tauri/tauri.conf.json文件中的build.distDir属性。
如果您在build.beforBuildCommand属性中设置了一个命令,则此命令将在build命令之前执行。

7. icon

为所有主要平台生成各种图标.

Tauri附带了一个基于其标志的默认图标集。当您分发应用程序时,这不是您想要的。为了纠正这种常见的情况,Tauri提供了图标命令,该命令将获取一个输入文件(默认为“./app-icon.png”),并创建各种平台所需的所有图标。

源图像文件要求:

  • png图片
  • 源图像必须为方形, 即长宽比一定要是1:1, 例如都是图片分辨率592*592

用法: cargo-tauri icon [OPTIONS] [INPUT]


# 通过npm调用
npm run tauri icon
# 通过yarn调用
yarn tauri icon
# 通过pnpm调用
pnpm tauri icon
# 通过cargo调用
cargo tauri icon

参数和选项:


Arguments:
  [INPUT]   源图标的路径(png,带透明度的1240x1240px)[默认值:./app icon.png]

Options:
  -o, --output <OUTPUT>  输出目录。默认值:tauri.conf.json文件同级的“icons”目录
  -v, --verbose...       开启详细日志
  -p, --png <PNG>        要生成的自定义PNG图标大小。设置后,不会生成默认图标
  -h, --help             打印帮助信息
  -V, --version          打印版本信息

有关更多信息,请查看完整的Tauri图标指南

8. completions

命令补全

用法: cargo-tauri completions [OPTIONS] --shell \<SHELL>


# 通过npm调用
npm run tauri completions
# 通过yarn调用
yarn tauri completions
# 通过pnpm调用
pnpm tauri completions
# 通过cargo调用
cargo tauri completions

选项:


Options:
  -s, --shell <SHELL>    要为其生成命令补全脚本的Shell类型。[可能的值:bash、elvish、fish、powershell、zsh] 
  -v, --verbose...       开启详细日志
  -o, --output <OUTPUT>  shell命令补全的输出文件。默认情况下,完成打印到控制台.
  -h, --help             打印帮助信息
  -V, --version          打印版本信息

Tauri CLI可以为Bash、Zsh、PowerShell和Fish生成shell补全。
以下是一些配置Bash、Zsh和PowerShell的说明。如果您遇到问题,请按照shell的说明进行操作。请注意,出于安全原因,建议在执行生成的完成脚本之前先进行检查。

8.1. Bash

生成Bash补全并移动到已知文件夹:

用法:


# 通过npm生成
npm run tauri completions -- --shell bash > tauri.sh
mv tauri.sh /usr/local/etc/bash_completion.d/tauri.bash

# 通过yarn生成
yarn tauri completions --shell bash > tauri.sh
mv tauri.sh /usr/local/etc/bash_completion.d/tauri.bash

# 通过pnpm生成
pnpm tauri completions --shell bash > tauri.sh
mv tauri.sh /usr/local/etc/bash_completion.d/tauri.bash


# 通过cargo生成
cargo tauri completions --shell bash > tauri.sh
mv tauri.sh /usr/local/etc/bash_completion.d/tauri.bash

通过在.bashrc中添加以下内容来加载completions脚本:


source /usr/local/etc/bash_completion.d/tauri.bash

8.2. Zsh

生成Zsh补全并移动到已知文件夹:

用法:



# 通过npm生成
npm run tauri completions -- --shell zsh > completions.zsh
mv completions.zsh $HOME/.completions/_tauri


# 通过yarn生成
yarn tauri completions --shell zsh > completions.zsh
mv completions.zsh $HOME/.completions/_tauri


# 通过pnpm生成
pnpm tauri completions --shell zsh > completions.zsh
mv completions.zsh $HOME/.completions/_tauri


# 通过cargo生成
cargo tauri completions --shell zsh > completions.zsh
mv completions.zsh $HOME/.completions/_tauri

使用fpath加载completions文件夹,并将以下内容添加到.zshrc:

fpath=(~/.completions $fpath)
autoload -U compinit

8.3. PowerShell

生成PowerShell补全并将其添加到$profile文件中,以便在所有会话上执行它:


# 通过npm生成
npm run tauri completions -- --shell powershell > ((Split-Path -Path $profile)+"\_tauri.ps1")
Add-Content -Path $profile -Value '& "$PSScriptRoot\_tauri.ps1"'

# 通过yarn生成
yarn tauri completions --shell powershell > ((Split-Path -Path $profile)+"\_tauri.ps1")
Add-Content -Path $profile -Value '& "$PSScriptRoot\_tauri.ps1"'


# 通过pnpm生成
pnpm tauri completions --shell powershell > ((Split-Path -Path $profile)+"\_tauri.ps1")
Add-Content -Path $profile -Value '& "$PSScriptRoot\_tauri.ps1"'


# 通过cargo生成
cargo tauri completions --shell powershell > ((Split-Path -Path $profile)+"\_tauri.ps1")
Add-Content -Path $profile -Value '& "$PSScriptRoot\_tauri.ps1"'

9. version

描述:
获取tauri版本信息

用法:


# 通过npm获取版本信息
npm run tauri -- --version
# 通过yarn获取版本信息
yarn tauri -- --version
# 通过pnpm获取版本信息
pnpm tauri -- --version
# 通过cargo获取版本信息
cargo tauri -- --version

10. 参考文档

Tauri Cli

使用Tauri自带命令生成各平台图标方法,超级简单,只需要一张png图片