.NET 本地工具
何时使用此技能
在以下情况下使用此技能:
- 在开发团队中建立一致的工具链
- 确保 CI/CD 管道与本地开发使用相同版本的工具
- 管理项目特定的 CLI 工具(如 docfx、incrementalist、dotnet-ef 等)
- 避免项目间全局工具版本冲突
什么是本地工具?
本地工具是按仓库级别安装和版本化的 .NET CLI 工具,而非全局安装。它们通过 .config/dotnet-tools.json 定义,并通过 dotnet tool restore 恢复。
本地工具 vs 全局工具
| 方面 | 全局工具 | 本地工具 |
|---|---|---|
| 安装方式 | dotnet tool install -g | dotnet tool restore |
| 作用范围 | 机器全局 | 按仓库 |
| 版本控制 | 手动 | 在 .config/dotnet-tools.json 中 |
| CI/CD | 每个工具单独安装 | 单条恢复命令 |
| 冲突 | 可能存在版本冲突 | 项目间隔离 |
设置本地工具
初始化清单文件
# 创建 .config/dotnet-tools.json
dotnet new tool-manifest
这将创建:
.config/
└── dotnet-tools.json
本地安装工具
# 本地安装一个工具
dotnet tool install docfx
# 安装特定版本
dotnet tool install docfx --version 2.78.3
# 从指定源安装
dotnet tool install MyTool --add-source https://mycompany.pkgs.visualstudio.com/_packaging/feed/nuget/v3/index.json
恢复工具
# 从清单恢复所有工具
dotnet tool restore
dotnet-tools.json 格式
{
"version": 1,
"isRoot": true,
"tools": {
"docfx": {
"version": "2.78.3",
"commands": [
"docfx"
],
"rollForward": false
},
"dotnet-ef": {
"version": "9.0.0",
"commands": [
"dotnet-ef"
],
"rollForward": false
},
"incrementalist.cmd": {
"version": "1.2.0",
"commands": [
"incrementalist"
],
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": [
"reportgenerator"
],
"rollForward": false
}
}
}
字段说明
| 字段 | 描述 |
|---|---|
version | 清单架构版本(始终为 1) |
isRoot | 标记为根清单(阻止向上搜索父目录) |
tools | 工具配置字典 |
tools.<name>.version | 要安装的精确版本 |
tools.<name>.commands | 工具提供的 CLI 命令 |
tools.<name>.rollForward | 是否允许更新版本(通常设为 false 以确保可重现性) |
常用工具
文档生成
# DocFX - API 文档生成器
dotnet tool install docfx
"docfx": {
"version": "2.78.3",
"commands": ["docfx"],
"rollForward": false
}
用法:
dotnet docfx docfx.json
dotnet docfx serve _site
Entity Framework Core
# EF Core 迁移 CLI
dotnet tool install dotnet-ef
"dotnet-ef": {
"version": "9.0.0",
"commands": ["dotnet-ef"],
"rollForward": false
}
用法:
dotnet ef migrations add InitialCreate
dotnet ef database update
代码覆盖率
# ReportGenerator 覆盖率报告生成器
dotnet tool install dotnet-reportgenerator-globaltool
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": ["reportgenerator"],
"rollForward": false
}
用法:
dotnet reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport -reporttypes:Html
增量构建
# Incrementalist - 仅构建更改的项目
dotnet tool install incrementalist.cmd
"incrementalist.cmd": {
"version": "1.2.0",
"commands": ["incrementalist"],
"rollForward": false
}
用法:
# 获取自 main 分支以来受更改影响的项目
incrementalist --branch main
代码格式化
# CSharpier - 固定风格的 C# 格式化工具
dotnet tool install csharpier
"csharpier": {
"version": "0.30.3",
"commands": ["dotnet-csharpier"],
"rollForward": false
}
用法:
dotnet csharpier .
dotnet csharpier --check . # CI 模式 - 如果需要更改则失败
代码分析
# JB dotnet-inspect(需要许可证)
dotnet tool install jb
"jb": {
"version": "2024.3.4",
"commands": ["jb"],
"rollForward": false
}
CI/CD 集成
GitHub Actions
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 配置 .NET
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
- name: 恢复工具
run: dotnet tool restore
- name: 构建
run: dotnet build
- name: 测试并收集覆盖率
run: dotnet test --collect:"XPlat Code Coverage"
- name: 生成覆盖率报告
run: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coveragereport
- name: 构建文档
run: dotnet docfx docs/docfx.json
Azure Pipelines
steps:
- task: UseDotNet@2
inputs:
useGlobalJson: true
- script: dotnet tool restore
displayName: '恢复 .NET 工具'
- script: dotnet build -c Release
displayName: '构建'
- script: dotnet test -c Release --collect:"XPlat Code Coverage"
displayName: '测试'
- script: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:$(Build.ArtifactStagingDirectory)/coverage
displayName: '生成覆盖率报告'
管理工具版本
更新工具
# 更新到最新版本
dotnet tool update docfx
# 更新到特定版本
dotnet tool update docfx --version 2.79.0
列出已安装工具
# 列出本地工具
dotnet tool list
# 检查过时版本
dotnet tool list --outdated
卸载工具
dotnet tool uninstall docfx
最佳实践
1. 始终设置 isRoot: true
防止 MSBuild 向上搜索父目录的工具清单:
{
"version": 1,
"isRoot": true,
...
}
2. 锁定精确版本
使用 "rollForward": false 确保可重现构建:
"docfx": {
"version": "2.78.3",
"rollForward": false
}
3. 在 CI 中先恢复再使用
在使用任何本地工具之前,始终先运行 dotnet tool restore:
- run: dotnet tool restore
- run: dotnet docfx docs/docfx.json
4. 记录工具依赖
在 README 中添加注释或章节:
## 开发环境设置
1. 恢复工具:`dotnet tool restore`
2. 构建:`dotnet build`
3. 测试:`dotnet test`
5. 使用 Dependabot 管理更新
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "weekly"
# 包含 .config/dotnet-tools.json 中的本地工具
故障排除
恢复后工具未找到
确保您从仓库根目录运行:
# 错误 - 从子目录运行
cd src/MyApp
dotnet docfx # 错误:工具未找到
# 正确 - 从解决方案根目录运行
cd ../..
dotnet docfx docs/docfx.json
版本冲突
如果遇到版本冲突,请检查:
- 全局工具中不同版本:
dotnet tool list -g - 多个工具清单:在父目录中查找
.config/dotnet-tools.json
清除工具缓存
# 清除 NuGet 工具缓存
dotnet nuget locals all --clear
# 重新恢复工具
dotnet tool restore
示例:完整的开发环境配置
{
"version": 1,
"isRoot": true,
"tools": {
"docfx": {
"version": "2.78.3",
"commands": ["docfx"],
"rollForward": false
},
"dotnet-ef": {
"version": "9.0.0",
"commands": ["dotnet-ef"],
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": ["reportgenerator"],
"rollForward": false
},
"csharpier": {
"version": "0.30.3",
"commands": ["dotnet-csharpier"],
"rollForward": false
},
"incrementalist.cmd": {
"version": "1.2.0",
"commands": ["incrementalist"],
"rollForward": false
}
}
}
开发工作流程:
# 初始设置
dotnet tool restore
# 提交前格式化代码
dotnet csharpier .
# 运行测试并收集覆盖率
dotnet test --collect:"XPlat Code Coverage"
dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coverage
# 构建文档
dotnet docfx docs/docfx.json
# 检查哪些项目更改了(用于大型仓库)
incrementalist --branch main