22 个稳定版本
2024.504.1814 | 2024年5月4日 |
---|---|
2024.422.214 | 2024年4月22日 |
2024.330.139 | 2024年3月30日 |
2023.603.904 |
|
0.5.2 |
|
#43 in 构建工具
6,328 月下载量
84KB
641 行
cargo-auto
使用 Rust 语言编写的自动化任务,用于 Rust 项目的流程
版本:2024.504.1814 日期:2024-05-04 作者: bestia.dev 仓库: GitHub
cargo-auto 是 automation_tasks_rs 项目的一部分
标签:#maintained #ready-for-use #rustlang #automation #workflow
我的 GitHub 项目更像是教程而不是成品: bestia-dev 教程。
我推荐使用 CRUSTDE - 容器化的 Rust 开发环境 在 Linux 上编写 Rust 项目,与您的系统隔离。
试试看
首先,我们将使用 cargo-auto
创建一个类似于 cargo new
的新空 CLI Rust 项目,但具有更完整的项目结构。
cargo install cargo-auto
cargo auto new_cli my_hello_project
cd my_hello_project
cargo auto
# it lists all the prepared automation tasks
# try a few
cargo auto build
cargo auto release
cargo auto doc
cargo auto test
我们还可以将 自动化任务
添加到现有的 Rust 项目中。在您的 Rust 项目目录(包含 Cargo.toml
的目录)中运行
cargo auto new_auto_for_cli
cargo auto
# it lists all the prepared automation tasks
# try to build
cargo auto build
恭喜!您已经使用 cargo-auto
。就这么简单。
现在您可以修改任务以满足您的需求。它全部都是 Rust 语言。
动机
货物是一个构建Rust项目的强大工具。它拥有所有基本功能:cargo build
、cargo build --release
、cargo fmt
、cargo test
、cargo doc
...
但有时我们需要做更多的事情,如复制一些文件、发布到FTP或输入长命令。这些重复性任务必须自动化。
任务自动化使工作更简单、更快,并简化工作流程,同时提高工作流程的一致性和准确性。
这也被称为“工作流程自动化”。
有各种各样的构建系统和任务执行器:make
、cmake
、shell scripts
、cargo-xtask
、cargo-make
、cargo-task
、cargo-script
、cargo-run-script
、runner
、python scripts
、powershell scripts
、cmd prompt scripts
...
遗憾的是,Rust社区目前还没有标准。
我希望有一种类似于build.rs的东西,这样我就可以用纯Rust编写我的“任务”,我不想学习另一种语法奇怪且难以调试的元语言。因此,我会创建一个非常简单、容易、像Rust一样、可扩展的东西。
cargo auto 子命令
命令 cargo install cargo-auto
将向cargo添加一个新的子命令
cargo auto
这个二进制文件非常简单。它只有一个简单的依赖项:lazy_static
。
该二进制文件仅读取CLI参数,并用它们运行 automation_tasks_rs
二进制文件。如果需要,它首先编译 automation_tasks_rs
。cargo-auto
的源代码流程简单、注释齐全、易于审计。
源代码在 GitHub 上,采用MIT开源许可。
bash 自动补全
借助dev_bestia_cargo_completion包的帮助,cargo
和 cargo auto
获得了bash自动补全功能。试试看!
cargo auto new_cli
我非常喜欢Rust中的命令 cargo new project_name
。它创建了一个超级简单的Rust Hello项目,可以立即构建和运行。但这个例子太简单了。它缺乏严肃CLI程序的基本文件结构。
我为Rust CLI项目编写了一个有偏见的模板。它很容易运行
cargo auto new_cli project_name
# then
cd project_name
cargo auto build
# then follow detailed instructions
cargo auto new_wasm
我为浏览器中的简单Rust WASM项目编写了一个有偏见的模板。它与new_cli模板非常相似,但针对WASM。
它很容易运行
cargo auto new_wasm project_name github_owner_or_organization web_server_domain server_username
# then
cd project_name
cargo auto build
# then follow detailed instructions
cargo auto new_pwa_wasm
我为浏览器中的简单Rust PWA-WASM项目编写了一个有偏见的模板。它与new_cli模板非常相似,但针对WASM。它添加了PWA标准功能,以便作为离线应用程序运行。
模板需要使用 icon512x512.png
文件来显示图标。如果您没有自己的图标,可以使用默认图标。
它很容易运行
curl -L https://github.com/automation-tasks-rs/cargo_auto_template_new_pwa_wasm/raw/main/icon512x512.png --output icon512x512.png
cargo auto new_pwa_wasm project_name github_owner_or_organization web_server_domain server_username
# then
cd project_name
cargo auto build
# then follow detailed instructions
使用 Rust 进行脚本编写
Rust 是一种编译型语言。它实际上并不是一种脚本或解释型语言。但小型项目的编译速度非常快,可以忽略不计。后续调用将使用已编译的二进制文件,因此速度会更快。
此工具 cargo-auto
旨在用于 Rust 项目,这意味着所有 Rust 基础设施已经就绪。
automation_tasks_rs Rust 子项目
命令 cargo auto new_auto_for_cli
将在您的 Rust 项目
内部创建一个新的 Rust 子项目 automation_tasks_rs
。它不会干扰主 Rust 项目。此目录将作为主项目的一部分添加到 git 提交中并推送到远程仓库。它有自己的 .gitignore
文件,以避免将其目标目录提交到版本控制。automation_tasks_rs
辅助项目包含用 Rust 代码编写的用户定义的任务。这是您的任务。此辅助项目应从 automation_tasks_rs
目录开始在一个新编辑器中打开。它不与主项目共享依赖项。它是完全独立和独立的。
您可以编辑它并添加自己的依赖项和 Rust 代码。无限制。表达自由。
现在这是您的代码、您的任务以及您的辅助 Rust 项目!
因为只有您知道您想自动化什么以及如何操作。
永远不要在 Rust 代码中写入秘密、密码、短语或令牌,因为这样它们就会被推送到 GitHub,整个世界在下一秒就可以阅读!基本示例(大多数有用的函数已经存在)
/// match arguments and call tasks functions
fn match_arguments_and_call_tasks(mut args: std::env::Args){
// the first argument is the user defined task: (no argument for help), build, release,...
let arg_1 = args.next();
match arg_1 {
None => print_help(),
Some(task) => {
println!("Running auto task: {}", &task);
if &task == "build"{
task_build();
} else if &task == "release" {
task_release();
} else if &task == "doc" {
task_doc();
} else {
println!("Task {} is unknown.", &task);
print_help();
}
}
}
}
/// write a comprehensible help for user defined tasks
fn print_help() {
println!(r#"
User defined tasks in automation_tasks_rs:
cargo auto build - builds the crate in debug mode
cargo auto release - builds the crate in release mode
cargo auto docs - builds the docs
"#);
}
// region: tasks
/// cargo build
fn task_build() {
run_shell_command("cargo fmt");
run_shell_command("cargo build");
}
/// cargo build --release
fn task_release() {
run_shell_command("cargo fmt");
run_shell_command("cargo build --release");
}
/// cargo doc, then copies to /docs/ folder, because this is a github standard folder
fn task_doc() {
run_shell_command("cargo doc --no-deps --document-private-items");
// copy target/doc into docs/ because it is github standard
run_shell_command("rsync -a --info=progress2 --delete-after target/doc/ docs/");
// Create simple index.html file in docs directory
run_shell_command(&format!(
"printf \"<meta http-equiv=\\\"refresh\\\" content=\\\"0; url={}/index.html\\\" />\\n\" > docs/index.html",
cargo_toml.package_name().replace("-","_")
));
run_shell_command("cargo fmt");
}
// endregion: tasks
更复杂的任务
您可以使用 Rust 语言编写更复杂的任务。
例如,在这个项目中,我使用自动化来创建 GitHub 发布版本:[https://github.com/automation-tasks-rs/dropbox_backup_to_external_disk](https://github.com/automation-tasks-rs/dropbox_backup_to_external_disk)
这是一个相当复杂的包含更多子项目的工作空间
[https://github.com/automation-tasks-rs/cargo_crev_reviews_workspace](https://github.com/automation-tasks-rs/cargo_crev_reviews_workspace)
您的想象力是没有止境的。如果您编写了一些可能有助于其他开发者的内容,请与我分享,我将在这里添加它。
开发细节
在单独的 md 文件中阅读开发细节
DEVELOPMENT.md
发布变更日志
在单独的 md 文件中阅读变更日志
RELEASES.md
TODO
近期内没有太多大事情。
开源免费,就像啤酒一样
我的开源项目免费,就像啤酒一样(MIT 许可证)。
我热爱编程。
但我也需要喝酒。如果您觉得我的项目和教程有帮助,请通过向我的 [https://paypal.me/LucianoBestia](https://paypal.me/LucianoBestia) 捐款来买我一杯啤酒。
您知道您当地酒吧啤酒的价格吧 ;-)
这样我就可以为您的健康喝一杯免费啤酒了 :-)
[https://translate.google.com/?hl=en&sl=sl&tl=en&text=Na%20zdravje&op=translate](https://translate.google.com/?hl=en&sl=sl&tl=en&text=Na%20zdravje&op=translate) Alla salute! Prost! Nazdravlje! 🍻
//bestia.dev
//github.com/automation-tasks-rs
//bestiadev.substack.com
//youtube.com/@bestia-dev-tutorials
依赖项
~17–31MB
~583K SLoC