在代码目录下创建.template.config/template.json

template.json内容如下:
{
"$schema": "http://json.schemastore.org/template",
"author": "Zelun",
"classifications": ["myname", "framework"],
"name": "myname-name",
"identity": "myname-identity",
"groupIdentity": "myname-groupIdentity",
"shortName": "myname-shortName",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "MyName",
"preferNameDirectory": true
}
- author:作者
- classifications:分类
- name:模板名称
- shortName:短名称
- sourceName:使用模板创建项目时,可替换的名称,有用!
保存后回到根目录,开妈准备安装使用
安装模板
#注意最后的点“.”
dotnet new install .\
#删除模板使用,注意最后的点“.”
dotnet new uninstall .\
#列出模板使用
dotnet new list
#列出可删除模板
dotnet new uninstall
使用模板
dotnet new myname -n DemoProject
打包到nuget
在代码目录内创建一个 nuspec 文件:myname.template.dotnet.nuspec,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>myname.template.dotnet</id>
<version>1.0.1</version>
<description>
myname.template.dotnet, including WebApi, Console
</description>
<authors>Zelun</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
需要注意,packageType 为 Template,metadata.id 必须保证唯一,其他按需设置即可。
必须是在 content 文件夹内。nuget 在打包的时候,是根据 content 文件夹来进行的。
使用 nuget pack 命令打包
nuget pack myname.template.dotnet.nuspec -OutputDirectory .
nuget push myname.template.dotnet.nupkg -s http://url pwd
最终目录结构
详见:https://github.com/atzq1234/myname-template-demo
── templatename-root
└── content
└── MyName
└── .template.config
└──template.json
└── .vs
└──.dockerignore
└── MyName.sln
└── Codexxx[代码目录]
└── myname.template.dotnet.nuspec
└── myname.template.dotnet.1.0.1.nupkg
└── nuget.exe
└── README.md