dotnet / templating

This repo contains the Template Engine which is used by dotnet new
https://docs.microsoft.com/dotnet/
MIT License
1.63k stars 369 forks source link

Template Pack installation not correct #2004

Closed msallin closed 5 years ago

msallin commented 5 years ago

Problem

I want to create a template pack, containing multiple templates. I followed the approach documented here: https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates When I install the resulting NuGet-Package, I get the following behavior.

1) dotnet new shows only one template ("Test.1") but dotnet new -u shows both ("Test.1" and "Test.2" from "AdatumCorporation.Utility.Templates"). 2) It is not possible to create a template using the short name with the fullname dotnet new Test.1 it works.

C:\temp>dotnet new
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.

Templates                                         Short Name         Language          Tags
----------------------------------------------------------------------------------------------------------------------------
Console Application                               console            [C#], F#, VB      Common/Console
Class library                                     classlib           [C#], F#, VB      Common/Library
Unit Test Project                                 mstest             [C#], F#, VB      Test/MSTest
NUnit 3 Test Project                              nunit              [C#], F#, VB      Test/NUnit
NUnit 3 Test Item                                 nunit-test         [C#], F#, VB      Test/NUnit
xUnit Test Project                                xunit              [C#], F#, VB      Test/xUnit
Test.1                                            test               [C#]              Web
Razor Page                                        page               [C#]              Web/ASP.NET
MVC ViewImports                                   viewimports        [C#]              Web/ASP.NET
MVC ViewStart                                     viewstart          [C#]              Web/ASP.NET
ASP.NET Core Empty                                web                [C#], F#          Web/Empty
ASP.NET Core Web App (Model-View-Controller)      mvc                [C#], F#          Web/MVC
ASP.NET Core Web App                              webapp             [C#]              Web/MVC/Razor Pages
ASP.NET Core with Angular                         angular            [C#]              Web/MVC/SPA
ASP.NET Core with React.js                        react              [C#]              Web/MVC/SPA
ASP.NET Core with React.js and Redux              reactredux         [C#]              Web/MVC/SPA
Razor Class Library                               razorclasslib      [C#]              Web/Razor/Library/Razor Class Library
ASP.NET Core Web API                              webapi             [C#], F#          Web/WebAPI
global.json file                                  globaljson                           Config
NuGet Config                                      nugetconfig                          Config
Web Config                                        webconfig                            Config
Solution File                                     sln                                  Solution

Examples:
    dotnet new mvc --auth Individual
    dotnet new sln
    dotnet new --help
C:\temp>dotnet new -u
Template Instantiation Commands for .NET Core CLI

Currently installed items:
  Microsoft.DotNet.Common.ItemTemplates
    Templates:
      global.json file (globaljson)
      NuGet Config (nugetconfig)
      Solution File (sln)
      Web Config (webconfig)
  Microsoft.DotNet.Common.ProjectTemplates.2.2
    Templates:
      Class library (classlib) C#
      Class library (classlib) F#
      Class library (classlib) VB
      Console Application (console) C#
      Console Application (console) F#
      Console Application (console) VB
  Microsoft.DotNet.Test.ProjectTemplates.2.2
    Templates:
      Unit Test Project (mstest) C#
      Unit Test Project (mstest) F#
      Unit Test Project (mstest) VB
      xUnit Test Project (xunit) C#
      xUnit Test Project (xunit) F#
      xUnit Test Project (xunit) VB
  Microsoft.DotNet.Web.ItemTemplates
    Templates:
      Razor Page (page) C#
      MVC ViewImports (viewimports) C#
      MVC ViewStart (viewstart) C#
  Microsoft.DotNet.Web.ProjectTemplates.2.2
    Templates:
      ASP.NET Core Empty (web) C#
      ASP.NET Core Empty (web) F#
      Razor Class Library (razorclasslib) C#
      ASP.NET Core Web App (webapp) C#
      ASP.NET Core Web App (Model-View-Controller) (mvc) C#
      ASP.NET Core Web App (Model-View-Controller) (mvc) F#
      ASP.NET Core Web API (webapi) C#
      ASP.NET Core Web API (webapi) F#
  Microsoft.DotNet.Web.Spa.ProjectTemplates
    Templates:
      ASP.NET Core with Angular (angular) C#
      ASP.NET Core with React.js (react) C#
      ASP.NET Core with React.js and Redux (reactredux) C#
  NUnit3.DotNetNew.Template
    Templates:
      NUnit 3 Test Project (nunit) C#
      NUnit 3 Test Item (nunit-test) C#
      NUnit 3 Test Project (nunit) F#
      NUnit 3 Test Item (nunit-test) F#
      NUnit 3 Test Project (nunit) VB
      NUnit 3 Test Item (nunit-test) VB
  AdatumCorporation.Utility.Templates
    Templates:
      Test.1 (test) C#
      Test.2 (test2) C#
C:\temp>dotnet new test1
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.

Test.1 (C#)
Author: .NET Core Workgroup
    (No Parameters)

Repro

I've the following directory structure. The content of MyDotnetTemplates.csproj is the same as on https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates. The content of template.json is the following.

C:\TEMP2
│   MyDotnetTemplates.csproj
│
└───templates
    ├───mytemplate1
    │   └───.template.config
    │           template.json
    │
    └───mytemplate2
        └───.template.config
                template.json
{
  "$schema": "http://json.schemastore.org/template",
  "author": ".NET Core Workgroup",
  "classifications": [
    "Web"
  ],
  "name": "Test.2",
  "identity": "Test.2",
  "groupIdentity": "Test",
  "shortName": "test2",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "sourceName": "Blueprint",
  "preferNameDirectory": true,
  "sources": [
    {
      "modifiers": [
        {
          "exclude": [
            ".vs/**",
            ".git/**",
            "**/bin/**",
            "**/obj/**",
            "**/TestResults/**",
            "**/coverage.opencover.xml",
            "**/coveragereport"
          ]
        }
      ]
    }
  ]
}
{
  "$schema": "http://json.schemastore.org/template",
  "author": ".NET Core Workgroup",
  "classifications": [
    "Web"
  ],
  "name": "Test.1",
  "identity": "Test.1",
  "groupIdentity": "Test",
  "shortName": "test",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "sourceName": "Blueprint",
  "preferNameDirectory": true,
  "sources": [
    {
      "modifiers": [
        {
          "exclude": [
            ".vs/**",
            ".git/**",
            "**/bin/**",
            "**/obj/**",
            "**/TestResults/**",
            "**/coverage.opencover.xml",
            "**/coveragereport"
          ]
        }
      ]
    }
  ]
}

dotnet --info

C:\>dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.2.301
 Commit:    70d6be0814

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.301\

Host (useful for support):
  Version: 2.2.6
  Commit:  7dac9b1b51

.NET Core SDKs installed:
  1.0.4 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.507 [C:\Program Files\dotnet\sdk]
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.1.701 [C:\Program Files\dotnet\sdk]
  2.2.104 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  2.2.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
phenning commented 5 years ago

You have the same groupIdentity for both templates, so it will only be listed once.

GroupIdentity serves as a rollup to group together templates which are semantically the same template, but may have different symbols to differentiate. For example, the templates contained in the following template packs, all have the same groupIdentity, but each one has a distinct id and only supports one version of netcoreapp via the Framework symbol each one defines.

Microsoft.DotNet.Web.ProjectTemplates.2.2 Microsoft.DotNet.Web.ProjectTemplates.2.1 Microsoft.DotNet.Web.ProjectTemplates.2.0

You can either add something to allow the template engine to disambiguate your templates from each other, set the groupIdentity to something unique between them if they are really distinct templates, or remove the groupIdentity entirely.