0

I have been automating all my builds through PSake and finding it really useful to get my TeamCity build more powerful.

One thing I would like to introduce is a step to make sure all projects in a solution have warnings as errors set to true. It isn't that often that I spot a project not having this but I am also now working on a legacy codebase where it is not so prevalent.

I was hoping to be able to create a quick check that the setting was on. I am guessing there would be a way by checking for the correct text (

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

) in the .*proj files but I hoped there might be a cleaner or more effective way.

Any ideas?

ArtificialGold
  • 825
  • 5
  • 14

1 Answers1

1

I don't understand what's wrong with probing the .*proj files, so I'll assume you wouldn't like to just search for a whole string literal.

Project files are XML based, so here's a Linq to XML code snippet that will let you find what you're looking for:

void VerifyTreatWarningsAsErrorsIsOn( string projFilename )
{
    XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";
    var twaeList = XDocument.Load( projFilename ).Descendants( xmlns + "TreatWarningsAsErrors" ).ToList();

    if ( twaeList.Count() > 0 )
    {
        var falseValues = twaeList.Where( x => x.Value == "true" );
        if ( falseValues.Count() > 0 )
        {
                // Not good..
        }
    }
    else
    {
        // None exist.. not good either..
    }
}

One thing that's a bit off in your problem domain is that you might have project files, which have the flag switched on for one configuration (e.g. debug), but you won't be able to tell if it even exists in other configurations.

So actually, you can extend the above code and traverse all configurations and look for the flag per configuration. Moreover - if the configuration doesn't exist, or is set to 'false' - you can just manipulate the XML document and insert / fix all these problems on the fly.

HTH.

AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32
  • Thanks for the code, looks like it could be a good approach. I guess I was hoping there was a known technique through a tool or something. I guess I will create my own, possibly using your code here. In response to your different configurations point, I was guessing I could check for the setting in the All Configurations section and check it is nowhere else. – ArtificialGold Mar 09 '12 at 15:21