dotnet-template-samples icon indicating copy to clipboard operation
dotnet-template-samples copied to clipboard

Best way to handle symbols in packages.config.

Open pierceboggan opened this issue 8 years ago • 2 comments

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.

pierceboggan avatar Jun 19 '17 19:06 pierceboggan

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 avatar Jan 12 '18 12:01 johanbenschop

@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?

sayedihashimi avatar May 16 '18 21:05 sayedihashimi