dotnet-template-samples
dotnet-template-samples copied to clipboard
Best way to handle symbols in packages.config.
Problem
I need to be able to optionally add/remove NuGet packages from a project based on a symbol's value. I would love if we had a sample that accomplished this, as I think it's a somewhat-common ask of a templating engine.
Potential Solutions
Outside of having multiple packages.config, what is a sufficient solution? You can't really markup a packages.config with symbols if you want the project to still be able to restore NuGets when opened.
I just had the same 'problem' and found a good solution. The templating engine has several SpecialOperationConfigs built in, among which is one for **/packages.config using the same syntax as for XML (see https://github.com/dotnet/templating/blob/master/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/SimpleConfigModel.cs#L435 for reference).
Inside of your packages.config simply add comments above and below the lines you want to be added (or removed) conditionally as below.
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--#if (myCondition)-->
<package id="System.Optionally.Included" version="42.0.0" targetFramework="net47" />
<!--#endif-->
</packages>
@johanbenschop has the correct approach.
I'd like to close this since I believe that it should be fixed with the comment previously posted. Is there still any issue that needs to be addressed here?