Is there any MSBuild Interface available?

Unanswered Is there any MSBuild Interface available?

  • Saturday, January 06, 2007 1:31 AM
     
     

    Hi all,

    Has anybody had call MSBuild via api interface from win form application? Currently, I'm able to call msbuild.exe from win form using process.start() method; however, I think it would be nice if I could call msbuild directly from api interface.

    Best Regards,

All Replies

  • Saturday, January 06, 2007 4:09 AM
     
     

    There is object model for MSBuild that you can use (see MSDN for reference).

    What you probably need is Engine object to build you projects.

    Hope that helps.

    Regards, Eugene

  • Monday, January 08, 2007 4:54 PM
     
     

    Thanks so much eugene. I'll give a try.

    Best Regards,

  • Monday, January 08, 2007 5:45 PM
     
     

    Eugene,

    I've tried and it seems work; however, I do have 2 issue:

    1. how do I pass in target and configuration parameter? For an example, in command line I could passing /t:build /p:configuration=debug. Below is my code

    2. how do I display compile output to rich textbox?

    public void PerformBuild (string s_solutionfile)

    {

    // Instantiate a new Engine object

    Engine engine = new Engine();

    // Point to the path that contains the .NET Framework 2.0 CLR and tools

    engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";

    // Instantiate a new FileLogger to generate build log

    FileLogger logger = new FileLogger();

    // Set the logfile parameter to indicate the log destination

    logger.Parameters = @"logfile=C:\temp\build.log";

    // Register the logger with the engine

    engine.RegisterLogger(logger);

    string[] s_test = targetNames;

    // Build a project file

    bool success = engine.BuildProjectFile(s_solutionfile);

    //Unregister all loggers to close the log file

    engine.UnregisterAllLoggers();

    if (success)

    {

    richTextBox_DisplayOutput.AppendText("\n");

    richTextBox_DisplayOutput.AppendText("EXECUTED COMMAND SUCCESSFULL: ........" + SELECTED_BUILD_CONFIG + "\n");

    richTextBox_DisplayOutput.AppendText("********************************************************************************************" + "\n");

    richTextBox_DisplayOutput.AppendText("\n");

    }

    else

    {

    richTextBox_DisplayOutput.AppendText("\n");

    richTextBox_DisplayOutput.AppendText("ERROR EXECUTING COMMAND: ........ " + SELECTED_BUILD_CONFIG + "\n");

    richTextBox_DisplayOutput.AppendText("********************************************************************************************" + "\n");

    richTextBox_DisplayOutput.AppendText("\n");

    }

    }

  • Monday, January 08, 2007 10:40 PM
     
     

    Hi All,

    I've search project and buildpropertygroup class but not help. I can't find anything that I could set configuration property. Could any body please help me out or point me to some usefull resource? I'm very appreciated.

    Best Regards,

  • Tuesday, January 09, 2007 6:23 AM
     
     

    Andy,

    To pass parameters to build, you may want to use GlobalProperties property of Engine class.

    As to redirecting output to rich text control, you may want to implement custom logger - basicallly what you need is to develop your own implementation of ILogger interface, and register you implementation with an engine using RegisterLogger method Engine class.

    Do not be discouraged - the tasks are doable, though as you are starting to find out, the documentation on MSBuild extensibility is very poor, and object model has lots of issues.

    Best regards, Eugene

  • Tuesday, January 09, 2007 12:15 PM
     
     
    Use Project.GlobalProperties.SetProperty ("Configuration","debug").
  • Tuesday, January 09, 2007 5:36 PM
     
     

    Thanks so much for all of your responded.

    Here are what I tried, but still no luck

    ====================================

          public void PerformBuild (string s_solutionfile, string s_targetName, string s_propertyGroup)
          {
                // Instantiate a new Engine object
                Engine engine = new Engine();
             
                engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";
               

                // Instantiate a new FileLogger to generate build log
                FileLogger logger = new FileLogger();

                // Set the logfile parameter to indicate the log destination
                logger.Parameters = @"logfile=C:\temp\build.log";

                // Register the logger with the engine
                engine.RegisterLogger(logger);

                engine.GlobalProperties.SetProperty(Configuration, "Debug");
               

                // Build a project file
                bool success = engine.BuildProjectFile(s_solutionfile, new string[] { "Build" }, engine.GlobalProperties);


                //Unregister all loggers to close the log file
                engine.UnregisterAllLoggers();

               if (success)
                {
     //               Console.WriteLine("Build succeeded.");
                        richTextBox_DisplayOutput.AppendText("\n");
                        richTextBox_DisplayOutput.AppendText("EXECUTED COMMAND SUCCESSFULL:     ........" + SELECTED_BUILD_CONFIG + "\n");
                        richTextBox_DisplayOutput.AppendText("********************************************************************************************" + "\n");
                        richTextBox_DisplayOutput.AppendText("\n");
                }
                else
                {
      //              Console.WriteLine(@"Build failed. View C:\temp\build.log for details");
                        richTextBox_DisplayOutput.AppendText("\n");
                        richTextBox_DisplayOutput.AppendText("ERROR EXECUTING COMMAND:     ........ " + SELECTED_BUILD_CONFIG + "\n");
                        richTextBox_DisplayOutput.AppendText("********************************************************************************************" + "\n");
                        richTextBox_DisplayOutput.AppendText("\n");
                }

         }

    =========================================================================

    This I got error message: "The name "Configuration" does not exist in the current context

  • Wednesday, January 10, 2007 12:48 AM
     
     

    Thanks all.

    I got it

    It should be

    Engine.GlobalProperties.SetProperty("Configuration", "Debug");