Create new custom project on specified template using .NET Core
Description:
We will try to create new project template like “console”, “web” using .Net Core.Use case:
You have to write same code in multiple solution files then create project template of that code and reuse everywhere.Prerequisite:
- Install dotnet core 2.0.0+ SDK from https://www.microsoft.com/net/download/core
- Nuget exe
Steps to create project template
- Create class library project and add your custom logic in it.
- Need to add template.json file with template confiurationThe template.json file is placed in a .template.config folder in the root directory of the class library. The file provides configuration information to the template engine. The minimum configuration requires the members shown in the following table, which is sufficient to create a functional template.
{
"$schema": "http://json.schemastore.org/template",
"author": "Rahul Jadhav",
"classifications": [ "Cusotm", "Class", "Library" ],
"identity": "ClassLibrary.SampleProject",
"name": "Custom Class Library Project",
"shortName": "customclasslibrary"
}
- Create nuget package of same using these instructions no need to add package to nuget package gallery. We just need nuget file.
Suppose you have created file like ClassLibrary.SampleProject.nupkg
Execute following commands in command prompt:
- Now we will add above nuget package in projects list using following command
dotnet new -i
In our case,
dotnet new -i ClassLibrary.SampleProject.nupkg
You will see ClassLibrary.SampleProject.nupkg Template in Templates list displayed on command prompt after successful installation
- Create a empty folder with project_name
mkdir
cd
- Execute dotnet core command to create class library project and give short name of template which we have installed previouslyIn our case it is “customclasslibrary”
dotnet new customclasslibrary
You will see custom class library project structure is created in newly created folder.
Now you don’t have rewrite custom code for every solution your template file has it and it is added by default when we execute above command.
If you like above article please like and share it.
Thank you :)
Comments
Post a Comment