25 个发布版本
0.2.21 | 2024年6月1日 |
---|---|
0.2.19 | 2023年11月2日 |
0.2.18 | 2023年7月13日 |
0.2.15 | 2023年1月30日 |
0.2.1 | 2021年6月28日 |
#10 in Cargo 插件
1,497 每月下载量
72KB
1.5K SLoC
cargo-temp
一个 CLI 工具,允许您在临时目录中创建一个带有已安装依赖项的新 Rust 项目。
安装
需要 Rust 1.51。
cargo install cargo-temp
如果您想使用 generate
功能,需要 Rust 1.61。
cargo install --all-features cargo-temp
或者
cargo install --features generate cargo-temp
用法
创建一个新临时项目
-
无额外依赖项
cargo-temp
-
具有多个依赖项
cargo-temp rand tokio
-
指定版本时
cargo-temp anyhow=1.0
使用 cargo 的比较要求
-
精确版本
cargo-temp anyhow==1.0.13
-
最大版本
cargo-temp anyhow=<1.0.2
仓库
您可以在 Cargo.toml
中添加仓库。
示例
-
HTTP
cargo-temp anyhow=https://github.com/dtolnay/anyhow
-
SSH
cargo-temp anyhow=ssh://[email protected]/dtolnay/anyhow.git
如果您在通过 SSH 添加依赖项时遇到一些问题,请参阅此链接: 支持 SSH Git URLs。如果不起作用,请提交问题。
- 不指定包名
cargo-temp https://github.com/dtolnay/anyhow.git
选择分支或修订版本
-
分支
cargo-temp anyhow=https://github.com/dtolnay/anyhow.git#branch=master
-
修订版本
cargo-temp anyhow=https://github.com/dtolnay/anyhow.git#rev=7e0f77a38
如果不指定分支或修订版本,cargo 将使用仓库的默认分支。
依赖项功能
您可以使用 +
为依赖项添加功能。
示例
-
具有功能的依赖项
cargo-temp serde+derive
-
具有版本和功能的依赖项
cargo-temp serde=1.0+derive
-
具有分支和功能的仓库
cargo-temp serde=https://github.com/serde-rs/serde#branch=master+derive
-
不指定包名
cargo-temp https://github.com/tokio-rs/tokio#branch=compat+io_std
如果您想添加多个功能,可以使用 +
,如下所示
cargo-temp serde=1.0+derive+alloc
功能
TO_DELETE 文件
如果您改变主意并决定保留项目,您只需删除TO_DELETE
文件,当shell或编辑器退出时,目录不会被删除。
Git工作树
您可以使用以下方式从当前仓库创建一个git工作树
cargo-temp --worktree
这将创建一个新工作树,位于当前HEAD处。您可以指定一个分支,如下所示
cargo-temp --worktree <branch>
退出shell(或您的编辑器)时,工作树将被清理。相当于git worktree prune
。
临时Git克隆
如果您想从Git仓库创建一个临时项目,可以使用与仓库URL一起使用的--git
选项
cargo-temp --git <url>
Cargo-temp默认将历史截断到最后一个提交。您可以在配置文件中更改此行为
- 您可以选择保留历史中的提交数量。
这将保留历史中的最后3个提交。git_repo_depth = 3
- 如果您不想截断历史,可以将
git_repo_depth
设置为false。git_repo_depth = false
git_repo_depth = true
与默认行为相同。
基准测试
如果您想使用criterion-rs
创建一个具有基准测试的临时项目,您可以使用带有可选基准文件名称的--bench
选项
cargo-temp --bench my_benchmark
生成的目录结构将如下所示
tmp-id/
├── benches
│ └── my_benchmark.rs
├── Cargo.toml
├── src
│ └── main.rs
└── TO_DELETE
这也会将这些行添加到项目的Cargo.toml
中
[dev-dependencies]
criterion = "*"
[profile.release]
debug = true
[[bench]]
name = "my_benchmark"
harness = false
最后,基准文件包含一些导入和一个Hello, world!
示例
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn criterion_benchmark(_c: &mut Criterion) {
println!("Hello, world!");
}
criterion_group!(
benches,
criterion_benchmark
);
criterion_main!(benches);
版本
如果您想为临时项目指定特定版本,可以使用--edition
选项
cargo-temp --edition 2015
可用的选项有
15
或2015
18
或2018
21
或2021
如果参数不匹配这些选项,则默认为最新版本。
项目名称
如果您想提供特定的项目名称,可以使用--name
选项
cargo-temp --name project
此名称将用作临时项目目录的后缀,例如tmp-wXyZ-project
。如果您决定保留项目,目录将被重命名为与项目名称匹配。
生成
使用cargo-generate
(当前使用版本为v0.16)从模板生成临时项目
例如,要使用此模板创建临时项目
cargo temp generate --git https://github.com/ashleygwilliams/wasm-pack-template
cargo-generate
CLI的大多数功能都直接提供,例外是
--init
- 此标志由cargo-temp用于将模板初始化为临时目录。--destination
- 由于我们已使用--init
,因此无法使用cargo-temp的此功能。--allow-commands
- 需要进一步调查以避免与cargo-temp的子进程冲突。
设置
配置文件位于 {CONFIG_DIR}/cargo-temp/config.toml
。第一次运行 cargo-temp
时,它将自动创建。我们同时使用 Linux 和 OSX 的 XDG 系统 和 Windows 的 已知文件夹系统。
欢迎信息
每次创建临时项目时,欢迎信息都会解释如何退出临时项目和如何退出时保留它。
默认启用此信息,可以使用 welcome_message
设置来禁用它
welcome_message = false # You can also remove this line from the config file
临时项目目录
创建临时项目时的路径。默认设置为缓存目录。
temporary_project_dir = "/home/name/.cache/cargo-temp/"
如果目录不存在,如果缺少其所有父组件,则将创建该目录。
Cargo 目标目录
Cargo 的目标目录覆盖。默认情况下,此设置未设置,如果已设置 CARGO_TARGET_DIR
环境变量,则将忽略该设置。
cargo_target_dir = "/home/name/repos/tmp"
编辑器
您可以使用 editor
通过 IDE 而不是 shell 来启动,并使用 editor_args
来提供其参数。默认情况下,这些设置未设置。
-
在 Unix 上运行 VS Code 的示例
editor = "/usr/bin/code" editor_args = [ "--wait", "--new-window" ]
-
在 Windows 上运行 VS Code 的示例
editor = "C:\\Program Files\\Microsoft VS Code\\Code.exe" editor_args = [ "--wait", "--new-window" ]
使用 VCS
默认情况下,cargo-temp 将使用您项目的默认 cargo VCS(通常是 git),您可以在配置文件中使用 vcs
选项来更改它。
vcs = "pijul"
可能的值是
- pijul
- fossil
- hg
- git
- none
将 --vcs
值原样传递给 cargo。
删除项目前的确认提示
cargo-temp 在退出 shell 时会自动删除临时项目,如果项目中存在标志文件 TO_DELETE
。如果您希望启用一个询问您是否要删除项目的提示,则可以将其添加到您的 config.toml
中。
prompt = true
子进程
您可以使用以下方式在临时 shell 中启动子进程
[[subprocess]]
command = "alacritty -e cargo watch -x run"
foreground = false
command
字段是像 echo Hello
这样的 shell 命令。foreground
字段允许在后台而不是前台运行程序。
其他设置
working_dir
覆盖默认工作目录。默认情况下,使用已创建的临时目录。keep_on_exit
用于在退出 shell 后保持进程活动。默认情况下,在 shell 退出时终止进程。此设置不适用于前台进程。
平台特定
Unix
stdout
和stderr
设置允许启用或禁用输出。对于后台进程,默认值为 false,对于前台进程,默认值为 true。stdin
设置不存在,因为它始终是禁用的。
Windows
inherit_handles
允许句柄继承 - 如果此参数为 true,则调用进程中的每个可继承句柄都将由新进程继承。如果参数为 false,则句柄不会继承(请参阅 CreateProcessW)。
示例
- Unix
[[subprocess]] command = "cargo run" foreground = true [[subprocess]] command = "firefox" foreground = false
- Windows
[[subprocess]] command = "cargo.exe run" foreground = true [[subprocess]] command = "firefox.exe" foreground = false
配置文件示例
# Print a welcome message when creating a new temporary project. Enabled by default.
welcome-message = true
# Path where the temporary projects are created. Cache directory by default.
temporary_project_dir = "/home/me/repos/temp"
# Cargo's target directory override. Optional.
cargo_target_dir = "/home/me/repos/target"
# Open the temporary project with VS Code. Optional.
editor = "/usr/bin/code"
editor_args = ["--wait", "--new-window"]
# Specify the VCS you want to use within the project. Default is `git`.
vcs = "pijul"
# Specify the path to the directory where you want to preserve a saved project. Optional (default is `temporary_project_dir`).
preserved_project_dir = "/home/me/projects/"
# Use a confirmation prompt before deleting a project
prompt = true
# Watch over changes in the project using `cargo watch`
[[subprocess]]
command = "cargo watch"
foreground = true
依赖关系
~6–23MB
~315K SLoC