name: mise-task-configuration user-invocable: false description: 在mise.toml中定义和配置Mise任务时使用。覆盖任务定义、依赖关系、文件任务和并行执行。 allowed-tools:
- Read
- Write
- Edit
- Bash
- Grep
- Glob
Mise - 任务配置
在Mise中定义和管理任务,用于构建自动化、测试和开发工作流。
基本任务定义
简单的TOML任务
# mise.toml
[tasks.build]
description = "构建项目"
run = "cargo build --release"
[tasks.test]
description = "运行所有测试"
run = "cargo test"
[tasks.lint]
description = "运行代码检查器"
run = "cargo clippy -- -D warnings"
运行任务
# 运行一个任务
mise run build
mise build # 如果没有命令冲突,可以使用简写
# 运行多个任务
mise run build test lint
# 列出可用任务
mise tasks ls
# 显示任务详情
mise tasks info build
任务依赖
顺序依赖
[tasks.deploy]
description = "部署应用程序"
depends = ["build", "test"]
run = "./deploy.sh"
并行依赖
[tasks.ci]
description = "运行CI检查"
depends = ["lint", "test", "security-scan"]
run = "echo '所有检查通过'"
Mise会自动在可能时并行运行依赖任务。
文件任务
创建文件任务
# 创建任务目录
mkdir -p .mise/tasks
# 创建可执行任务文件
cat > .mise/tasks/deploy <<'EOF'
#!/usr/bin/env bash
# mise description="部署应用程序"
# mise depends=["build", "test"]
echo "部署中..."
./scripts/deploy.sh
EOF
chmod +x .mise/tasks/deploy
文件任务元数据
#!/usr/bin/env bash
# mise description="任务描述"
# mise depends=["dependency1", "dependency2"]
# mise sources=["src/**/*.rs"]
# mise outputs=["target/release/app"]
# 任务实现
高级任务配置
任务参数
[tasks.test]
description = "运行测试,可选过滤器"
run = '''
if [ -n "$1" ]; then
cargo test "$1"
else
cargo test
fi
'''
# 运行所有测试
mise test
# 运行特定测试
mise test user_tests
任务中的环境变量
[tasks.build]
description = "使用特定配置构建"
env = { RUST_ENV = "production", OPTIMIZATION = "3" }
run = "cargo build --release"
[tasks.dev]
description = "启动开发服务器"
env = { NODE_ENV = "development", PORT = "3000" }
run = "npm run dev"
任务别名
[tasks.b]
alias = "build"
[tasks.t]
alias = "test"
[tasks.d]
alias = "dev"
# 使用别名
mise b # 运行build
mise t # 运行test
文件监视
监视模式任务
[tasks.watch]
description = "监视并重建变化"
run = "cargo watch -x build"
[tasks."test:watch"]
description = "监视并运行测试"
run = "cargo watch -x test"
# 使用mise内置监视
mise watch --task build
单仓库任务模式
工作空间任务
# 根mise.toml
[tasks.build-all]
description = "构建所有包"
depends = ["pkg-a:build", "pkg-b:build", "pkg-c:build"]
run = "echo '所有包已构建'"
[tasks."pkg-a:build"]
description = "构建包A"
run = "cd packages/pkg-a && cargo build"
[tasks."pkg-b:build"]
description = "构建包B"
run = "cd packages/pkg-b && cargo build"
每包任务
# packages/pkg-a/mise.toml
[tasks.build]
description = "构建包A"
run = "cargo build"
[tasks.test]
description = "测试包A"
depends = ["build"]
run = "cargo test"
任务验证
验证任务配置
# 验证所有任务
mise tasks validate
# 检查特定任务
mise tasks info build
常见验证错误
# 错误:缺少必需字段
[tasks.broken]
run = "echo test" # 缺少description
# 错误:无效依赖
[tasks.deploy]
description = "部署"
depends = ["nonexistent-task"]
run = "./deploy.sh"
# 错误:循环依赖
[tasks.a]
description = "任务A"
depends = ["b"]
run = "echo a"
[tasks.b]
description = "任务B"
depends = ["a"] # 循环!
run = "echo b"
最佳实践
按目的组织任务
# 构建任务
[tasks.build]
description = "构建生产二进制文件"
run = "cargo build --release"
[tasks."build:debug"]
description = "构建调试二进制文件"
run = "cargo build"
# 测试任务
[tasks.test]
description = "运行所有测试"
run = "cargo test"
[tasks."test:unit"]
description = "仅运行单元测试"
run = "cargo test --lib"
[tasks."test:integration"]
description = "运行集成测试"
run = "cargo test --test '*'"
# 开发任务
[tasks.dev]
description = "启动开发服务器"
run = "cargo run"
[tasks.watch]
description = "监视并重建"
run = "cargo watch -x run"
使用描述性名称
# 好:清晰、描述性名称
[tasks."test:integration:api"]
description = "运行API集成测试"
run = "pytest tests/integration/api"
# 避免:模糊名称
[tasks.t1]
description = "某些测试"
run = "pytest"
利用依赖
# 好:显式依赖
[tasks.deploy]
description = "部署到生产环境"
depends = ["lint", "test", "build"]
run = "./scripts/deploy.sh"
# 避免:在run命令中手动排序
[tasks.deploy]
description = "部署到生产环境"
run = '''
cargo clippy
cargo test
cargo build --release
./scripts/deploy.sh
'''
使用环境变量
# 访问mise环境变量
[tasks.info]
description = "显示任务信息"
run = '''
echo "任务: $MISE_TASK_NAME"
echo "项目根目录: $MISE_PROJECT_ROOT"
echo "配置根目录: $MISE_CONFIG_ROOT"
'''
常见模式
前后钩子
[tasks.build]
description = "使用前后钩子构建"
depends = ["pre-build"]
run = "cargo build"
[tasks.pre-build]
description = "构建前运行"
run = "echo '准备构建...'"
[tasks.post-build]
description = "构建后运行"
run = "echo '构建完成!'"
[tasks."build:full"]
description = "包含所有钩子的完整构建"
depends = ["pre-build", "build", "post-build"]
run = "echo '完整构建流程完成'"
条件执行
[tasks.deploy]
description = "仅当测试通过时部署"
run = '''
if mise test; then
./scripts/deploy.sh
else
echo "测试失败,部署取消"
exit 1
fi
'''
多阶段构建
[tasks.build]
description = "多阶段构建"
depends = ["compile", "optimize", "package"]
run = "echo '构建完成'"
[tasks.compile]
description = "编译源代码"
run = "cargo build --release"
[tasks.optimize]
description = "优化二进制文件"
depends = ["compile"]
run = "strip target/release/app"
[tasks.package]
description = "创建包"
depends = ["optimize"]
run = "tar -czf app.tar.gz target/release/app"
反模式
不要硬编码路径
# 坏:硬编码绝对路径
[tasks.build]
description = "构建"
run = "cd /Users/me/project && cargo build"
# 好:使用相对路径和环境变量
[tasks.build]
description = "构建"
run = "cd $MISE_PROJECT_ROOT && cargo build"
不要重复逻辑
# 坏:重复的测试逻辑
[tasks."test:unit"]
run = "cargo test --lib"
[tasks."test:integration"]
run = "cargo test --test '*'"
[tasks."test:all"]
run = "cargo test --lib && cargo test --test '*'"
# 好:使用依赖
[tasks."test:all"]
depends = ["test:unit", "test:integration"]
run = "echo '所有测试完成'"
不要忽略退出码
# 坏:吞掉错误
[tasks.ci]
run = '''
cargo clippy || true
cargo test || true
echo "CI完成"
'''
# 好:快速失败
[tasks.ci]
depends = ["lint", "test"]
run = "echo 'CI检查通过'"
相关技能
- 工具管理: 使用Mise管理工具版本
- 环境管理: 环境变量和配置