template-authoring

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Template Authoring

模板创作

This skill helps an agent create and validate custom
dotnet new
templates. It guides bootstrapping templates from existing projects and validates
template.json
files for authoring issues before publishing.
本Skill可帮助Agent创建和验证自定义
dotnet new
模板。它指导用户从现有项目初始化模板,并在发布前验证
template.json
文件是否存在创作问题。

When 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
    .template.config/template.json
    from scratch
  • 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
    template-discovery
    or
    template-instantiation
  • User has MSBuild issues unrelated to template authoring — route to
    dotnet-msbuild
    plugin
  • 用户希望查找或使用现有模板 —— 请转至
    template-discovery
    template-instantiation
  • 用户遇到与模板创作无关的MSBuild问题 —— 请转至
    dotnet-msbuild
    插件

Inputs

输入参数

InputRequiredDescription
Source project pathFor creationPath to the .csproj to use as template source
template.json pathFor validationPath to an existing template.json to validate
Template nameFor creationHuman-readable name for the template
Short nameRecommendedShort name for
dotnet new <shortname>
usage
输入项是否必填描述
源项目路径创建模板时必填作为模板源的.csproj文件路径
template.json路径验证模板时必填待验证的现有template.json文件路径
模板名称创建模板时必填模板的可读名称
短名称推荐填写用于
dotnet new <shortname>
命令的短名称

Workflow

工作流程

Step 1: Bootstrap from existing project

步骤1:从现有项目初始化模板

Analyze the source
.csproj
and create a
.template.config/template.json
:
  1. Create
    .template.config
    directory next to the project
  2. Generate
    template.json
    with
    identity
    (reverse-DNS),
    name
    ,
    shortName
    ,
    sourceName
    (project name for replacement),
    classifications
    , and
    tags
  3. 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
  1. 在项目旁创建
    .template.config
    目录
  2. 生成包含
    identity
    (反向DNS格式)、
    name
    shortName
    sourceName
    (用于替换的项目名称)、
    classifications
    tags
    template.json
  3. 保留源项目中的以下内容: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
template.json
for common authoring issues:
Validation checks to perform:
  • Required fields — verify
    identity
    ,
    name
    , and
    shortName
    are present
  • Identity format — use reverse-DNS format (e.g.,
    MyOrg.Templates.WebApi
    )
  • Parameter issues — check datatypes are valid (
    string
    ,
    bool
    ,
    choice
    ,
    int
    ,
    float
    ), choices have defaults, descriptions are present
  • ShortName conflicts — avoid names that collide with built-in CLI commands (
    build
    ,
    run
    ,
    test
    ,
    publish
    ). Check with
    dotnet new list
    to see if the name is already taken
  • 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:
  1. Add parameters with appropriate types (string, bool, choice), defaults, and descriptions
  2. Add conditional content using
    #if
    preprocessor directives for optional features
  3. Configure post-actions for solution add, restore, or custom scripts
  4. Set constraints to restrict which SDKs or workloads the template supports
  5. Add classifications and tags for discoverability
根据验证结果和用户需求:
  1. 添加参数,设置合适的类型(string、bool、choice)、默认值和描述
  2. 添加条件内容,使用
    #if
    预处理指令实现可选功能
  3. 配置post-actions,用于添加解决方案、还原依赖或执行自定义脚本
  4. 设置约束,限制模板支持的SDK或工作负载
  5. 添加分类和标签,提升模板的可发现性

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/TestProject
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/TestProject

Validation

验证标准

  • template.json
    passes manual validation with zero errors
  • 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

常见陷阱

PitfallSolution
Identity format issuesUse reverse-DNS format (e.g.,
MyOrg.Templates.WebApi
). Avoid spaces or special characters.
ShortName conflicts with CLI commandsAvoid names like
build
,
run
,
test
,
publish
. Check by running
dotnet new list
to see if the name is already taken.
Missing parameter descriptionsEvery parameter should have a
description
and
displayName
for discoverability.
Not testing all parameter combinationsUse
dotnet new <template> --dry-run
with different parameter values to verify conditional content works correctly.
Hardcoded versions in templateUse
sourceName
replacement for project names and consider parameterizing framework versions.
Not setting classificationsAdd appropriate
classifications
(e.g.,
["Web", "API"]
) for template discovery.
陷阱解决方案
Identity格式问题使用反向DNS格式(例如:
MyOrg.Templates.WebApi
)。避免空格或特殊字符。
ShortName与CLI命令冲突避免使用
build
run
test
publish
等名称。可通过运行
dotnet new list
命令检查名称是否已被占用。
缺少参数描述每个参数都应包含
description
displayName
,以提升可发现性。
未测试所有参数组合使用
dotnet new <template> --dry-run
命令搭配不同参数值,验证条件内容是否正常工作。
模板中存在硬编码版本使用
sourceName
替换项目名称,并考虑将框架版本参数化。
未设置分类添加合适的
classifications
(例如:
["Web", "API"]
),以便模板被发现。

More Info

更多信息