ZeroSharp

Robert Anderson's ones and zeros

Persisting Changes to Config Files Within NuGet Packages

| Comments

Whenever NuGet updates or restores a NuGet package, the config files within it are overwritten. Here’s a method to make sure the changes are reapplied via a config transform whenever the solution is built.

I’m using the NUnit.Runners NuGet packages. To get our coverage tool to play nicely, I need to replace <supportedRuntime "v2.0.50727"> with <supportedRuntime "v4.0.30319"> within the NUnit-console-x86.exe.config.

Normally, a config transform is for modifying the web.config or app.config files. Here, we need to modify a config file within the packages subdirectory.

In my .csproj file, I have added the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <PropertyGroup>
   <NUnitRunnerDir>$(SolutionDir)packages\NUnit.Runners.2.6.3\tools\</NUnitRunnerDir>
  <PropertyGroup>

  <!-- Default NUnit test runner requires a modification to the config file-->
  <UsingTask
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

  <Target Name="AfterBuild" Condition="exists('$(NUnitRunnerDir)NUnit-console-x86.exe.config')">
    <TransformXml
        Source="$(NUnitRunnerDir)NUnit-console-x86.exe.config"
        Destination="$(NUnitRunnerDir)NUnit-console-x86.exe.config"
        Transform="$(SolutionDir)UnitTests\Transforms\NUnit-console-x86.exe.CLR4.config" />
  </Target>

And the transform file itself looks like this:

NUnit-console-x86.exe.CLR4.config
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <startup>
    <supportedRuntime version="v4.0.30319" xdt:Transform="Replace" />
  </startup>
</configuration>

By the way, Web.config Transformation Tester is a handy tool!

Now whenever I build the project, the AfterBuild event ensures the supportedRuntime version is set correctly.

Comments