名称: dotnet-scaffold-project 描述: “创建一个新的.NET项目。生成包含CPM、分析器、editorconfig、SourceLink的解决方案。”
dotnet-scaffold-project
使用所有现代最佳实践搭建一个新的.NET项目。生成完整的解决方案结构,包括中央包管理、分析器、.editorconfig、SourceLink和确定性构建。
前提条件: 首先运行 [skill:dotnet-version-detection] 以确定可用的SDK版本 — 这影响可用的功能和模板。
范围
- 完整的解决方案结构生成 (src/, tests/, .sln)
- 中央包管理和Directory.Build.props
- 分析器、.editorconfig、SourceLink、确定性构建
超出范围
- 解决方案布局原理和约定 – 参见 [skill:dotnet-project-structure]
- 分析器配置详情 – 参见 [skill:dotnet-add-analyzers]
- CI工作流生成 – 参见 [skill:dotnet-add-ci]
交叉引用:布局原理参见 [skill:dotnet-project-structure],分析器配置参见 [skill:dotnet-add-analyzers],脚手架后添加CI参见 [skill:dotnet-add-ci]。
步骤 1: 创建解决方案结构
创建目录布局和解决方案文件。
# 创建目录结构
mkdir -p MyApp/src MyApp/tests
# 创建解决方案文件
cd MyApp
dotnet new sln -n MyApp
# 对于.NET 9+ SDK,转换为.slnx
dotnet sln MyApp.sln migrate
选择项目模板
根据应用程序类型选择适当的模板:
| 模板 | 命令 | SDK |
|---|---|---|
| Web API (最小化) | dotnet new webapi -n MyApp.Api -o src/MyApp.Api |
Microsoft.NET.Sdk.Web |
| Web API (控制器) | dotnet new webapi -n MyApp.Api -o src/MyApp.Api --use-controllers |
Microsoft.NET.Sdk.Web |
| 控制台应用 | dotnet new console -n MyApp.Cli -o src/MyApp.Cli |
Microsoft.NET.Sdk |
| Worker 服务 | dotnet new worker -n MyApp.Worker -o src/MyApp.Worker |
Microsoft.NET.Sdk.Worker |
| 类库 | dotnet new classlib -n MyApp.Core -o src/MyApp.Core |
Microsoft.NET.Sdk |
| Blazor Web 应用 | dotnet new blazor -n MyApp.Web -o src/MyApp.Web |
Microsoft.NET.Sdk.Web |
| MAUI 应用 | dotnet new maui -n MyApp.Mobile -o src/MyApp.Mobile |
Microsoft.Maui.Sdk |
| xUnit 测试 | dotnet new xunit -n MyApp.Tests -o tests/MyApp.Tests |
Microsoft.NET.Sdk |
# 示例:Web API 带类库和测试
dotnet new classlib -n MyApp.Core -o src/MyApp.Core
dotnet new webapi -n MyApp.Api -o src/MyApp.Api
dotnet new xunit -n MyApp.UnitTests -o tests/MyApp.UnitTests
# 添加项目到解决方案
dotnet sln add src/MyApp.Core/MyApp.Core.csproj
dotnet sln add src/MyApp.Api/MyApp.Api.csproj
dotnet sln add tests/MyApp.UnitTests/MyApp.UnitTests.csproj
# 添加项目引用
dotnet add src/MyApp.Api/MyApp.Api.csproj reference src/MyApp.Core/MyApp.Core.csproj
dotnet add tests/MyApp.UnitTests/MyApp.UnitTests.csproj reference src/MyApp.Core/MyApp.Core.csproj
步骤 2: 添加 global.json
固定SDK版本以实现可重复构建。
{
"sdk": {
"version": "10.0.100",
"rollForward": "latestPatch"
}
}
调整版本以匹配 dotnet --version 的输出。
步骤 3: 添加 Directory.Build.props
在仓库根目录创建以在所有项目间共享构建设置。
<Project>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest-all</AnalysisLevel>
</PropertyGroup>
<!-- 确定性构建和SourceLink(用于库) -->
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DebugType>embedded</DebugType>
<ContinuousIntegrationBuild Condition="'$(CI)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>
<!-- NuGet 审计 -->
<PropertyGroup>
<NuGetAudit>true</NuGetAudit>
<NuGetAuditLevel>low</NuGetAuditLevel>
<NuGetAuditMode>all</NuGetAuditMode>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
</Project>
创建此文件后,从单个 .csproj 文件中移除 <TargetFramework>、<Nullable> 和 <ImplicitUsings> 以避免重复。
可选:单独测试属性
<!-- tests/Directory.Build.props -->
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<!-- 使用Microsoft.Testing.Platform v2 运行器(需要 Microsoft.NET.Test.Sdk 17.13+/18.x) -->
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
<!-- 测试不需要 TreatWarningsAsErrors -->
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
步骤 4: 添加 Directory.Build.targets
应用共享包引用(SourceLink、分析器)到所有项目。项目项放在 .targets 中,以便在项目评估后导入。
<Project>
<ItemGroup>
<!-- SourceLink 用于调试器源代码导航 -->
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="all" />
</ItemGroup>
</Project>
内置的Roslyn分析器已通过 Directory.Build.props(步骤3)中的 AnalysisLevel 和 EnforceCodeStyleInBuild 属性启用。有关额外的第三方分析器,请参见 [skill:dotnet-add-analyzers]。
步骤 5: 设置中央包管理
在仓库根目录创建 Directory.Packages.props。
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<!-- 框架包 -->
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<!-- 测试包 -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageVersion Include="xunit.v3" Version="3.2.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageVersion Include="coverlet.collector" Version="8.0.0" />
</ItemGroup>
</Project>
创建此文件后,从所有 .csproj 文件中的 <PackageReference> 元素移除 Version 属性。
步骤 6: 添加 .editorconfig
在仓库根目录创建。完整推荐配置请参见 [skill:dotnet-project-structure]。
最小启动配置:
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{csproj,props,targets,xml,json,yml,yaml}]
indent_size = 2
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
csharp_prefer_braces = true:warning
dotnet_style_require_accessibility_modifiers = always:warning
dotnet_sort_system_directives_first = true
csharp_using_directive_placement = outside_namespace:warning
步骤 7: 添加 nuget.config
配置包源并增强供应链安全:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<packageSourceMapping>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>
步骤 8: 添加 .gitignore
dotnet new gitignore
这生成标准的.NET .gitignore,覆盖 bin/、obj/、*.user 等。
步骤 9: 清理生成的项目
脚手架后,应用共享配置:
- 移除重复属性 从单个
.csproj文件(TargetFramework、Nullable、ImplicitUsings — 这些在 Directory.Build.props 中) - 移除 Version 属性 从 PackageReference 元素(由CPM管理)
- 删除模板生成的 Class1.cs 从类库
- 设置文件作用域命名空间 在所有生成的
.cs文件中
清理后的 csproj 示例
之前(模板生成):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
之后(带共享属性和CPM):
<Project Sdk="Microsoft.NET.Sdk">
</Project>
对于需要 Microsoft.NET.Sdk.Web 的Web项目,csproj 仍指定SDK但继承其他所有内容。
步骤 10: 验证
运行这些命令以验证脚手架项目:
# 恢复并验证锁文件生成
dotnet restore
find . -name "packages.lock.json" -type f
# 构建所有分析器
dotnet build --no-restore
# 运行测试
dotnet test --no-build
# 验证CPM是否激活(项目PackageReferences中没有Version属性)
# 应只在 Directory.Packages.props 中找到版本,不在csproj文件中
find . -name "*.csproj" -exec grep -l 'Version=' {} \; # 期望无输出
最终结构
MyApp/
├── .editorconfig
├── .gitignore
├── global.json
├── nuget.config
├── MyApp.slnx
├── Directory.Build.props
├── Directory.Build.targets
├── Directory.Packages.props
├── src/
│ ├── MyApp.Core/
│ │ └── MyApp.Core.csproj
│ └── MyApp.Api/
│ ├── MyApp.Api.csproj
│ ├── Program.cs
│ └── appsettings.json
└── tests/
└── MyApp.UnitTests/
├── MyApp.UnitTests.csproj
└── SampleTest.cs