名称: install-tool 描述: 安装或升级 .NET 工具,支持别名(如 ef、coverage 等)和清单管理 参数提示: “–tool <名称|别名> [–global] [–upgrade] [–upgrade-all] [–list]” 允许工具: Bash, Read, Write, Edit, AskUserQuestion
/dotnet:install-tool
安装、管理和升级 .NET 工具,支持别名解析和工具清单支持。
参数
从 $ARGUMENTS 解析参数:
| 标志 | 描述 | 默认值 |
|---|---|---|
--tool <名称|别名> |
工具名称或别名(见下表) | 除非 --list 或 --upgrade-all,否则必需 |
--global |
全局安装 | false(默认本地) |
--version <版本> |
特定版本 | 最新 |
--upgrade |
升级特定工具到最新 | false |
--upgrade-all |
升级清单中所有工具(交互式) | false |
--list |
列出已安装工具 | false |
--create-manifest |
如果缺失,创建工具清单 | 自动 |
工具别名
| 别名 | 完整包名 | 描述 |
|---|---|---|
coverage |
dotnet-coverage |
代码覆盖率收集 |
reportgen |
dotnet-reportgenerator-globaltool |
覆盖率报告生成 |
ef |
dotnet-ef |
Entity Framework Core 工具 |
outdated |
dotnet-outdated-tool |
检查过时包 |
stryker |
dotnet-stryker |
变异测试 |
csharpier |
csharpier |
固执的 C# 格式化工具 |
husky |
husky |
Git 钩子管理 |
nbgv |
nbgv |
Nerdbank.GitVersioning CLI |
swagger |
swashbuckle.aspnetcore.cli |
Swagger/OpenAPI 生成 |
httprepl |
microsoft.dotnet-httprepl |
HTTP REPL 用于 API 测试 |
内置工具(无需安装):
format- 代码格式化(SDK 6+)user-secrets- 用户密钥管理watch- 热重载dev-certs- HTTPS 开发证书
工作流程
步骤 1: 解析工具名称
如果提供别名,解析为完整包名:
coverage -> dotnet-coverage
ef -> dotnet-ef
如果请求内置工具:
'format' 是内置 SDK 工具。无需安装。
用法:
dotnet format [<项目 | 解决方案>]
运行 'dotnet format --help' 查看选项。
步骤 2: 检查当前状态
列出已安装工具:
# 全局工具
dotnet tool list --global
# 本地工具(从清单)
dotnet tool list
检查清单:
# 查找 .config/dotnet-tools.json
ls .config/dotnet-tools.json 2>/dev/null
步骤 3: 处理清单
如果本地安装(默认)且无清单:
未找到工具清单。创建吗?
工具清单 (.config/dotnet-tools.json) 跟踪本地工具
为此项目,使其在机器间可复制。
选项:
- 是,创建清单(推荐)
- 否,改为全局安装
如果批准,创建清单:
dotnet new tool-manifest
步骤 4: 安装工具
本地安装(默认):
dotnet tool install <包名> [--version <版本>]
全局安装:
dotnet tool install --global <包名> [--version <版本>]
步骤 5: 提供使用提示
安装后,提供相关使用信息:
工具已安装:dotnet-ef(别名:ef)
用法:
dotnet ef migrations add <名称>
dotnet ef database update
dotnet ef dbcontext scaffold
常用命令:
dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet ef migrations script
文档:https://learn.microsoft.com/ef/core/cli/dotnet
升级模式
单工具升级(--upgrade)
/dotnet:install-tool --tool ef --upgrade
工作流程:
- 检查当前版本
- 检查最新版本
- 如果有更新可用,升级:
dotnet tool update <包名> [--global]
全部升级(--upgrade-all)
/dotnet:install-tool --upgrade-all
工作流程:
- 读取
.config/dotnet-tools.json - 检查每个工具的更新
- 呈现交互式列表:
工具更新可用
工具 当前 最新 操作
dotnet-ef 8.0.0 10.0.0 [ ] 升级
dotnet-coverage 17.8.0 17.12.0 [ ] 升级
dotnet-reportgenerator-globaltool 5.2.0 5.4.0 [ ] 升级
选择要升级的工具:
1. 升级所有
2. 逐个选择
3. 跳过(无更改)
- 应用所选升级
- 报告结果
输出格式
列表模式:
已安装 .NET 工具
本地工具(从 .config/dotnet-tools.json):
包 版本 命令
dotnet-ef 10.0.0 dotnet-ef
dotnet-coverage 17.12.0 dotnet-coverage
csharpier 0.29.0 dotnet-csharpier
全局工具:
包 版本 命令
dotnet-outdated-tool 4.6.0 dotnet-outdated
nbgv 3.6.0 nbgv
清单:.config/dotnet-tools.json
安装成功:
工具安装成功
包:dotnet-ef
别名:ef
版本:10.0.0
范围:本地(清单)
清单更新:.config/dotnet-tools.json
快速开始:
dotnet ef migrations add InitialCreate
dotnet ef database update
运行 'dotnet ef --help' 查看所有命令。
升级成功:
工具升级完成
已更新:
- dotnet-ef: 8.0.0 -> 10.0.0
- dotnet-coverage: 17.8.0 -> 17.12.0
跳过:
- csharpier: 已是最新 (0.29.0)
清单更新:.config/dotnet-tools.json
内置工具:
'format' 是内置 SDK 工具(无需安装)
'dotnet format' 命令包含在 .NET SDK 6.0+ 中。
用法:
dotnet format # 格式化整个解决方案
dotnet format --verify-no-changes # 检查格式化(CI)
dotnet format whitespace # 仅格式化空白
dotnet format style # 仅格式化代码风格
dotnet format analyzers # 应用分析器修复
您的 SDK: 10.0.100(包含 format)
清单结构
.config/dotnet-tools.json:
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "10.0.0",
"commands": ["dotnet-ef"]
},
"dotnet-coverage": {
"version": "17.12.0",
"commands": ["dotnet-coverage"]
}
}
}
示例
# 本地安装 EF Core 工具
/dotnet:install-tool --tool ef
# 全局安装覆盖率工具
/dotnet:install-tool --tool coverage --global
# 安装特定版本
/dotnet:install-tool --tool ef --version 9.0.0
# 列出所有已安装工具
/dotnet:install-tool --list
# 升级特定工具
/dotnet:install-tool --tool ef --upgrade
# 交互式升级所有本地工具
/dotnet:install-tool --upgrade-all
# 使用完整包名
/dotnet:install-tool --tool dotnet-reportgenerator-globaltool
恢复工具
克隆带有工具清单的仓库后:
dotnet tool restore
这会从 .config/dotnet-tools.json 安装所有指定版本的工具。