I'm working on a Xamarin template that makes use of the following preprocessor directives
```C#
[assembly: Application(Debuggable = true)]
[assembly: Application(Debuggable = false)]
``
The result is that only the else code is outputted to the template. Is there a way to force thedotnet new` to include the whole code block and not execute it while generating the template?
@Plac3hold3r - there are a couple ways to accomplish this. If the entire content of the file with this problem should always be copied to the template output without any processing, you can configure the template to just copy the file instead of processing it. This is accomplished in the template.json file, under the sources section, something like this:
"sources": [
"modifiers": [
{
"copyOnly": [
<path relative to template root of file to not be processed>,
<path to another file to not be processed>,
...
],
},
]
]
But if there are other parts of the file that need to be processed during template creation, we can add templating directives within the file to prevent the problematic section from being processed. For a .proj or *,msbuild file it would look like this:
...
<!--/-:cnd:noEmit -->
#if DEBUG
...
#endif
<!--/+:cnd:noEmit -->
...
If you need this for a different file type, the <!--/ and --> would get replaced by the file-appropriate affixes (let me know the file extension and I can give you the correct markup).
The first symbol I added is telling template processing to turn off conditional processing from this point forward. (that's the -:cnd part). The :noEmit part is saying that the symbol itself should not be copied to the output. Then at the end, the +:cnd is saying to turn conditional processing back on for the rest of the file.
Thanks @seancpeters it works perfectly 馃憤
As it was a C# .cs file I end up using
``` C#
//-:cnd:noEmit
[assembly: Application(Debuggable = true)]
[assembly: Application(Debuggable = false)]
//+:cnd:noEmit
```
Most helpful comment
@Plac3hold3r - there are a couple ways to accomplish this. If the entire content of the file with this problem should always be copied to the template output without any processing, you can configure the template to just copy the file instead of processing it. This is accomplished in the template.json file, under the sources section, something like this:
But if there are other parts of the file that need to be processed during template creation, we can add templating directives within the file to prevent the problematic section from being processed. For a .proj or *,msbuild file it would look like this:
If you need this for a different file type, the
<!--/and-->would get replaced by the file-appropriate affixes (let me know the file extension and I can give you the correct markup).The first symbol I added is telling template processing to turn off conditional processing from this point forward. (that's the
-:cndpart). The:noEmitpart is saying that the symbol itself should not be copied to the output. Then at the end, the+:cndis saying to turn conditional processing back on for the rest of the file.