在dotnet new模板中添加非C#文件的可选内容

12

我希望根据开发人员在从模板创建C#解决方案时选择的内容来修改README.md的内容。我该怎么做?

我知道可以定义

"symbols": {
    "EnableContent":{
        "type": "parameter",
        "dataType":"bool",
        "defaultValue": "true"
    }
}

template.config/template.json 中启用 dotnet new 模板的可选内容。

在 C# 代码中,如果将 EnableContent 设置为 true(使用 C# 预处理器指令),则可以使用定义的符号来包含一些代码。

#if (EnableContent)
        public string foo()
        {
            return "bar";
        }

#endif

而在 .cshtml 文件中,它可以被用作:

Translated text:

而在 .cshtml 文件中,它可以被用作:

@*#if (EnableContent)
<p>foobar</p>
#endif*@

有没有办法在非C#文件中添加类似的决策制定,例如markdown文件(.md)?或者这是否取决于可用的C#预处理器指令?如果是这样,是否有任何解决方法可以在使用dotnet new的模板的上下文中在markdown文件中使用C#预处理器指令。

附言:我知道在这个例子中我可以做两个不同版本的README.md文件,然后使用源代码修改器选择正确的版本。

"sources": [
    {
        "modifiers": [
            {
                "condition": "(EnableContent)",
                "exclude": [ "README1.md" ]
            },
            {
                "condition": "(!EnableContent)",
                "exclude": [ "README2.md" ]
            }
        ]
    }

template.config/template.json中,但我的需求比那更复杂。

1个回答

24
我最终自己解决了这个问题。有一种方法叫做SpecialCustomOperations,可以定义自己的“语言”,以启用任何文本文件中的可选内容。
这是一个文档不太好的功能,但我从这个答案中发现了在markdown文件中启用SpecialCustomOperations的巨大价值。
template.config/template.json中需要定义。
"SpecialCustomOperations": {
  "**/*.md": {
    "operations": [
      {
        "type": "conditional",
        "configuration": {
          "if": ["---#if"],
          "else": ["---#else"],
          "elseif": ["---#elseif", "---#elif"],
          "endif": ["---#endif"],
          "trim" : "true",
          "wholeLine": "true",
        }
      }
    ]
  }
}

接着进行以下工作

---#if (FooBar)
Foo bar
---#elif (BarBaz)
Bar baz
---#else
Baz qux
---#endif

此外,我发现您可以为csproj文件(基本上是xml)定义类似的操作。在那里,您需要定义(按照此评论中的示例)。
"SpecialCustomOperations": {
"**/*.xaml": {
  "operations": [
    {
      "type": "conditional",
      "configuration": {
        "actionableIf": [ "<!--#if" ],
        "actionableElse": [ "#else", "<!--#else" ],
        "actionableElseif": [ "#elseif", "<!--#elseif" ],
        "endif": [ "#endif", "<!--#endif" ],
        "trim" : "true",
        "wholeLine": "true",
      }
    }
  ]
}
}

PS. 默认情况下启用特殊操作的文件类型列表可以在这里找到。在其他文件类型中,需要手动定义SpecialCustomOperations。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接