Search code examples
xmlmsbuildslowcheetahxdt-transform

MSBuild condition to prevent app.config XML transform


I am using the 'SlowCheetah' VS extension to transform our app.config with different values depending on the project configuration. So a 'Debug' configuration produces an app.config with values appropriate for Dev/Qa users, while a 'Release' build produces an app.config with production values.

The .csproj contains a section like this:

<ItemGroup>
<None Include="App.config">
  <SubType>Designer</SubType>
  <TransformOnBuild>true</TransformOnBuild>
</None>
<None Include="App.Debug.config">
  <DependentUpon>App.config</DependentUpon>
  <IsTransformFile>True</IsTransformFile>
  <SubType>Designer</SubType>
</None>    
<None Include="App.Release.config">
  <DependentUpon>App.config</DependentUpon>
  <IsTransformFile >True</IsTransformFile>    
</None>
<None Include="packages.config" />
<None Include="Properties\SlowCheetah\SlowCheetah.Transforms.targets" />

And the msbuild logic is contained mostly in the 'SlowCheetah.Transforms.targets' file. My files are being transformed correctly.

I want to protect against a developer accidentally running a 'Release' build inside Visual Studio and inadvertently running my app with a production config file. My idea is to use an msbuild condition, possibly something like:

Condition=" '$(BuildingInsideVisualStudio)'=='true' "

I've tried using this condition in several places in the .csproj file without success. I suspect I could get this to work if I modify the 'SlowCheetah.Transforms.targets' file itself, but that file should not be modified according to the comments at the top.

Ideally I would like ALL configurations for builds inside Visual Studio to use my Debug config file, and 'Release' builds outside Visual Studio (such as builds on a continuous integration server) to use the Prod app.config, but I'd settle for being able to prevent accidental running of a 'Release' build inside Visual Studio. Any suggestions as to if/how this can be achieved are appreciated.


Solution

  • Add this just before </Project>:

      <Target Name="BeforeBuild">
        <Error Condition=" '$(BuildingInsideVisualStudio)'=='true' And '$(Configuration)'=='Release' "
               Text="JMc is so mad at you you trying to build using the Release configuration from Visual Studio." />
      </Target>