views:

88

answers:

2

My dilemma is that I need to compile a large amount of projects and solutions as per the configuration settings specified in the files. I've found a few different ways to do this, and I've landed on using the Microsoft.Build.BuildEngine.Engine class, which has recently become deprecated.

Here's some sample code that illustrates its use: (note that node.Path is the path to the project file)

var builder = new Engine { BinPath = @"C:\Windows\Microsoft.NET\Framework\v3.5" };
var logger = new FileLogger { Parameters = @"logfile=" + Path.Combine(logdir, Path.GetFileName(node.Path)) + ".txt" };

compiler.RegisterLogger(logger);
bool success = compiler.BuildProjectFile(node.Path);
compiler.UnregisterAllLoggers();

return success;

My problem - it compiles in debug :(

Looking through the members of the class hasn't helped much as there aren't that many properties exposed. The one hint was the PropertyGroup property which seems to allow the setting of some project build options.. however it isn't clear how to get the resulting call to BuildProjectFile to output in release.

I'd really appreciate any help with this!

+1  A: 

I don't understand why you need to constantly compile. Settings files as you mention should be in app.config or web.config (depending it's a webservice). Settings files should not change the compilation of the app code. If it is I suggest you change your Programming model to allow for changing settings without recompiling.

greektreat
Perhaps I worded it wrong. The idea is my program, when ran, will compile a ton of projects and solutions, and this program needs to be ran at every major version change. Settings files aren't really a concern, unless they have pre-build or post-build tasks to copy dlls, run unit tests, etc.
Stefan Valianu
+3  A: 

Do you have to do this through code? You may find it easier to use something like NAnT, along with NAntContrib, the MSBuild task is fairly comprehensive and makes it quite easy to specify which target to build, for example:

  <msbuild project="${ProjectBasePath}\${ProjectName}.sln">
    <property name="Configuration" value="debug"/>
  </msbuild>

You can also call MSBuild from the command line, for example:

msbuild.exe project.proj /t:rebuild /p:Configuration=Debug

The advantage nant would give you, over msbuild is that you can script your build process. I, personally, find it quite easy to get on with and you can get a lot of power and flexibility out of it.

You can also call Visual Studio directly:

devenv.exe /Rebuild release "ProjectName.sln" /Out "c:\vs_errors.txt"

This will also output build output to c:\vs_errors.txt

Rob
@Stefan - I've added some info on how to call VS to build and receive the output to a text file. I've also just checked the command line args for msbuild, `/fileLogger` `/fileLoggerParameters` are the parameters you'd be after
Rob