template-authoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTemplate Authoring
模板创作
This skill helps an agent create and validate custom templates. It guides bootstrapping templates from existing projects and validates files for authoring issues before publishing.
dotnet newtemplate.json本Skill可帮助Agent创建和验证自定义模板。它指导用户从现有项目初始化模板,并在发布前验证文件是否存在创作问题。
dotnet newtemplate.jsonWhen to Use
适用场景
- User wants to create a reusable template from an existing .csproj
- User wants to validate a template.json for correctness
- User is setting up from scratch
.template.config/template.json - User wants to package a template for NuGet distribution
- 用户希望从现有.csproj创建可复用模板
- 用户希望验证template.json的正确性
- 用户从零开始设置
.template.config/template.json - 用户希望将模板打包为NuGet包进行分发
When Not to Use
不适用场景
- User wants to find or use existing templates — route to or
template-discoverytemplate-instantiation - User has MSBuild issues unrelated to template authoring — route to plugin
dotnet-msbuild
- 用户希望查找或使用现有模板 —— 请转至或
template-discoverytemplate-instantiation - 用户遇到与模板创作无关的MSBuild问题 —— 请转至插件
dotnet-msbuild
Inputs
输入参数
| Input | Required | Description |
|---|---|---|
| Source project path | For creation | Path to the .csproj to use as template source |
| template.json path | For validation | Path to an existing template.json to validate |
| Template name | For creation | Human-readable name for the template |
| Short name | Recommended | Short name for |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 源项目路径 | 创建模板时必填 | 作为模板源的.csproj文件路径 |
| template.json路径 | 验证模板时必填 | 待验证的现有template.json文件路径 |
| 模板名称 | 创建模板时必填 | 模板的可读名称 |
| 短名称 | 推荐填写 | 用于 |
Workflow
工作流程
Step 1: Bootstrap from existing project
步骤1:从现有项目初始化模板
Analyze the source and create a :
.csproj.template.config/template.json- Create directory next to the project
.template.config - Generate with
template.json(reverse-DNS),identity,name,shortName(project name for replacement),sourceName, andclassificationstags - Preserve from source: SDK type, package references with metadata (PrivateAssets, IncludeAssets), properties (OutputType, TreatWarningsAsErrors), CPM patterns
Minimal example:
json
{
"$schema": "http://json.schemastore.org/template",
"author": "MyOrg",
"classifications": ["Library"],
"identity": "MyOrg.Templates.MyLib",
"name": "My Library Template",
"shortName": "mylib",
"sourceName": "MyLib",
"tags": { "language": "C#", "type": "project" }
}分析源文件并创建:
.csproj.template.config/template.json- 在项目旁创建目录
.template.config - 生成包含(反向DNS格式)、
identity、name、shortName(用于替换的项目名称)、sourceName和classifications的tagstemplate.json - 保留源项目中的以下内容:SDK类型、带有元数据(PrivateAssets、IncludeAssets)的包引用、属性(OutputType、TreatWarningsAsErrors)、CPM模式
最小示例:
json
{
"$schema": "http://json.schemastore.org/template",
"author": "MyOrg",
"classifications": ["Library"],
"identity": "MyOrg.Templates.MyLib",
"name": "My Library Template",
"shortName": "mylib",
"sourceName": "MyLib",
"tags": { "language": "C#", "type": "project" }
}Step 2: Validate template.json
步骤2:验证template.json
Read and review the for common authoring issues:
template.jsonValidation checks to perform:
- Required fields — verify ,
identity, andnameare presentshortName - Identity format — use reverse-DNS format (e.g., )
MyOrg.Templates.WebApi - Parameter issues — check datatypes are valid (,
string,bool,choice,int), choices have defaults, descriptions are presentfloat - ShortName conflicts — avoid names that collide with built-in CLI commands (,
build,run,test). Check withpublishto see if the name is already takendotnet new list - Post-action completeness — verify post-actions have all required configuration
- Tags — ensure language, type, and classification tags are set for discoverability
读取并检查,排查常见创作问题:
template.json需执行的验证检查:
- 必填字段 —— 确认、
identity和name已存在shortName - Identity格式 —— 使用反向DNS格式(例如:)
MyOrg.Templates.WebApi - 参数问题 —— 检查数据类型是否有效(、
string、bool、choice、int)、选项是否有默认值、是否包含描述信息float - ShortName冲突 —— 避免与内置CLI命令(、
build、run、test)重名。可通过publish命令检查名称是否已被占用dotnet new list - Post-action完整性 —— 验证post-actions是否包含所有必需配置
- 标签 —— 确保设置了语言、类型和分类标签以提升可发现性
Step 3: Refine the template
步骤3:优化模板
Based on validation results and user requirements:
- Add parameters with appropriate types (string, bool, choice), defaults, and descriptions
- Add conditional content using preprocessor directives for optional features
#if - Configure post-actions for solution add, restore, or custom scripts
- Set constraints to restrict which SDKs or workloads the template supports
- Add classifications and tags for discoverability
根据验证结果和用户需求:
- 添加参数,设置合适的类型(string、bool、choice)、默认值和描述
- 添加条件内容,使用预处理指令实现可选功能
#if - 配置post-actions,用于添加解决方案、还原依赖或执行自定义脚本
- 设置约束,限制模板支持的SDK或工作负载
- 添加分类和标签,提升模板的可发现性
Step 4: Test the template locally
步骤4:本地测试模板
bash
dotnet new install ./path/to/template/root
dotnet new mylib --name TestProject --dry-run
dotnet new mylib --name TestProject --output ./test-output
dotnet build ./test-output/TestProjectbash
dotnet new install ./path/to/template/root
dotnet new mylib --name TestProject --dry-run
dotnet new mylib --name TestProject --output ./test-output
dotnet build ./test-output/TestProjectValidation
验证标准
- passes manual validation with zero errors
template.json - Template identity and shortName are unique and meaningful
- All parameters have descriptions and appropriate defaults
- Template can be installed, dry-run, and instantiated successfully
- Created projects build cleanly with
dotnet build - Conditional content produces correct output for all parameter combinations
- 通过手动验证,无错误
template.json - 模板的identity和shortName唯一且有意义
- 所有参数均包含描述和合适的默认值
- 模板可成功安装、执行dry-run并实例化
- 创建的项目可通过命令正常构建
dotnet build - 条件内容在所有参数组合下均可生成正确输出
Common Pitfalls
常见陷阱
| Pitfall | Solution |
|---|---|
| Identity format issues | Use reverse-DNS format (e.g., |
| ShortName conflicts with CLI commands | Avoid names like |
| Missing parameter descriptions | Every parameter should have a |
| Not testing all parameter combinations | Use |
| Hardcoded versions in template | Use |
| Not setting classifications | Add appropriate |
| 陷阱 | 解决方案 |
|---|---|
| Identity格式问题 | 使用反向DNS格式(例如: |
| ShortName与CLI命令冲突 | 避免使用 |
| 缺少参数描述 | 每个参数都应包含 |
| 未测试所有参数组合 | 使用 |
| 模板中存在硬编码版本 | 使用 |
| 未设置分类 | 添加合适的 |
More Info
更多信息
- Custom templates for dotnet new — official authoring guide
- template.json reference — full schema reference
- Template Engine Wiki — template engine internals
- Custom templates for dotnet new —— 官方创作指南
- template.json reference —— 完整架构参考
- Template Engine Wiki —— 模板引擎内部原理