Use .NET CORE create project templates, project templates, Template

Scene: daily work, you may encounter need to create a new solution of the case (such as the new company began a new project, the need for a new package of daemon), if more internal basic framework, solutions need to DDD mode, then from the new project to the various references to be able to rely on actually available, a lot of configurations require re-setting, testing, time-consuming, depending on the size of the project, often may take 1-2 hours or more Long.

Before .net core, although related solutions can be achieved "project templates" This demand, but very convenient when specific operations, starting from the .net core 1.0, provides a "template engine," added dotnet new - install (-i) commands and options, this command lets you easily create your own project template.

In this article you can learn and master:

  1. Learn how an existing solution project as a project template.
    1. Learn how to create a project template locally and install and use.
    2. How to master the local template packaged into nuget package, and install it by using the template package id.
  2. Understand and master the simple dotnet and nuget command and its configuration. (Windows and mac will do differences explain)

Ready to work

The following configuration items (DDD):

You can in my github repository:
https://github.com/ArtechChu/Template
directly download the template source code

  • For the project issued a total of two, Template.Console and Template.WebApi
    • Console project which is simply a reference to the other project outputs.
    • WebApi project under a simple configuration dependency injection, you can use this project as a template projects api

Console Project Summary:

WebApi Project Summary:

The local project as a local template, install and use the command

  1. The examples to Console, for example, the console project related to the project to copy the following folder:

  2. 手动创建一个名为“.template.config”的文件夹,并在该文件夹内创建文件:template.json

    {
      "$schema": "http://json.schemastore.org/template",
      "author": "Artech",
      "classifications": [ "Console" ],
      "name": "Custom Console",
      "identity": "Custom Console", //模板唯一标识
      "groupIdentity": "Custom Console", 
      "shortName": "CustomConsole", //【修改】短名称,使用 dotnet new <shortName> 安装模板时的名称
      "tags": {
        "language": "C#", 
        "type": "project" 
      },
      "sourceName": "Template", //【修改】在使用 -n 选项时,会替换模板中项目的名字
      "preferNameDirectory": true
    }
    • 这里主要说明下 shortName 和 sourceName 这 2 个属性。
      • shortName:短名称,用于在使用“dotnet new -l”命令时显示,安装时也可直接根据该短名称进行安装。
      • sourceName:当我们在使用"dotnet new" 命令进行安装时,如果指定了 -n 或者 -o 选项,那么选项后面的名字会自动替换 sourceName 中指定的名字,因为我们的项目命名规则是 "Template.XXXX",所以这里设定为“Template”,如果你的项目命名规则是“公司.项目.XXX”,那么这里请设定为“公司.项目”。
  3. 安装该模板到本地模板库

    # 通过如下命令查看当前本机已安装模板:
    dotnet  new  -l 

    # 模板安装命令:dotnet  new  i <path | nugetId>
    # 这里因为是安装本地模板,直接使用路径(绝对和相对均可)
    dotnet  new  -i  .

  4. 安装该短名称为 CustomConsole 的模板

    假定安装路径为 D:\TestTemplate
    假定新起的项目名为“Company.Group”

    # 这里使用 -n 和 -o 选项来分别指定新项目的名字以及输出目录
    # 设定新项目的名字为“Company.Group”,因为当前定位已经在 TestTemplate 文件夹内,所以直接用“.”,如下:
     dotnet   new   CustomConsole   -n   Company.Group   -o   .

    文件夹内容如下:

    • 使用模板新建的项目文件夹自动为“Company.Group.XXXX”

    测试:

    更多关于 template.json 的说明请参考:http://json.schemastore.org/template

    • 在 template.json 中,你还可以指定 symbols 等,来实现更多的自定义功能,如联动预编译指令等等。

将本地项目打包为 nuget 包,并通过命令进行安装和使用

本次示例以 Console +WebApi 为例,在 Templates\Nuget 文件夹中,建立 Content 文件夹用于存放 nuget 包内容,具体如下:

  • ConsoleTemplate 中的 .template.config\template.json 内容同上方 Console 示例。
  • WebApiTemplate 中的 .template.config\template.json 内容如下:

    {
      "$schema": "http://json.schemastore.org/template",
      "author": "Artech",
      "classifications": [ "WebApi" ],
      "name": "Custom WebApi",
      "identity": "Custom WebApi",
      "groupIdentity": "Custom WebApi",
      "shortName": "CustomWebApi",
      "tags": {
        "language": "C#",
        "type": "project"
      },
      "sourceName": "Template",
      "preferNameDirectory": true
    }
  1. 在 content 目录内创建一个 nuspec 文件:Custom.Template.NetCore.nuspec,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
      <metadata>
        <id>Custom.Template.NetCore</id>
        <version>1.0.1</version>
        <description>
          Custom Template, including WebApi, Console
        </description>
        <authors>Artech</authors>
        <packageTypes>
          <packageType name="Template" />
        </packageTypes>
      </metadata>
    </package>
    • 需要注意,packageType 为 Template,metadata.id 必须保证唯一,其他按需设置即可。
    • 必须是在 content 文件夹内。nuget 在打包的时候,是根据 content 文件夹来进行的。
  2. 使用 nuget pack 命令打包

    # 注意路径的相对位置
    nuget   pack   Custom.Template.NetCore.nuspec   -OutputDirectory   .

  • 打包后的内容为:

  1. 发布该 nuget 包到 nuget server

    这里用的是自建 nuget server,你可以按自身情况打包上传。

    • 你可以直接使用 Nuget Package Explorer 进行发布包
    • 也可以使用 nuget push 来发布,如下:
    nuget push Custom.Template.NetCore.1.0.1.nupkg -Source "你的nuget 服务 url" -ApiKey "你的nuget api key"
  2. 通过 nuget 安装模板到本地

  • 安装前本地已经安装的模板如下:

  • 安装

    dotnet new -i Custom.Template.NetCore::*

  1. 通过模板安装 CustomWebApi

    安装路径为:D:\TestWebApiTemplate

    dotnet  new  CustomWebApi  -n  Company.Group  -o  .
  2. 创建一个解决方案,并将所有的项目添加到解决方案 Company.Group.sln 中

    dotnet new sln -n Company.Group
    # windows 下无法使用 glob pattern 只能逐个添加
    dotnet sln Company.Group.sln add Company.Group.Application\Company.Group.Application.csproj
    dotnet sln Company.Group.sln add Company.Group.Domain\Company.Group.Domain.csproj
    dotnet sln Company.Group.sln add Company.Group.DomainService\Company.Group.DomainService.csproj
    dotnet sln Company.Group.sln add Company.Group.IApplication\Company.Group.IApplication.csproj
    dotnet sln Company.Group.sln add Company.Group.IDomainService\Company.Group.IDomainService.csproj
    dotnet sln Company.Group.sln add Company.Group.Infrastructure.CrossCutting\Company.Group.Infrastructure.CrossCutting.csproj
    dotnet sln Company.Group.sln add Company.Group.Repository\Company.Group.Repository.csproj
    dotnet sln Company.Group.sln add Company.Group.WebApi\Company.Group.WebApi.csproj

    如果你用的是 mac / linux ,则可以直接用 globbing pattern 来添加,如下:

    dotnet sln Company.Group.sln add **/*.csproj

参考

https://devblogs.microsoft.com/dotnet/how-to-create-your-own-templates-for-dotnet-new/

https://github.com/dotnet/dotnet-template-samples

https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore22

https://docs.microsoft.com/en-us/nuget/install-nuget-client-tools

Guess you like

Origin www.cnblogs.com/deepthought/p/11373537.html