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 --listThat'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 blazorserverordotnet new blazorwasmto create a new project.
dotnet new blazorserver -o BlazorServer
dotnet new blazorwasm -o BlazorWasm- Install
Fluxor.Balzor.Weband wire it up in theStartup.csorProgram.cs
services.AddFluxor(opt =>
{
opt.ScanAssemblies(typeof(Program).Assembly);
opt.UseRouting();
opt.UseReduxDevTools();
});- Add a
Storefolder and add necessary code files forActions,Reducers,State,Feature,Effects, so on and so forth - Initialize the
Storein theApp.razor
<Fluxor.Blazor.Web.StoreInitializer />- Import the
<script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script>in_Host.razororindex.html
<script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script>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
BlazorFluxorTemplates
│ GeekHour.AspNetCore.BlazorFluxor.Templates.nuspec
│
└───Content
├───BlazorFluxorWasm
│ │ project files
│ │
│ └───.template.config
│ template.json
│
└───BlazorFluxorServer
│ project files
│
└───.template.config
template.jsonThere are a couple of ways you can use to pack up a template into a NuGet package,
The dotnet pack command and a.csprojfile. Alternatively, nuget pack command along with a.nuspecfile
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.
{
"$schema": "http://json.schemastore.org/template",
"author": "Geek Hour",
"classifications": [ "Web", "Blazor", "Fluxor" ],
"identity": "GeekHour.Web.BlazorFluxorServer.CSharp.6.0",
"name": "Blazor Fluxor Server Application",
"shortName": "blazorfluxorserver",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "BlazorFluxorServer",
"preferNameDirectory": true,
"defaultName": "BlazorFluxorServer",
"description": "Blazor server template wired with Fluxor"
}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/WebAssemblyTime for creating the .nuspec file,
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>GeekHour.AspNetCore.BlazorFluxor.Templates</id>
<version>1.0.1</version>
<description>
Project templates Blazor and Fluxor.
</description>
<authors>Geek Hour</authors>
<license type="expression">MIT</license>
<projectUrl>https://github.com/GeekyHours/BlazorFluxorTemplates</projectUrl>
<repository type="git" url="https://github.com/GeekyHours/BlazorFluxorTemplates" branch="main"/>
<tags>Blazor Fluxor Server WebAssembly Template</tags>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
<files>
<file src="Content\**\*.*" exclude="Content\**\bin\**\*.*;Content\**\obj\**\*.*" target="Content" />
</files>
</package>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.