1. 創建模板配置
1.1 在項目目錄中創建.template.config
文件夾
1.2 創建一個名為“template.json” 的新文件
{
"author": "5DThinking",
"classifications": [ "WinForm" ], //對應模板的Tags
"identity": "5DThinking.Demo", //模板的唯一名稱
"name": "5DThinking.Demo", //對應模板的Templates
"shortName": "abc", //【修改】短名稱,使用 dotnet new <shortName> 安裝模板時的名稱
"tags": {
"language": "C#",
"type": "project"
}
"sourceName": "xxx", // 【修改】在使用 -n 選項時,會替換模板中項目的名字xxx
"preferNameDirectory": true // 可選,添加目錄
}
注意:"sourceName": "xxx"
指明模板中將要被替換的字符串
2. 安裝模板
運行命令dotnet new -i .
以安裝位於當前文件夾的模板
3. 測試模板
運行命令dotnet new abc -n 5DThinking.Test -o TestTemplate
,新項目成功在TestTemplate
目錄下生成,名字也都統一改為5DThinking.Test
4. 遇到的坑
現在運行新項目,出現一堆錯誤,主要是兩類:1.NuGet包還原問題 2..resx
文件報錯
臨時解決辦法:
- 從模板項目
lib
目錄中復制.dll
文件到新項目相應目錄中並覆蓋,在項目中重新引用 - 從模板項目復制
.resx
文件到新項目相應目錄中並覆蓋
注:這個解決辦法是野路子,雖然也行,但比較Low,下面嘗試打包的方式來解決
5. 用NuGet生成包
5.1 根目錄GMS
下創建Content\GMSTemplate
目錄,將模板文件及文件夾都復制進來
5.2 template.json
文件修改為:
{
"$schema": "http://json.schemastore.org/template",
"author": "5DThinking",
"classifications": [ "WinForm" ],
"name": "5DThinking WinForm GMS v1.0",
"identity": "GMS.WinForm.1.0.Template",
"shortName": "gms1.0",
"tags": {
"language": "C#" ,
"type":"project"
},
"sourceName": "Thinking.GMS",
"preferNameDirectory": true
}
5.3 在GMS
目錄下創建GMSTemplate.nuspec
文件,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>GMSTemplate</id>
<version>1.0.1</version>
<description>
GMS(General Management System) For WinForm
</description>
<authors>5DThinking</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
5.4 用NuGet打包,在GMS
目錄下執行命令nuget pack GMSTemplate.nuspec -OutputDirectory .
,然后生成GMSTemplate.1.0.1.nupkg
文件
5.5 在GMS
目錄下創建CreateYourProject.bat
文件,內容如下:
color 4
dotnet new -i GMSTemplate.1.0.1.nupkg
set /p OP=Please set your project name(for example:Baidu.Api):
md .1YourProject
cd .1YourProject
dotnet new gms1.0 -n %OP%
cd ../
echo "Create Successfully!!!! ^ please see the folder .1YourProject"
dotnet new -u GMSTemplate
echo "Delete Template Successfully"
pause
5.6 雙擊執行CreateYourProject.bat
文件,按提示輸入新的項目名稱,大功告成!