在代碼目錄下建立.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