Custom "dotnet new" Templates for Your Team
There are a bunch of dotnet templates readily available for use. To list out all the templates, open up the command prompt and type in the following command,
dotnet new --list
That's all fine and dandy! But what about creating your templates? Say, for example, you are bootstrapping a multi-project (e.g. Web, Domain, Infrastructure) microservice-based solution and find it super helpful for others to use as well. You can easily ship your template to NuGet or other sources following some easy steps.
Recently, I'm working mostly with Blazor
and Fluxor
(A state management library just like Redux/Flow). I was doing some tedious work every time I wanted to start from scratch,
- I've to use
dotnet new blazorserver
ordotnet new blazorwasm
to create a new project.
- Install
Fluxor.Balzor.Web
and wire it up in theStartup.cs
orProgram.cs
- Add a
Store
folder and add necessary code files forActions
,Reducers
,State
,Feature
,Effects
, so on and so forth - Initialize the
Store
in theApp.razor
- Import the
<script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script>
in_Host.razor
orindex.html
I figured out that it would be easier if I just create some custom templates where all of this boilerplate stuff is already in place for me. Speaking of creating custom templates for Blazor + Fluxor
, here's a shameless plug
Project Structure
The project structure I'm going for is following
There are a couple of ways you can use to pack up a template into a NuGet package,
The dotnet pack command and a.csproj
file. Alternatively, nuget pack command along with a.nuspec
file
I'm going for the second route, hence I have a .nuspec
file at the root of the project folder. To run nuget pack
command, you would need the Nuget.exe
. No worries! Just download it from, nuget.org/downloads.
So, I have two project folders under the Content
folder, each one containing a different flavor of Blazor
wired up with Fluxor
. You can get a better look at those here,
.NET template engine expects that you have a .template.config
folder on the root of your runnable project. To turn that runnable project into a template with some desired configurations, you better have a template.json
file under the .template.config
folder.
Pretty much everything is self-explanatory in the configuration file. To know more about what each of these flags does, refer to this official doc.
At this point, you are ready to install the templates locally using the following command,
> dotnet new --install .\BlazorFluxorTemplates\Content\
Template Name Short Name Language Tags
-------------------------------- ------------------ -------- -----------------------------
Blazor Fluxor Server Application blazorfluxorserver [C#] Web/Blazor/Fluxor
Blazor Fluxor Wasm Application blazorfluxorwasm [C#] Web/Blazor/Fluxor/WebAssembly
Time for creating the .nuspec
file,
The important thing to notice here is the <files>
node; You have to include the location of the template files here.
All done! Time to run the nuget pack
command to create a Nuget package (.nupkg
). I have downloaded Nuget.exe
in C:\
drive, so first I've to go inside the directory and then specify the location of the .nuspec
file following the pack
command.
Now that you have the Nuget package (.nupkg
), you can upload it the nuget.org so that the rest of the world can use your template.