如何在成功构建完成后获得通知?

Dro*_*per 10 add-in build visual-studio envdte

我正在编写一个VS加载项,我需要在成功构建后运行某个方法.我尝试过使用dte.Events.BuildEvents.OnBuildDone但即使构建失败也会发生这种情况.

我应该使用房产或其他活动吗?

Han*_*ant 13

OnBuildDone事件无法告诉您发生了什么.解决方案中的某些项目可能已正确构建,有些则没有.你需要OnBuildProjConfigDone.为每个项目触发,Success参数告诉您它是否有效.


rya*_*man 6

通常,您需要处理正在构建的多个项目.这可以是解决方案构建,也可以构建依赖于另一个项目的项目.

因此,要确定成功构建何时完成,您需要使用两个构建事件的组合:

OnBuildProjConfigDone和OnBuildDone.

您还需要一个成员变量来跟踪整体构建状态.

您将为每个构建的项目调用OnBuildProjConfigDone处理程序,并传递一个bool来告诉您该项目构建是否成功.将此结果分配给您的成员变量以跟踪整体状态.

最后,将调用您的OnBuildDone处理程序.在这里,您可以查看您的成员变量以查看是否有任何项目构建失败.

以下是我为VS2012编写的扩展中的一些示例代码.该扩展提供了一个"自定义构建"命令,用于构建活动项目,并在构建成功时启动调试器.

private bool _overallBuildSuccess;
private bool _customBuildInProgress;

private void CustomBuild_MenuItemCallback(object sender, EventArgs e)
{
    // Listen to the necessary build events.
    DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
    dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
    dte.Events.BuildEvents.OnBuildProjConfigDone += BuildEvents_OnBuildProjConfigDone;

    try
    {
        // Build the active project.
        _customBuildInProgress = true;
        dte.ExecuteCommand("Build.BuildSelection");
    }
    catch (COMException)
    {
        _customBuildInProgress = false;
        WriteToOutputWindow("Build", "Could not determine project to build from selection");
    }
}

private void BuildEvents_OnBuildProjConfigDone(string project, string projectConfig, string platform, string solutionConfig, bool success)
{
    // Ignore this build event if we didn't start it.
    if (!_customBuildInProgress)
    {
        return;
    }

    // Keep track of the overall build success.
    _overallBuildSuccess = success;
}

private void BuildEvents_OnBuildDone(EnvDTE.vsBuildScope scope, EnvDTE.vsBuildAction action)
{
    // Ignore this build event if we didn't start it.
    if (!_customBuildInProgress)
    {
        return;
    }

    _customBuildInProgress = false;

    if (_overallBuildSuccess)
    {
        // Launch the debugger.
        DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
        dte.ExecuteCommand("Debug.Start");
    }
    else
    {
        WriteToOutputWindow("Build", "Custom build failed.");
    }
}

private void WriteToOutputWindow(string paneName, string message)
{
    DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));

    Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
    OutputWindow outputWindow = (OutputWindow)window.Object;

    OutputWindowPane targetPane = outputWindow.OutputWindowPanes.Cast<OutputWindowPane>()
        .FirstOrDefault(x => x.Name.ToLower() == paneName.ToLower());

    if (targetPane == null)
    {
        targetPane = outputWindow.OutputWindowPanes.Add(paneName);
    }

    targetPane.Activate();
    outputWindow.ActivePane.OutputString(message);
    outputWindow.ActivePane.OutputString(Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)