Title: scratch plug-in development to achieve ASP.NET Core MVC (b) - How to create a project template
Author: Lamond Lu
Address: https://www.cnblogs.com/lwqlun/p/11155666.html
Source: HTTPS : //github.com/lamondlu/Mystique
In the last article, we introduced through one of the most simple examples to demo it for a moment, how to develop a plug-in ASP.NET Core Mvc With ApplicationPart in. At last, we have made a number of problems to be solved.
First of all it, first to solve the problem is, every time we create a new plug-in, each time re-creating projects, add library references, and manually modify csproj file, very tedious. Is there a way to reduce this part of the operation?
Recall that when every time we create a project using Visual Studio, we can choose a project template, Visual Studio can not only create a project based on the template of our choice, and we need an assembly reference will be pre-configured, no we need to repeat the configuration. So if we can create some of your own templates yet? The answer is yes.
In this paper, I come to you show you, how to create a project template for .NET Core project, streamline operations.
Write template.json #
If you want to create a project template, we need to do two things.
- Creating a project code as a template
- Write template.json file
Here we have previously created a DemoPlugin1 of the project, so this time we can directly convert it into a template project.
First, we add a project in the current .template.config
directory, and add a template.json file. It reads as follows:
{
"author": "Lamond Lu",
"classifications": [ "Web/Plugins" ],
"name": "DemoPlugin1",
"identity": "LamondDynamicPlugins",
"shortName": "ldp",
"tags": {
"language": "C#" ,
"type":"project"
},
"sourceName": "DemoPlugin1",
"preferNameDirectory": true
}
Configuration instructions:
Author
Attribute specifies the author of the template, Requiredclassifications
Attribute specifies classification template Requiredname
Represents the name of the template, when you usedotnet new
when you create a project, specify the template name requiredidentity
It represents a unique name for the template, optionalshortName
It represents the short name for the template, Requiredtags
Some label specified for the current templatesourceName
Specify a string, whendotnet new
the time specified parameter -n, -n parameter values, replacesourceName
the specified string. The current project, we project file names and use all the namespace are DemoPlugin1, but if we can not generate new projects are using this name, so we need to use the -n parameter specifies the name of the project to replace " DemoPlugin1 ", if the currentdotnet new
command specifies the -n parameter value DemoPlugin2, when the project will be created when all of the namespace in the project and the project file name, will use the new DemoPlugin2preferNameDirectory
Whether the build directory of the same name, is optional.
For
template.json
detailed configuration list, interested students can view the following link <https://github.com/dotnet/templating/wiki/Reference-for-template.json>
Create a template using the CLI-DOTNET #
After completion of writing code, we can use the following dotnet-CLI
command to install the project template.
dotnet new -i <PATH>
Here
<PATH>
you specify the directory where the current template project
After running the command, the output of a new local list of templates, we can find that DemoPlugin1
has emerged as a template in the list.
Use dotnet new
to create a project #
When the template is installed locally, we can use the dotnet new
command to create a project, for example, we now want to create a new plug-in, we can use the following command.
dotnet new ldp -n DemoPlugin2
After running command, the corresponding code is generated out. Came to the project directory, we found that the previous DemoPlugin1.csproj
file has been renamed DemoPlugin2.csproj
, indicating that prior to template.json
the specified file sourceName
attributes help me we correct replacement project file name.
Then, we can see a unique code controllers, its namespace is also properly replaced.
how about it? This is not a simple ratio manually before a lot?
Note: If you want to uninstall the template you just installed, you need to use the
dotnet new --debug:reinit
command, as used hereindotnet new -u
command may not be removed,dotnet new -u
apply only to download from nuget.org down the template package
How templates packaged into Nuget package #
In addition to these projects use this directory to create project templates way, we can also be packaged as a template for others to use Nuget package.
If you want to create a template, where we first need to create a metadata file Nuget package, here we namedLamondDynamicPlugin.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>LamondDynamicPlugin</id>
<version>1.0.0</version>
<description>
Creates dynamic plugin in asp.net core
</description>
<authors>Lamond Lu</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
Properties set here are very simple, only caveat
packageType
must be setTemplate
.
After you write good metadata file, we can use the Nuget pack
command to package, this is the first parameter command to specify a .nuspec
path to the file is located.
nuget pack DemoPlugin1/LamondDynamicPlugin.nuspec
After running, Nuget will generate a LamondDynamicPlugin.nupkg
file, this is what we call Nuget package.
To install the template package Nuget way, we can use the same commanddotnet new -i
dotnet new -i LamondDynamicPlugin.nupkg
Of course, in general, we will publish this Nuget package to nuget.org, so you can share the template to other people.
After publishing to Nuget, we can use it LamondDynamicPlugin.nuspec
to install the template a unique id defined. Installation of command and from the directory before mounting template is the same.
dotnet new -i LamondDynamicPlugin
If you want to uninstall a template, just use the dotnet new -u [Nuget包]
can.
dotnet new -u LamondDynamicPlugin
Summary #
In this article I demonstrate how dotnet-CLI
to create a .NET Core project templates, and how to package Nuget project template for others to use, this would resolve the project to create a plug-in manually before we troublesome problem. The next issue, I will try to abstract out the business logic module, create a core class libraries, stay tuned.