Back to home

Adding a project into the Helix Solution Structure with dotnet new custom template

Overview

Setting up the SXA Solution structure sometimes would be tedious for the one who is new to the SXA and the solution setup for the same. It needs quite lot of manual work including virtual folder creation for the Feature, Foundation, Project and adding them into the solution.

What is the solution ?

  • The solution is to use the dotnew template to create the custom template with the basic project
  • Pack them as a nuget package to reuse
  • With the powershell script execute the template name to create a project under the desired folder( Feature/Foundation/Project) and add the same into the main solution file

Getting Started…!!!

Creating the Custom dotnet templates

We are going to create a custom dotnet template which we can reuse with creating the helix projects.

I am going to put all the custom templates underneath the folder templates inside my solution repo as below.

Above the two folder represents I have two dotnet custom templates to be created. The one is for the basic class library project and another one is for the unit test project for the same.

The module folder will contain the class library project. You need to create a simple class library project and move the required files into this module folder as shown below.

Now we have the list of files which has to be the part of our new custom template. Next we will be configuring the template config file.

This template config file is important and this will require for the .NET to understand our custom template. It must present in the root folder of our custom template like below.

This folder will be having a json file named template.json where we will be mentioning the custom template configurations. I have mentioned the json file below,

Note:- You can read more about the template config from here

The highlighted shortName is the one which we will be using with the CLI/Powershell to install the custom template into our solution.

Repeat the above to create the resources for the test project template into the folder tests. It will look like below.,

Having the above steps completed, we would have the necessary files to create the template. Let us move on to see how to create the template…

Creating the template pack

As the name itself conveys the template pack is combination of templates clubbed together. This template pack is represented by the .nupkg file. Hence it can be installed into the machine like downloading the package from the nuget repo and use it.

Naturally the .csproj is used to compile and create the binary of the respective project. We specify the resources which has to be part of the output binary only via the .csproj file. This .csproj is not only used to create the binary file but also can be used to create the template pack.

To do so the following .csproj file has to be created in the root folder of the dotnetcustomtemplate which we created earlier.

The content of the .csproj file is as follow.

The section would deal with the property of the nuget file. The properties like PackageVersion, Id, Author etc., value will be used to install/uninstall this particular template from the nuget feed. For more info about these properties, you can refer here.

The section tells we need to include all the files from the folder except the binaries. Since we do not need them. Now we have all the configurations done to create the template pack.

We need to use the below .NET CLI command to create the template package from the folder where the .csproj file exists.

dotnet pack

This command will create the template package and the same can be found from folder /bin/debug. The name of the package will be LearnHelix.NetNewTemplates.1.0.0.nupkg. You can copy paste this template package into the root folder and delete the bin/obj folder as they are no longer needed.

Now we have the template package ready and we need to install them to be used with the dotnet CLI commands. You need to run the below command to get it installed,

dotnet new -i LearnHelix.NetNewTemplates.1.0.0.nupkg

The powershell output is given below. Once its installed, list of all the available templates will be displayed. We can see that our custom template is also available for the use.

At any time the custom template can also be uninstalled from the system using the command,

dotnet new -u

Preparing the Powershell Script and to create the Projects with Helix Solution

Let us see how to use the custom templates with the .dotnet CLI commands to create the projects.

dotnet new learnhelix-project -n "$SolutionName.$Layer.$Name" -o "./src/$Layer/$Name" -la "$Layer"
dotnet sln (Resolve-Path -Path "$SolutionName.sln").Path add -s "$Layer\$Name" "./src/$Layer/$Name/code/$SolutionName.$Layer.$Name.csproj"

You can notice that the package id learnhelix-project is used here ( this is the name which we configured when we create the .json file for the template package).

$SolutionName is a custom variable – For instance, let us say Learn.Helix ( this is the base solution name)
$Layer – Feature/Foundation/Project
$Name – Name of the project that we are creating

So this particular command will create a .csproj file in the above format and add the same into the solution. This is the idea basically. The final powershell will look something like., I name this as add-module.ps and place it under the tools folder.

So far we have seen how to create the custom template pack. Also we have created the two custom templates for our use. Let us see how to use them with the powershell script to create the projects on-the-fly.

For this I have created an empty solution and name it Learn.Helix.sln ( remember this is the same name as $SolutionName). You can notice that its have 0 projects with it.

Now we have run the powershell script to add the projects as needed with/without Unit Tests.

You can notice that I have given the input values,
Name of the project – Multisite, Layar – Feature and Y to Unit Tests ( as I want Unit Test project also to be created for the same)

After the scripts runs successfully, if you go and check the solution, you would notice that there is a Multisite project created under the Feature layer of Helix solution with Unit testing enabled.

In this way the effort to setup and creating the project with SXA solution can be simplified 🙂

You can refer my github for more details on this.