I have an .editorconfig file with the following conditional in it:
...omitted
##if (StyleCop)
##########################################
# StyleCop
##########################################
...omitted
#endif
...omitted
In my template.json file, I have then added the following boolean option:
"StyleCop": {
"type": "parameter",
"datatype": "bool",
"description": "Adds and enforces StyleCop analyzers.",
"defaultValue": "true"
},
When I create a project using dotnet new and turn off the StyleCop option, the .editorconfig file failes to remove the code above. Is there a bug with files that start with a . or have no file extension?
As with all things in .NET, look what the F# people do! https://github.com/SAFE-Stack/SAFE-template/blob/master/Content/.template.config/template.json. Using this as an example, I’ve been able to put conditionals in Dockerfiles. Look at line 655 where they define conditionals to be used in ‘fs’ files.
@brettrowberry You're awesome! Thanks.
There should still probably be some more support for common file extensions like .editorconfig and Dockerfile built into the templating engine. So I'll leave this open but it's nice to know we can add custom types.
Looking into this a bit more. I can't see what the format is for this config. I'm trying to work out what the syntax is for specifying 'actionable' conditionals i.e. the ones you want to uncomment. I found this in the source code:
ConditionalTokens tokens = new ConditionalTokens
{
IfTokens = new[] { "#if" }.TokenConfigs(),
ElseTokens = new[] { "#else" }.TokenConfigs(),
ElseIfTokens = new[] { "#elseif", "#elif" }.TokenConfigs(),
EndIfTokens = new[] { "#endif", "##endif" }.TokenConfigs(),
ActionableIfTokens = new[] { "##if" }.TokenConfigs(),
ActionableElseIfTokens = new[] { "##elseif", "##elif" }.TokenConfigs(),
ActionableElseTokens = new[] { "##else" }.TokenConfigs(),
ActionableOperations = new[] { replaceOperationId, uncommentOperationId }
};
IOperationProvider[] operations =
{
new Conditional(tokens, true, true, CppStyleEvaluatorDefinition.Evaluate, null, true),
new Replacement("##".TokenConfig(), "#", uncommentOperationId, false),
new Replacement("#".TokenConfig(), "", replaceOperationId, false),
};
It sounds like sln files didn’t work out of the box at some point, but now they do, so more can definitely be built in.
I'm trying to work out what the syntax is for specifying 'actionable' conditionals
What do you mean?
I got both .editorconfig and Dockerfile working like so:
"SpecialCustomOperations": {
"**/.editorconfig": {
"operations": [
{
"type": "conditional",
"configuration": {
"if": [ "#if" ],
"else": [ "#else" ],
"elseif": [ "#elseif" ],
"endif": [ "#endif" ],
"actionableIf": [ "##if" ],
"actionableElse": [ "##else" ],
"actionableElseif": [ "##elseif" ],
"actions": [ "uncomment", "reduceComment" ],
"trim": "true",
"wholeLine": "true",
"evaluator": "C++"
}
},
{
"type": "replacement",
"configuration": {
"original": "#",
"replacement": "",
"id": "uncomment"
}
},
{
"type": "replacement",
"configuration": {
"original": "##",
"replacement": "#",
"id": "reduceComment"
}
}
]
},
"**/Dockerfile": {
"operations": [
{
"type": "conditional",
"configuration": {
"if": [ "#if" ],
"else": [ "#else" ],
"elseif": [ "#elseif" ],
"endif": [ "#endif" ],
"actionableIf": [ "##if" ],
"actionableElse": [ "##else" ],
"actionableElseif": [ "##elseif" ],
"actions": [ "uncomment", "reduceComment" ],
"trim": "true",
"wholeLine": "true",
"evaluator": "C++"
}
},
{
"type": "replacement",
"configuration": {
"original": "#",
"replacement": "",
"id": "uncomment"
}
},
{
"type": "replacement",
"configuration": {
"original": "##",
"replacement": "#",
"id": "reduceComment"
}
}
]
}
},
Thank you very much for providing a custom configuration for .editorconfig!
This is exactly the way to deal with non-default types of files. There is nothing more to do about this issue.
Concerning adding default configurations for different types of files (including .editorconfig), please, refer to issue #282.
We will track it there, no need for a duplicate.