ASP.NET Core MVC from scratch to achieve the development of plug-in (two) - How to create a project template

Original: scratch to achieve the development of plug-in ASP.NET Core MVC (b) - How to create a project template

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.configdirectory, and add a template.json file. It reads as follows:

 
 
Copy
{ "author": "Lamond Lu", "classifications": [ "Web/Plugins" ], "name": "DemoPlugin1", "identity": "LamondDynamicPlugins", "shortName": "ldp", "tags": { "language": "C#" , "type":"project" }, "sourceName": "DemoPlugin1", "preferNameDirectory": true }

Configuration instructions:

  • AuthorAttribute specifies the author of the template, Required
  • classificationsAttribute specifies classification template Required
  • nameRepresents the name of the template, when you use dotnet newwhen you create a project, specify the template name required
  • identityIt represents a unique name for the template, optional
  • shortNameIt represents the short name for the template, Required
  • tagsSome label specified for the current template
  • sourceNameSpecify a string, when dotnet newthe time specified parameter -n, -n parameter values, replace sourceNamethe 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 current dotnet newcommand 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 DemoPlugin2
  • preferNameDirectoryWhether the build directory of the same name, is optional.

For template.jsondetailed 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-CLIcommand to install the project template.

 
 
Copy
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 DemoPlugin1has emerged as a template in the list.

Use dotnet newto create a project #

When the template is installed locally, we can use the dotnet newcommand to create a project, for example, we now want to create a new plug-in, we can use the following command.

 
 
Copy
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.csprojfile has been renamed DemoPlugin2.csproj, indicating that prior to template.jsonthe specified file sourceNameattributes 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:reinitcommand, as used herein dotnet new -ucommand may not be removed, dotnet new -uapply 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

 
 
Copy
<?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 packageTypemust be set Template.

After you write good metadata file, we can use the Nuget packcommand to package, this is the first parameter command to specify a .nuspecpath to the file is located.

 
 
Copy
nuget pack DemoPlugin1/LamondDynamicPlugin.nuspec

After running, Nuget will generate a LamondDynamicPlugin.nupkgfile, this is what we call Nuget package.

To install the template package Nuget way, we can use the same commanddotnet new -i

 
 
Copy
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.nuspecto install the template a unique id defined. Installation of command and from the directory before mounting template is the same.

 
 
Copy
dotnet new -i LamondDynamicPlugin

If you want to uninstall a template, just use the dotnet new -u [Nuget包]can.

 
 
Copy
dotnet new -u LamondDynamicPlugin

Summary #

In this article I demonstrate how dotnet-CLIto 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.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11904714.html