3

Custom "dotnet new" Templates for Your Team

 3 years ago
source link: https://www.fiyazhasan.me/dotnet-new-custom-templates/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
June 23, 2021

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
1-1.pngdotnet new templates

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 or dotnet new blazorwasm to create a new project.
dotnet new blazorserver -o BlazorServer
dotnet new blazorwasm -o BlazorWasm
Blazor Server
icon
services.AddFluxor(opt =>
{
    opt.ScanAssemblies(typeof(Program).Assembly);
    opt.UseRouting();
    opt.UseReduxDevTools();
});
Startup.cs / Program.cs
  • Add a Store folder and add necessary code files for Actions, Reducers, State, Feature, Effects, so on and so forth
  • Initialize the Store in the App.razor
<Fluxor.Blazor.Web.StoreInitializer />
App.razor
  • Import the <script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script> in _Host.razor or index.html
<script src="_content/Fluxor.Blazor.Web/scripts/index.js"></script>
_Host.razor / index.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

default-package-icon-256x256.png

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.json

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,

BlazorFluxorTemplates

.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"
  }
template.json

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.

Time 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>
GeekHour.AspNetCore.BlazorFluxor.Templates.nuspec

The important thing to notice here is the <files> node; You have to include the location of the template files in 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.

3-1.png

Now that you have the Nuget package, you can upload it the nuget.org so that the rest of the world can use your template.

Useful Links:

logo-ms-social.png
templating
template-sample

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK