1

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?

marce
  • 781
  • 1
  • 10
  • 20

1 Answers1

1

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

Try adding this:

"sources": [
  {
    "modifiers": [
      {
        "condition": "(!addtests)",
        "exclude": [
          "IniConsole.Test/**"
        ]
      }
    ]
  }
]
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • Thanks, works! One note: to _exclude_ we want the condition to be `(notests)` and not `(!notests)`. Maybe update your answer to make that clear. I decided to switch the flag name to `addtests` and set it to default `true`, condition to exclude is now `(!addtests)` which is better to read. – marce Apr 23 '23 at 10:07