Table of contents
If you are looking for specific examples or more advanced solutions, then I encourage you to check out this Microsoft’s repository: https://github.com/dotnet/dotnet-template-samples.
Two ways of creating templates
I distinguish two ways of creating templates:
-
Simple - where you put the files you want to use into a directory, and the dotnet new command simply copies the files to the new location and changes their content if necessary,
-
Nuget - where you can create your own template package and freely distribute it as a nuget. It also offers the possibility to put many templates into one package.
Simple
To create a simple template you need to:
-
Create a directory for the stuff that will go into the template - let’s call it
simplest
- this will be our current working directory. -
Inside the working directory, create another one called
.template.config
, and inside the.template.config
directory, create a file calledtemplate.json
-
To the
template.json
file, paste the contents: -
Add any file to the working directory, for example, a project file
PersonComponent.Core.csproj
and a C# class file namedInstaller.cs
with the contents: -
Now go in the console to the parent directory for the working directory and invoke the `dotnet new -i .\simplest' command there. You should see an output at the end:
-
Now the best part Create a new directory named
MyProject
somewhere else and navigate to it in the console, where call thedotnet new ztr-core
command. -
The disadvantage of this solution is that it relies on the template source directory all the time. Add a new
dumb.txt
file to thesimplest
directory even without the contents, and you will see that when you call thedotnet new ztr-core
method again, this one will also be copied. -
Now delete or rename the source directory (
simplest
) and try to create a new project again (dotnet new ztr-core
) -
To uninstall a template use the command
dotnet new -u "full path to template"
.
Nuget
The Nuget template has two major advantages:
-
May be packaged as a nuget,
-
May be a template bundle, that is, it may contain many single, completely different templates provided in one file.
-
To create such a package, create a directory tree with files as below:
-
Complete the project
ZtrDotnetTemplates.csproj
file : -
Complete the console template configuration file
templates/console/.template.config/template.json
-
Complete the
templates/core/.template.config/template.json
,templates/core/Installer.cs
andtemplates/core/PersonComponent.Core.csproj
files. -
Build and install nuget!
-
Use it!
-
Summary
-
I think that a custom project template can be a very useful tool, not only for .Net projects, but also for other solutions, not related to any programming language.
-
Remember, that this article is just a seed, and the whole template engine has still many possibilities, which you can discover in the links below - there are, among others, repository addresses related to this project.
-
Note that the whole thing will be even more fun if you link your own templates to CI/CD (e.g. on Github) which will give you access to your favorite patterns almost everywhere!
References
-
Github with examples https://github.com/dotnet/dotnet-template-samples — a good place to start
-
A basic link that briefly explains everything: https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates, it’s not the most readable, but on a second/third try you can understand everything,
-
A series of articles from MS. It contains the same content as above just more detailed: https://docs.microsoft.com/en-us/dotnet/core/tutorials/cli-templates-create-item-template
-
Github containing the template engine repo: https://github.com/dotnet/templating — a place where you can find a lot of detailed information.
Translated with www.DeepL.com/Translator (free version)References
-
Github with examples https://github.com/dotnet/dotnet-template-samples — a good place to start
-
A basic link that briefly explains everything: https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates, it’s not the most readable, but on a second/third try you can understand everything,
-
A series of articles from MS. It contains the same content as above just more detailed: https://docs.microsoft.com/en-us/dotnet/core/tutorials/cli-templates-create-item-template
-
Github containing the template engine repo: https://github.com/dotnet/templating — a place where you can find a lot of detailed information.