41 个版本 (21 个破坏性更新)
| 新 0.23.0 | 2024年8月21日 |
|---|---|
| 0.22.1 | 2024年6月16日 |
| 0.17.0 | 2024年3月19日 |
| 0.13.2 | 2023年12月10日 |
| 0.7.1 | 2022年10月11日 |
#32 在 命令行工具
每月下载量 256
225KB
5.5K SLoC
任务管理器
tk(任务管理器) 是一个工具,用于管理来自不同任务文件的任务,例如 Makefile、justfile、package.json、deno.jso、.fleet/run.json 等,并调用来自不同项目管理工具的任务,例如 Apache Maven、Gradle、Cargo 和 npm 等。

特性
- 列出不同任务文件中的任务:
tk --list - 调用任务:
tk task_name - 从运行器调用任务:
tk --runner=npm start - 在源之间同步任务:
tk --from=npm --to=fleet task1 task2 - 默认支持 .env:使用
tk --no-dotenv禁用 --doctor支持:检查系统运行任务时可能存在的问题- 执行命令行:使用正确的语言版本和 PATH 执行
tk -- node hello.js
如何开始使用?
- 通过 cargo 安装:
cargo install task-keeper - 从 GitHub 发布版 下载并安装
安装后,执行 tk --help 以获取使用说明。以下是一些命令
- 列出任务:
tk --list - 执行任务:
tk task_name
为什么是任务守护者?
抱歉,我在不同的任务文件和管理工具中迷失了方向,有时候我甚至都记不得如何运行它们。
- 查找任务:
Makefile,justfile,package.json,deno.json,Taskfile.yml,tasks.py,README.md等,需要检查太多的任务文件。 - 运行任务:
just taskName,npm run task_name,deno task task_name,composer run--script task_name等 - 检查过时的依赖:
mvn versions::display:-dependency:-updates,./gradlew dependencyUpdates,npm outdated等 - 构建项目:
mvn -DskipTests package,./gradlew assemble,cargo build等
差异太多,我想节省我的大脑和键盘,你知道 MacBook 键盘的价格标签是 400 美元以上。
任务运行器支持
- make(Makefile): https://gnu.ac.cn/software/make/manual/make.html, Modern Make 支持
- ant(build.xml): https://ant.apache.ac.cn/
- npm(package.json): https://docs.npmjs.net.cn/cli/v8/using-npm/scripts
- deno(deno.json): https://deno.land/manual/tools/task_runner
- composer(composer.json): https://composer.php.ac.cn/doc/articles/scripts.md
- just(justfile): https://github.com/casey/just
- Rakefile(rake): https://ruby.github.io/rake/
- invoke(tasks.py): https://www.pyinvoke.org/
- task(Taskfile.yml): https://github.com/go-task/task https://www.sobyte.net/post/2022-04/taskfile/
- cargo-make(Makefile.toml): https://github.com/sagiegurari/cargo-make
- JBang(jbang-catalog.json): https://www.jbang.dev/documentation/guide/latest/alias_catalogs.html
- proc(Procfile): https://devcenter.heroku.com/articles/procfile
- Bun Shell(Taskfile.ts): https://bun.net.cn/docs/runtime/shell
- markdown(README.md):支持 shell 代码块
- task.sh:纯 shell 脚本
- fleet(fleet/run.json): https://www.jetbrains.com/help/fleet/run-configurations.html#reference
- VS Code Tasks: https://vscode.js.cn/docs/editor/tasks
- zed Tasks: https://zed.rust-lang.net.cn/docs/tasks
- Rye: https://rye.pythonlang.cn/guide/pyproject/#projectscripts
- argc:基于 Bash 的命令运行器 https://github.com/sigoden/argc
- nur:基于 nu shell 的任务运行器 https://github.com/ddanier/nur
- cargo-xtask: https://github.com/linux-china/xtask-demo
- go-xtask: https://github.com/linux-china/xtask-go-demo
Bun Shell - Taskfile.ts
Bun Shell 是 Bun 提供的一个很好的特性,使得使用 JavaScript 和 TypeScript 进行 shell 脚本编写变得有趣。
现在Task Keeper支持使用Bun Shell的Taskfile.ts,你可以使用以下代码运行任务
import {$} from "bun";
export async function hello() {
await $`echo Hello World!`;
}
export async function list_js() {
await $`ls *.js`;
}
然后使用tk hello通过Bun Shell运行任务。
Fleet Run配置
现在支持命令类型
- 命令
- maven/gradle
- cargo
- go
- maven-run
- cargo-run
- docker-run
- python
- flask
- fastapi
- node
- php
有关详细信息,请参阅https://www.jetbrains.com/help/fleet/run-configurations.html
语言版本检测和PATH
Task Keeper使用.java-version、.node-version、.python-version文件来检测语言版本并将其绑定到本地安装的SDK。
为了使任务运行器能够顺利运行任务,Task Keeper将自动将以下目录添加到PATH
node-modules/.binvenv/bin或.venv/binvendor/binbin.bin
例如,如果你使用Python虚拟环境来管理你的项目,Task Keeper将自动将venv/bin添加到PATH,你不需要做任何事情。
hello:
python hello.py
提示:你可以使用双横线来运行带有语言检测和正确PATH的命令,例如tk -- mvn spring-boot:run。为了使生活更轻松,你可以使用alias e='tk --'创建别名,然后你可以运行e mvn spring-boot:run来运行你的项目。
包管理器支持
所有包管理器的常见任务
- init:通过管理器创建项目,如
mvn archetype:generate、npm init、cargo new xxx等 - install:安装所有依赖项
npm istall、dependency:resolve - compile:编译源代码,某些脚本语言不支持
- build:cargo:
tk build -- --release、maven:mvn -DskipTests clean package、npm:npm run build - start:启动项目
go run main.go - test:运行测试 npm:
npm test、maven:mvn test、cargo:cargo test - doc:生成文档
mvn javadoc:javadoc - deps:列出所有依赖项
- clean:清理构建工件,maven:
mvn clean、cargo:cargo clean - outdated:显示过时的依赖项
go list -u -m all - update:更新过时的依赖项
go get -u - 添加依赖项:
tk add dependency或tk -D add dependency或tk --runner=npm add dependency
注意: 如果包管理器的任务名称在任务运行器中,并且任务守护者将不会执行包管理器的命令。
Apache Maven
可用
Gradle
请为依赖项版本管理设置 gradle-versions-plugin。您可以通过Gradle初始化脚本将插件透明地添加到您运行的每个Gradle项目中。使用以下代码:
initscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:+'
}
}
allprojects {
apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
tasks.named("dependencyUpdates").configure {
// configure the task, for example wrt. resolution strategies
}
}
Sbt
请将 sbt-updates 和 DependencyTreePlugin 作为全局插件添加。使用以下代码:
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.3")
addDependencyTreePlugin
npm
可用
- 如果
npm-check命令可用,则npm-check -u将用作outdated任务。 yarn、pnpm、bun支持
Cargo
可用。请安装 cargo-tree、cargo-outdated。
Composer
可用
Bundler
可用
Go 模块
可用
CMake
仅支持 cmake-conan,并且需要 conanfile.txt。
默认构建目录是 cmake-build-debug,您可以通过 CMAKE_BINARY_DIR=_build tk build 来覆盖它。
Meson
可用
Swift
可用。请为 outdated 操作安装 swift-outdated。
Bazel
可用。
Python
以下工具可用:
- Rye:如果检测到
requirements.lock或[tool.rye] - uv:如果检测到
uv.lock或[tool.uv] - Poetry
- pipenv
- requirements.txt
Lein
可用。
请设置 antq 以处理过时的依赖项。使用以下代码:
{
:user
{:dependencies [[com.github.liquidz/antq "RELEASE"]]
:aliases {"outdated" ["run" "-m" "antq.core"]}
}
}
Mix 包管理器
可用。
Rebar3 包管理器
可用。
Dart 包管理器
可用。
Zig 构建系统
可用。
task - Taskfile.yml
如果您使用 JetBrains IDE 编辑 Taskfile.yml,请在 Taskfile.yml 顶部添加 $schema 注释以完成。
# $schema: https://taskfile.dev/schema.json
version: '3'
tasks:
hello:
cmds:
- echo 'Hello World from Task!'
silent: true
xtask for Rust 和 Golang
- cargo-xtask: https://github.com/linux-china/xtask-demo
- go-xtask: https://github.com/linux-china/xtask-go-demo
从 README.md 中的任务
任务守护者将解析 README.md 并提取以下代码块格式的任务:
```shell {#task_name}
curl https://httpbin.org/ip
```
现在仅支持 shell、sh、javascript 和 typescript。
使用 node 或 deno 运行 JavaScript/TypeScript
```javascript {#task_name .deno}
console.log("hello world");
```
任务选项/参数和全局选项
- 任务选项:
tk hello --name linux_china - 任务参数:
tk hello - Jackie,使用-分隔任务参数 - 命令的全局选项:
tk hello --name Jack -- --verbose,使用--双横线来分隔全局选项
待办事项
任务执行器
- jake(Jakefile): https://jakejs.com/docs-page.html#item-overview-jakefiles
- mask(maskfile.md): https://github.com/jacobdeichert/mask
包管理器
- realize(.realize.yaml): https://github.com/oxequa/realize
版本检测
任务管理器将检测版本配置文件并调整环境变量以运行任务。
Python
.python-version用于pyenv的版本管理。
Java
.java-version用于版本管理,值如下
- 17: 由https://adoptium.net/分发的OpenJDK
- 22.2.r17-grl: GraalVM
任务管理器将尝试从$HOME/.jbang/cache/jdks或$HOME/.sdkman/candidates/java/中查找Java。
.sdkmanrc支持,并自动设置HOME和PATH环境变量。请参阅https://sdkman.java.net.cn/usage#env以获取详细信息。
Node.js
.node-version用于Node.js版本管理,值如下
- 18: 匹配Node.js的主版本
- 16.16.0: 匹配Node.js的版本
任务管理器将尝试从$HOME/.nvm/versions/node或$HOME/.volta/tools/image/node中查找Node。
参考
- 《Gemfile和Gemfile.lock的终极指南》:https://blog.saeloun.com/2022/08/16/understanding_gemfile_and_gemfile_lock
- 你的Makefile是错误的:https://tech.davis-hansson.com/p/make/
- 用最精彩的示例学习Makefile:https://makefiletutorial.com/
- Taskfile:Makefile的现代替代品 - https://itnext.io/taskfile-a-modern-alternative-to-makefile-6b3f545f77bd
Markdown中的任务脚本演示
$ curl https://httpbin.org/get
$ curl -X POST https://httpbin.org/post
curl --silent https://httpbin.org/ip | jq '.origin'
curl https://httpbin.org/ip \
--user-agent "Task Keeper/0.1.0" \
--silent
curl https://httpbin.org/headers
let name: string = "linux_china";
console.log(name);
依赖关系
~13–27MB
~412K SLoC