Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Build Order of Visual Studio solution to a text file

Is there a way to output the Build order to a text file via command line?

To explain: We use multiple branches of source and have large solutions of 100+ projects on each branch. I need to write build scripts to build these solutions from command line. We can then tailor the solutions on the branches to only have project references for the projects that team are working on. This should greatly increase solution load time and ease the frustration of the Developers and me, I hope :)

I'm going to keep looking and maybe look at using C# and the APIs provided with VS. We are using 2012 update 1.

like image 877
mikeyg Avatar asked Mar 01 '13 12:03

mikeyg


2 Answers

This is a good candidate for a Visual Studio Plugin project.

  1. Create a new Visual Studio Add-in project.
  2. In the project creation wizard make sure you choose the following configuration in the Choose Add-in Options step (the other steps are not important, I'm assuming you'll use C#):

enter image description here

  1. In the Connect.cs file, add the following fields:

    private BuildEvents _buildEvents;
    private Events _events;
    private bool buildEventConnected = false;
    
  2. And add / modify these methods accordingly:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        _events = _applicationObject.Events;
        _buildEvents = _events.BuildEvents;
    
        if (connectMode != ext_ConnectMode.ext_cm_UISetup && !buildEventConnected)
        {
            _buildEvents.OnBuildDone +=
                new _dispBuildEvents_OnBuildDoneEventHandler(BuildEvents_OnBuildDone);
            buildEventConnected = true;
        }
    }
    
    private void BuildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)
    {
        const string BUILD_OUTPUT_PANE_GUID = "{1BD8A850-02D1-11D1-BEE7-00A0C913D1F8}";
        TextDocument txtOutput = default(TextDocument);
        TextSelection txtSelection = default(TextSelection);
        Window vsWindow = default(Window);
        vsWindow = _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
        OutputWindow vsOutputWindow = default(OutputWindow);
        OutputWindowPane objBuildOutputWindowPane = default(OutputWindowPane);
        vsOutputWindow = (OutputWindow)vsWindow.Object;
        foreach (OutputWindowPane objOutputWindowPane in vsOutputWindow.OutputWindowPanes)
        {
            if (objOutputWindowPane.Guid.ToUpper() == BUILD_OUTPUT_PANE_GUID)
            {
                objBuildOutputWindowPane = objOutputWindowPane;
                break;
            }
        }
        txtOutput = objBuildOutputWindowPane.TextDocument;
        txtSelection = txtOutput.Selection;
        txtSelection.StartOfDocument(false);
        txtSelection.EndOfDocument(true);
        objBuildOutputWindowPane.OutputString(System.DateTime.Now.ToString());
        txtSelection = txtOutput.Selection;
        var solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
        System.IO.File.WriteAllText(solutionDir + "\\build_output.log", txtSelection.Text);
    }
    
    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
    {
        if (buildEventConnected)
        {
            _buildEvents.OnBuildDone -= new _dispBuildEvents_OnBuildDoneEventHandler(BuildEvents_OnBuildDone);
            buildEventConnected = false;
        }
    }
    

That's it, on every build you'll have the output sent to the build_output.log file in your solution's folder.

like image 74
Alex Filipovici Avatar answered Oct 05 '22 08:10

Alex Filipovici


Quick way is to do a "Clean Solution" so that you can see the inverted order in build log.

like image 31
TuneFanta Avatar answered Oct 05 '22 06:10

TuneFanta