#zng #ui #user-interface

app cargo-zng

Zng 项目管理器

17 个不稳定版本 (3 个破坏性更新)

0.4.7 2024 年 8 月 17 日
0.4.6 2024 年 8 月 15 日
0.4.5 2024 年 7 月 26 日
0.3.1 2024 年 6 月 22 日
0.1.2 2024 年 5 月 31 日

24GUI

Download history 208/week @ 2024-05-25 152/week @ 2024-06-01 477/week @ 2024-06-08 159/week @ 2024-06-15 202/week @ 2024-06-22 10/week @ 2024-06-29 418/week @ 2024-07-06 159/week @ 2024-07-13 99/week @ 2024-07-20 59/week @ 2024-07-27 3/week @ 2024-08-03 90/week @ 2024-08-10 146/week @ 2024-08-17

每月 371 次下载

Apache-2.0 OR MIT

240KB
4.5K SLoC

此 crate 是 zng 项目的组成部分。

Cargo 扩展用于 Zng 项目管理。从模板创建新项目,收集本地化字符串,打包应用程序以进行分发。

安装

cargo install cargo-zng

使用方法

命令概览

$ cargo zng --help

Zng project manager.

Usage: cargo zng <COMMAND>

Commands:
  fmt   Format code and macros
  new   New project from a Zng template repository
  l10n  Localization text scraper
  res   Build resources
  help  Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print version

fmt

使用 cargo fmt 格式化代码,并格式化 Zng 宏和一些其他大括号宏。

$ cargo zng fmt --help

Format code and macros

Runs cargo fmt and formats Zng macros

Usage: cargo zng fmt [OPTIONS]

Options:
      --check
          Only check if files are formatted

      --manifest-path <MANIFEST_PATH>
          Format the crate identified by Cargo.toml

  -p, --package <PACKAGE>
          Format the workspace crate identified by package name

  -f, --files <FILES>
          Format all files matched by glob

  -s, --stdin
          Format the stdin to the stdout

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

格式化程序支持 Zng 宏,并尝试格式化所有大括号宏的内容,例如 foo! { <contents> },如果内容无法按此方式格式化,则不进行修改。

IDE 集成

您可以将 Rust-Analyzer 配置为使用 cargo zng fmt 作为您的 IDE 格式化程序。

在 VsCode 中,将此添加到工作区配置 .vscode/settings.json

"rust-analyzer.rustfmt.overrideCommand": [
    "cargo",
    "zng",
    "fmt",
    "--stdin"
],

现在使用格式化上下文、操作和命令格式化 Zng 宏。

从 Zng 模板仓库初始化新的仓库。

$ cargo zng new --help

New project from a Zng template repository

Usage: cargo zng new [OPTIONS] [VALUE]...

Arguments:
  [VALUE]...
          Set template values by position

          The first value for all templates is the app name.

          EXAMPLE

          cargo zng new "My App!" | creates a "my-app" project.

          cargo zng new "my_app"  | creates a "my_app" project.

Options:
  -t, --template <TEMPLATE>
          Zng template

          Can be a .git URL or an `owner/repo` for a GitHub repository. Can also be an absolute path or `./path` to a local template directory.

          Use `#branch` to select a branch, that is `owner/repo#branch`.

          [default: zng-ui/zng-template]

  -s, --set [<SET>...]
          Set a template value

          Templates have a `.zng-template/keys` file that defines the possible options.

          EXAMPLE

          -s"key=value" -s"k2=v2"

  -k, --keys
          Show all possible values that can be set on the template

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Zng 项目生成器非常简单,它不使用任何模板引擎,仅对 UTF-8 文本文件中的 Rust 字符串进行替换。替换键是有效的 crate/type 名称,因此模板设计者可以像正常的 Rust 项目一样构建/检查它们的模板。

模板键编码它们提供的格式,以下是当前支持的关键案例

  • t-key-t — kebab-case (cleaned)
  • T-KEY-T — UPPER-KEBAB-CASE (cleaned)
  • t_key_t — snake_case (cleaned)
  • T_KEY_T — UPPER_SNAKE_CASE (cleaned)
  • T-Key-T — Train-Case (cleaned)
  • t.key.t — 小写
  • T.KEY.T — 大写
  • T.Key.T — 首字母大写
  • ttKeyTt — 驼峰命名法(清理后)
  • TtKeyTt — 帕斯卡命名法(清理后)
  • {{key}} — 不变
  • f-key-f — 清理后,其他不变
  • f-Key-f — 首字母大写(清理后)

清理后的值仅保留ASCII字母的第一个字符和ASCII字母数字,' ','-' 和 '_' 其他字符。

清理后的值在所有操作系统中的有效文件名。文件名中的值将自动清理。

实际的键由模板在 .zng-template/keys 文件中声明,它们是ASCII字母,且包含 >=3 个小写字母。

调用 cargo zng new --keys 以显示模板键的帮助。

默认模板有 3 个键

  • app — 应用程序名称,Cargo 包和crate名称由此派生。每个模板的第一个键必须是这个。
  • org — 在 zng::env::init 中用作 'organization' 值。
  • qualifier — 在 zng::env::init 中用作 'qualifier' 值。

对于示例输入 cargo zng new "My App!" "My Org" 模板代码

// file: src/t_app_t_init.rs

pub fn init_t_app_t() {
    println!("init t-app-t");
    zng::env::init("{{qualifier}}", "{{org}}", "{{app}}");
}

生成

// file: src/my_app_init.rs

pub fn init_my_app() {
    println!("init my-app");
    zng::env::init("", "My Org", "My App!");
}

有关模板的示例,请参阅 zng-ui/zng-template

忽略

.zng-template 目录不包括在最终的模板中,其他文件也可以通过 .zng-template/ignore 文件 忽略。ignore 文件使用与 .gitignore 相同的语法,路径相对于工作区根目录。

后置

如果存在 .zng-template/post,则在应用模板替换后执行。

如果存在 post/post.sh,则将其作为Bash脚本执行。尝试在 $ZR_SH,$PROGRAMFILES/Git/bin/bash.exe,bash,sh 中运行。

如果存在 post/Cargo.toml,则将其作为cargo二进制执行。以静默方式构建包含 dev/debug 配置文件的crate。

如果退出代码不是0,后置脚本或crate在工作区根目录运行,如果 cargo new 失败。

请注意,模板键也会在 post/** 文件上进行替换,因此代码可以通过模板键进行配置。.zng-template 目录被忽略,因此后置文件夹将不会出现在当前目录中,而是在 ZNG_TEMPLATE_POST_DIR 环境变量中。

l10n

本地化文本抓取器。

$ cargo zng l10n --help

Localization text scraper

See the docs for `l10n!` for more details about the expected format.

Usage: cargo zng l10n [OPTIONS]

Options:
  -i, --input <INPUT>
          Rust files glob or directory

          [default: ]

  -o, --output <OUTPUT>
          L10n resources dir

          [default: ]

  -p, --package <PACKAGE>
          Package to scrap and copy dependencies

          If set the --input and --output default is src/**.rs and l10n/

          [default: ]

      --manifest-path <MANIFEST_PATH>
          Path to Cargo.toml of crate to scrap and copy dependencies

          If set the --input and --output default to src/**.rs and l10n/

          [default: ]

      --no-deps
          Don't copy dependencies localization

          Use with --package or --manifest-path to not copy {dep-pkg}/l10n/*.ftl files

      --no-local
          Don't scrap `#.#.#-local` dependencies

          Use with --package or --manifest-path to not scrap local dependencies.

  -m, --macros <MACROS>
          Custom l10n macro names, comma separated

          [default: ]

      --pseudo <PSEUDO>
          Generate pseudo locale from dir/lang

          EXAMPLE

          "l10n/en" generates pseudo from "l10n/en.ftl" and "l10n/en/*.ftl"

          [default: ]

      --pseudo-m <PSEUDO_M>
          Generate pseudo mirrored locale

          [default: ]

      --pseudo-w <PSEUDO_W>
          Generate pseudo wide locale

          [default: ]

      --check
          Only verify that the generated files are the same

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

另请参阅有关预期格式的zng::l10n::l10n!文档以获取更多详细信息。

res

构建资源

$ cargo zng res --help

Build resources

Builds resources SOURCE to TARGET, delegates `.zr-{tool}` files to `cargo-zng-res-{tool}` executables and crates.

Usage: cargo zng res [OPTIONS] [SOURCE] [TARGET]

Arguments:
  [SOURCE]
          Resources source dir

          [default: res]

  [TARGET]
          Resources target dir

          This directory is wiped before each build.

          [default: target/res]

Options:
      --pack
          Copy all static files to the target dir

      --tool-dir <DIR>
          Search for `zng-res-{tool}` in this directory first

          [default: tools]

      --tools
          Prints help for all tools available

      --tool <TOOL>
          Prints the full help for a tool

      --tool-cache <TOOL_CACHE>
          Tools cache dir

          [default: target/res.cache]

      --recursion-limit <RECURSION_LIMIT>
          Number of build passes allowed before final

          [default: 32]

      --metadata <TOML_FILE>
          TOML file that that defines metadata uses by tools (ZR_APP, ZR_ORG, ..)

          This is only needed if the workspace has multiple bin crates and none or many set '[package.metadata.zng.about]'.

          See `zng::env::About` for more details.

      --metadata-dump
          Writes the metadata extracted the workspace or --metadata

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

此子命令可用于构建资源和软件包发布。它非常简单,您创建一个尽可能接近最终资源结构的资源目录树,并在其上放置特殊的.zr-{tool}文件,这些文件是调用cargo-zng-res-{tool}包或可执行文件的调用。

资源构建

资源构建遵循以下步骤

  • 清除TARGET目录。
  • 遍历SOURCE目录,在TARGET上创建匹配的目录,运行.zr-*工具请求。
  • 遍历TARGET目录,运行由前一个阶段生成的任何新的.zr-*请求(请求在工具运行后被删除)。
    • 这会一直重复,直到一个阶段没有找到任何.zr-*或达到--recursion_limit
  • 运行所有请求了zng-res::on-final=的请求的工具。

工具

您可以使用cargo zng res --tools查看所有可用工具的帮助。工具按以下顺序搜索

  • 如果存在位于tools/cargo-zng-res-{tool}的crate,则执行它(使用--quiet构建)。
  • 如果存在位于tools/cargo-zng-res的crate并且它有一个src/bin/{tool}.rs文件,则使用--bin {tool}执行它。
  • 如果工具是内置的,则执行它。
  • 如果同一目录中安装了cargo-zng-res-{tool}[.exe],则执行它。

工具创建

工具使用环境变量进行配置

  • ZR_SOURCE_DIR — 正在构建的资源目录。
  • ZR_TARGET_DIR — 资源正在构建到的目标目录。
  • ZR_CACHE_DIR — 用于特定请求的中间数据的目录。键基于源目录、目标目录、请求文件和请求文件内容。
  • ZR_WORKSPACE_DIR — 包含源目录的Cargo工作区。这也是工具的当前工作目录(current_dir)。
  • ZR_REQUEST — 调用工具的请求文件。
  • ZR_REQUEST_DD — 请求文件的父目录。
  • ZR_TARGET — 由请求文件名隐含的目标文件。也就是说,去除请求文件名中的 .zr-{tool} 和等效的目标子目录。
  • ZR_TARGET_DD — 目标文件的父目录。
  • ZR_FINAL — 如果工具请求 zng-res::on-final={args},则设置为参数。
  • ZR_HELP — 打印 cargo zng res --tools 的帮助文本。如果设置了此变量,则其他变量将不会设置。

在 Cargo 工作区中,zng::env::about 元数据也会从主要二进制包中提取。

  • ZR_APP — package.metadata.zng.about.app 或 package.name
  • ZR_ORG — package.metadata.zng.about.org 或 package.authors 的第一个
  • ZR_VERSION — package.version
  • ZR_DESCRIPTION — package.description
  • ZR_HOMEPAGE — package.homepage
  • ZR_LICENSE — package.license
  • ZR_PKG_NAME — package.name
  • ZR_PKG_AUTHORS — package.authors
  • ZR_CRATE_NAME — snake_case 中的 package.name
  • ZR_QUALIFIER — package.metadata.zng.about.qualifier

工具可以通过打印带有前缀 zng-res:: 的 stdout 来向资源构建器发出请求。当前支持的请求

  • zng-res::delegate — 继续搜索可以处理此请求的工具。
  • zng-res::warning={message} — 将 {message} 打印为警告。
  • zng-res::on-final={args} — 订阅在所有工具运行之后再次以 ZR_FINAL={args} 被调用。

如果工具失败,则打印整个 stderr,并且资源构建失败。

重建过程首先删除目标目录并重新运行所有工具。如果一个工具任务可能很慢,应该缓存结果。环境变量ZNG_RES_CACHE设置了一个目录的路径,该工具可以在此目录中存储针对此请求的中间文件。缓存目录以<SOURCE><TARGET><REQUEST>和请求文件内容为键。

工具工作目录(current_dir)始终设置为Cargo工作区根目录。如果<SOURCE>不在任何Cargo项目中,则打印警告,并将<SOURCE>用作工作目录。

内置工具

以下是提供的内置工具

$ cargo zng res --tools

.zr-copy @ cargo-zng
  Copy the file or dir

.zr-glob @ cargo-zng
  Copy all matches in place

.zr-rp @ cargo-zng
  Replace ${VAR|<file|!cmd} occurrences in the content

.zr-sh @ cargo-zng
  Run a bash script

.zr-shf @ cargo-zng
  Run a bash script on the final pass

.zr-warn @ cargo-zng
  Print a warning message

.zr-fail @ cargo-zng
  Print an error message and fail the build

.zr-apk @ cargo-zng
  Build an Android APK from a staging directory

call 'cargo zng res --help tool' to read full help from a tool

每个工具的扩展帮助

.zr-copy

$ cargo zng res --tool copy

.zr-copy</bold> @ cargo-zng
  Copy the file or dir

  The request file:
    source/foo.txt.zr-copy
     | # comment
     | path/bar.txt

  Copies `path/bar.txt` to:
    target/foo.txt

  Paths are relative to the Cargo workspace root.

.zr-glob

$ cargo zng res --tool glob

.zr-glob</bold> @ cargo-zng
  Copy all matches in place

  The request file:
    source/l10n/fluent-files.zr-glob
     | # localization dir
     | l10n
     | # only Fluent files
     | **/*.ftl
     | # except test locales
     | !:**/pseudo*

  Copies all '.ftl' not in a *pseudo* path to:
    target/l10n/

  The first path pattern is required and defines the entries that
  will be copied, an initial pattern with '**' flattens the matches.
  The path is relative to the Cargo workspace root.

  The subsequent patterns are optional and filter each file or dir selected by
  the first pattern. The paths are relative to each match, if it is a file
  the filters apply to the file name only, if it is a dir the filters apply to
  the dir and descendants.

  The glob pattern syntax is:

      ? — matches any single character.
      * — matches any (possibly empty) sequence of characters.
     ** — matches the current directory and arbitrary subdirectories.
    [c] — matches any character inside the brackets.
  [a-z] — matches any characters in the Unicode sequence.
   [!b] — negates the brackets match.

  And in filter patterns only:

  !:pattern — negates the entire pattern.

.zr-rp

$ cargo zng res --tool rp

.zr-rp</bold> @ cargo-zng
  Replace ${VAR|<file|!cmd} occurrences in the content

  The request file:
    source/greetings.txt.zr-rp
     | Thanks for using ${ZR_APP}!

  Writes the text content with ZR_APP replaced:
    target/greetings.txt
    | Thanks for using Foo App!

  The parameters syntax is ${VAR|!|<[:[case]][?else]}:

  ${VAR}          — Replaces with the env var value, or fails if it is not set.
  ${VAR:case}     — Replaces with the env var value, case converted.
  ${VAR:?else}    — If VAR is not set or is empty uses 'else' instead.

  ${<file.txt}    — Replaces with the 'file.txt' content.
                    Paths are relative to the workspace root.
  ${<file:case}   — Replaces with the 'file.txt' content, case converted.
  ${<file:?else}  — If file cannot be read or is empty uses 'else' instead.

  ${!cmd -h}      — Replaces with the stdout of the bash script line.
                    The script runs the same bash used by '.zr-sh'.
                    The script must be defined all in one line.
                    A separate bash instance is used for each occurrence.
                    The working directory is the workspace root.
  ${!cmd:case}    — Replaces with the stdout, case converted.
                    If the script contains ':' quote it with double quotes"
  $!{!cmd:?else}  — If script fails or ha no stdout, uses 'else' instead.

  $${VAR}         — Escapes $, replaces with '${VAR}'.

  The :case functions are:

  :k or :kebab  — kebab-case (cleaned)
  :K or :KEBAB  — UPPER-KEBAB-CASE (cleaned)
  :s or :snake  — snake_case (cleaned)
  :S or :SNAKE  — UPPER_SNAKE_CASE (cleaned)
  :l or :lower  — lower case
  :U or :UPPER  — UPPER CASE
  :T or :Title  — Title Case
  :c or :camel  — camelCase (cleaned)
  :P or :Pascal — PascalCase (cleaned)
  :Tr or :Train — Train-Case (cleaned)
  :           — Unchanged
  :clean      — Cleaned
  :f or :file — Sanitize file name

  Cleaned values only keep ascii alphabetic first char and ascii alphanumerics, ' ', '-' and '_' other chars.
  More then one case function can be used, separated by pipe ':T|f' converts to title case and sanitize for file name.


  The fallback(:?else) can have nested ${...} patterns.
  You can set both case and else: '${VAR:case?else}'.

  Variables:

  All env variables can be used, of particular use with this tool are:

  ZR_APP — package.metadata.zng.about.app or package.name
  ZR_ORG — package.metadata.zng.about.org or the first package.authors
  ZR_VERSION — package.version
  ZR_DESCRIPTION — package.description
  ZR_HOMEPAGE — package.homepage
  ZR_LICENSE — package.license
  ZR_PKG_NAME — package.name
  ZR_PKG_AUTHORS — package.authors
  ZR_CRATE_NAME — package.name in snake_case
  ZR_QUALIFIER — package.metadata.zng.about.qualifier

  See `zng::env::about` for more details about metadata vars.
  See the cargo-zng crate docs for a full list of ZR vars.

.zr-sh

$ cargo zng res --tool sh

.zr-sh</bold> @ cargo-zng
  Run a bash script

  Script is configured using environment variables (like other tools):

  ZR_SOURCE_DIR — Resources directory that is being build.
  ZR_TARGET_DIR — Target directory where resources are being built to.
  ZR_CACHE_DIR — Dir to use for intermediary data for the specific request.
  ZR_WORKSPACE_DIR — Cargo workspace that contains source dir. Also the working dir.
  ZR_REQUEST — Request file that called the tool (.zr-sh).
  ZR_REQUEST_DD — Parent dir of the request file.
  ZR_TARGET — Target file implied by the request file name.
  ZR_TARGET_DD — Parent dir of the target file.

  ZR_FINAL — Set if the script previously printed `zng-res::on-final={args}`.

  In a Cargo workspace the `zng::env::about` metadata is also set:

  ZR_APP — package.metadata.zng.about.app or package.name
  ZR_ORG — package.metadata.zng.about.org or the first package.authors
  ZR_VERSION — package.version
  ZR_DESCRIPTION — package.description
  ZR_HOMEPAGE — package.homepage
  ZR_LICENSE — package.license
  ZR_PKG_NAME — package.name
  ZR_PKG_AUTHORS — package.authors
  ZR_CRATE_NAME — package.name in snake_case
  ZR_QUALIFIER — package.metadata.zng.about.qualifier

  Script can make requests to the resource builder by printing to stdout.
  Current supported requests:

  zng-res::warning={msg}  Prints the `{msg}` as a warning after the script exits.
  zng-res::on-final={args}  Schedule second run with `ZR_FINAL={args}`, on final pass.

  If the script fails the entire stderr is printed and the resource build fails. Scripts run with
  `set -e` by default.

  Tries to run on $ZR_SH, $PROGRAMFILES/Git/bin/bash.exe, bash, sh.

.zr-shf

$ cargo zng res --tool shf

.zr-shf</bold> @ cargo-zng
  Run a bash script on the final pass

  Apart from running on final this tool behaves exactly like .zr-sh

.zr-warn

$ cargo zng res --tool warn

.zr-warn</bold> @ cargo-zng
  Print a warning message

  You can combine this with '.zr-rp' tool

  The request file:
    source/warn.zr-warn.zr-rp
     | ${ZR_APP}!

  Prints a warning with the value of ZR_APP

.zr-fail

$ cargo zng res --tool fail

.zr-fail</bold> @ cargo-zng
  Print an error message and fail the build

  The request file:
    some/dir/disallow.zr-fail.zr-rp
     | Don't copy ${ZR_REQUEST_DD} with a glob!

  Prints an error message and fails the build if copied

依赖项

~11–24MB
~344K SLoC