Talk With Angel Garcia!

Talk With Angel Garcia!

a blog about software and beers

Creating your own Xamarin projects templates using .NET Core CLI

Hi folks! It’s been a long time since my last post but I’m back, this time we are going to learn how to build our own templates for .NET projects, in our example will be a Xamarin template.

This post is part of the Code Snippets’s Xamarin Month, make sure to check all the amazing posts.

Have you ever wonder how to make a reusable boilerplate that includes all the features and architecture that you use for almost every new app?, for example in VS for Mac we don’t have a project template for Prism and we end up setting up the same configuration manually, let’s solve this problem making a template with a Clean Architecture structure and Prism (soon I will be posting about this architecture in Xamarin).

This is the base project I’ll use to make the template, it’s called PrismCleanApp.

Step 1

First things first, we need to install the .NET CLI, starting with .NET Core 2.0, you can create your own custom templates for any type of project.

To check if everything is ok, open a terminal and type:

dotnet --version

Step 2

Basically we just need to create a configuration file (template.json) inside a folder named .template.config in the root of your project source files.

The template.json should have the following structure:

{
    "author": "Angel Garcia",
    "classifications": [ "Multiplatform" ],
    "name": "Clean Architecture Structure for Xamarin Forms Apps using Prism",
    "identity": "CleanArchitectureXamarinPrism", 
    "shortName": "cleanxam",                 
    "tags": {
        "language": "C#"                      
    },
    "sourceName": "YourProjectRootNamespace", 
    "preferNameDirectory" : "true"
}

When we see this file our attention goes for the following properties:

  • identity: the Id for our template, should be unique.

  • shortName: normally use as a shorthand for the identity (we are going to use it later for create the new projects based on this template).

  • sourceName: is the name of the base project root namespace, in my case is PrismCleanApp, this is used by te CLI to replace ALL the occurrences that matches the value for the name you give to the projects that will use this template.

  • preferNameDirectory: this is used to indicate that if we don’t set a name when creating a new project using this template the name will be the same as the directory where you run the command.

Step 3

Now that we are clear about the template file, let’s use the following command with the help of the CLI:

dotnet new -i ROOT_BASE_PROJECT_PATH

Where -i is a short way for —install and ROOT_BASE_PROJECT_PATH is the folder path where our template is.

The output will be the list of your installed templates including our CleanArchitectureXamarinPrism (cleanxam):

So, now we are ready to create a new project based on our template, again using the CLI, go to the terminal and type:

dotnet new cleanxam -n MyNewProject -o MyNewProject

-n for the name of your project and -o for the output folder’s name.

That’s all! We now have our new project with all the cool stuffs that we used in our template!

Notice all the namespaces changed to our new project’s name!

Finally

Open your SLN with Visual Studio (for Mac in my case) and enjoy.

With this we could have the setup for Plugins, Push Notifications, Splash Screens, App Center Analytics & Crashing reports, BaseViewModels and all your common stuffs just making a base project with all those features and creating a template 😎.

See the code on Github

DROP A COMMENT

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.