Search code examples
.net-coredotnet-new

Exclude a project with a symbol switch for dotnet new template


I have a dotnet new template which I provide as a NuGet. It normally contains/creates two projects (with common package references, .editorconfig,...):

  • A console application project
  • A unit test project

Now I want to add a --notests (or, if possible, -notest, similar to how dotnet new webapi -minimal works) switch to the dotnet new command which should prevent the creation of the test project.

I have defined a symbol in template.json as follows:

{
  "$schema": "http://json.schemastore.org/template",
  "author": "me",
  "classifications": [ "Common", "Console" ],
  "identity": "Initech",
  "name": "Initech Console",
  "shortName": "iniconsole",
  "tags": {
      "language": "C#"
  },
  "symbols": {
    "notests": {
      "type": "parameter",
      "datatype": "bool",
      "defaultValue": "false",
      "description": "Do not create a unit test project"
    }
  },
  "sourceName": "IniConsole"
}

I know that I can use it in source files, for example in the IniConsole.sln file:

...

//#if (!notests)
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IniConsole.Test", "IniConsole.Test\IniConsole.Test.csproj", "{161D9B2A-4E8B-43B6-A77E-40BED559521F}"
EndProject
//#endif

...

How can I use this symbol to exclude the whole IniConsole.Test folder from the generated solution? Or is there a different way to accomplish that?


Solution

  • You can add sources property in your template.json file.

    Try adding this:

    "sources": [
      {
        "modifiers": [
          {
            "condition": "(!addtests)",
            "exclude": [
              "IniConsole.Test/**"
            ]
          }
        ]
      }
    ]