Unlocking Efficiency with Custom Project Templates
As developers, we often find ourselves working on multiple projects that share common coding standards and setup requirements. Custom project templates can significantly streamline this process, offering several benefits:
Why Templates Matter
- Jumpstart Development: Skip repetitive setup tasks and focus on building features.
- Customizable: Align with your team's coding standards and architecture.
- Consistency: Provides a solid foundation for any .NET project.
Creating Custom Templates
To create a custom template, follow these steps:
- Create a `.template.config` folder in your project root.
- Add a `template.json` file inside this folder.
{
"$schema": "http://json.schemastore.org/template",
"author": "Your Name",
"classifications": [ "WebAPI", "Swagger", "Application Insights" ],
"identity": "apitemplate.Template",
"name": "My Dotnet API Template",
"shortName": "apitemplate",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "apitemplate",
"preferNameDirectory": true,
"sources": [
{
"modifiers": [
{
"condition": "(!exclude)",
"include": "**/*",
"exclude": [
"**/[Bb]in/**",
"**/[Oo]bj/**",
".template.config/**/*"
]
}
]
}
]
}
Understanding the template.json
Here's a breakdown of the `template.json` properties:
- $schema: Specifies the JSON schema for validation.
- author: The template creator's name.
- classifications: Categories for template discovery.
- identity: Unique identifier for the template.
- name: User-friendly template name.
- shortName: Command-line friendly name for `dotnet new`.
- tags: Additional template information.
- sourceName: Token to be replaced in source files.
- preferNameDirectory: Whether to create a new directory for the project.
- sources: Describes how source files should be processed.
Installing and Managing Templates
To install your template:dotnet new install .\
To overwrite an existing template:
dotnet new install
To uninstall a template:
dotnet new uninstall
Take a Look
I've created a GitHub repository with step-by-step instructions on how to create and install custom templates. Check it out here:🚀 GitHub repo for source code
Beyond NuGet Packages
While NuGet packages are great for sharing common code, templates can include these packages by default, eliminating the need for additional setup instructions for your teammates.
Create a custom template for your organization's standards to ease project setup by providing boilerplate code.
Sharing Templates with Your Team
To share templates:
1. Add tag IsPackable with value true to your .csproj file.2. Create a NuGet package:
dotnet pack
3. Share the resulting `.nupkg` file with your team.
4. Team members can install the template using:
dotnet new install path-to-your-package.nupkg
This is how it looks in visual studio.
0 Comments