Skip to content

Commit

Permalink
Added further extension methods to make them more readable.
Browse files Browse the repository at this point in the history
Preparing for 3.0 -> IVsUpdateSolutionEvents
  • Loading branch information
StefanKert committed Jan 7, 2019
1 parent 70932b1 commit 3a680f8
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/BuildVision/BuildVision.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
<Compile Include="Core\ServiceProviderPackage.cs" />
<Compile Include="Core\Services.cs" />
<Compile Include="Core\StatusBarNotificationService.cs" />
<Compile Include="Helpers\BuildEventContextExtensions.cs" />
<Compile Include="Helpers\BuildMessageEventArgsExtensions.cs" />
<Compile Include="Helpers\ProjectItemExtensions.cs" />
<Compile Include="Helpers\SolutionProjectsExtensions.cs" />
<Compile Include="Helpers\ProjectExtensions.cs" />
Expand Down
83 changes: 77 additions & 6 deletions src/BuildVision/Core/BuildVisionPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.Settings;
using VSLangProj;
using Task = System.Threading.Tasks.Task;

namespace BuildVision.Core
Expand All @@ -38,22 +39,26 @@ namespace BuildVision.Core
// This attribute registers a tool window exposed by this package.
[ProvideToolWindow(typeof(BuildVisionPane))]
[Guid(PackageGuids.GuidBuildVisionPackageString)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
//[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideBindingPath]
[ProvideBindingPath(SubPath = "Lib")]
// TODO: Add ProvideProfileAttribute for each DialogPage and implement IVsUserSettings, IVsUserSettingsQuery.
//// [ProvideProfile(typeof(GeneralSettingsDialogPage), SettingsCategoryName, "General Options", 0, 0, true)]
[ProvideProfile(typeof(GeneralSettingsDialogPage), SettingsCategoryName, "General Options", 0, 0, true)]
// TODO: ProvideOptionPage keywords.
[ProvideOptionPage(typeof(GeneralSettingsDialogPage), "BuildVision", "General", 0, 0, true)]
[ProvideOptionPage(typeof(WindowSettingsDialogPage), "BuildVision", "Tool Window", 0, 0, true)]
[ProvideOptionPage(typeof(GridSettingsDialogPage), "BuildVision", "Projects Grid", 0, 0, true)]
[ProvideOptionPage(typeof(BuildMessagesSettingsDialogPage), "BuildVision", "Build Messages", 0, 0, true)]
[ProvideOptionPage(typeof(ProjectItemSettingsDialogPage), "BuildVision", "Project Item", 0, 0, true)]
public sealed partial class BuildVisionPackage : AsyncPackage, IPackageContext
public sealed partial class BuildVisionPackage : AsyncPackage, IPackageContext, IVsUpdateSolutionEvents2
{
private DTE _dte;
private SolutionEvents _solutionEvents;
private BuildVisionPaneViewModel _viewModel;
private IVsSolutionBuildManager2 _solutionBuildManager;
private uint _updateSolutionEventsCookie;

private IVsSolution2 _vsSolution;

public ControlSettings ControlSettings { get; set; }

Expand All @@ -76,23 +81,44 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
mcs.AddCommand(menuToolWin);
}

_solutionBuildManager = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;
if (_solutionBuildManager != null)
{
_solutionBuildManager.AdviseUpdateSolutionEvents(this, out _updateSolutionEventsCookie);
}

_solutionEvents = _dte.Events.SolutionEvents;
_solutionEvents.AfterClosing += SolutionEvents_AfterClosing;
_solutionEvents.Opened += SolutionEvents_Opened;

var toolWindow = GetWindowPane(typeof(BuildVisionPane));
//var toolWindow = GetWindowPane(typeof(BuildVisionPane));
IPackageContext packageContext = this;
_viewModel = BuildVisionPane.GetViewModel(toolWindow);
ViewModelHelper.UpdateSolution(_dte.Solution, _viewModel.SolutionItem);
//_viewModel = BuildVisionPane.GetViewModel(toolWindow);
//ViewModelHelper.UpdateSolution(_dte.Solution, _viewModel.SolutionItem);
//var buildContext = new BuildContext(packageContext, _dte, _dte.Events.BuildEvents, _dte.Events.WindowEvents, _dte.Events.CommandEvents, viewModel);
}

private void SolutionEvents_Opened()
{
SetVsSolution();

ViewModelHelper.UpdateSolution(_dte.Solution, _viewModel.SolutionItem);
_viewModel.ResetIndicators(ResetIndicatorMode.ResetValue);
}

private void SetVsSolution()
{
if (_vsSolution == null)
_vsSolution = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution2;
}

private Project GetProject(IVsHierarchy pHierProj)
{
object objProj;
pHierProj.GetProperty(VSConstants.VSITEMID_ROOT, (int) __VSHPROPID.VSHPROPID_ExtObject, out objProj);
return objProj as Project;
}

private void SolutionEvents_AfterClosing()
{
_viewModel.TextCurrentState = Resources.BuildDoneText_BuildNotStarted;
Expand Down Expand Up @@ -141,5 +167,50 @@ public async Task ExecuteCommandAsync(string commandName)
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);
_dte.ExecuteCommand(commandName);
}

public int UpdateSolution_Begin(ref int pfCancelUpdate)
{
Console.WriteLine($"UpdateSolution_Begin");
return 0;
}

public int UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand)
{
Console.WriteLine($"UpdateSolution_Done: fSucceeded {fSucceeded}, fModified {fModified}, fCancelCommand {fCancelCommand}");
return 0;
}

public int UpdateSolution_StartUpdate(ref int pfCancelUpdate)
{
Console.WriteLine($"UpdateSolution_StartUpdate");
return 0;
}

public int UpdateSolution_Cancel()
{
Console.WriteLine($"UpdateSolution_Cancel");
return 0;
}

public int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{
var proj = GetProject(pIVsHierarchy);
Console.WriteLine($"OnActiveProjectCfgChange {proj.UniqueName}");
return 0;
}

public int UpdateProjectCfg_Begin(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, ref int pfCancel)
{
var proj = GetProject(pHierProj);
Console.WriteLine($"UpdateProjectCfg_Begin {proj.UniqueName}");
return 0;
}

public int UpdateProjectCfg_Done(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, int fSuccess, int fCancel)
{
var proj = GetProject(pHierProj);
Console.WriteLine($"UpdateProjectCfg_Done {proj.UniqueName}: dwAction {dwAction}, fSuccess {fSuccess}, fCancel {fCancel}");
return 0;
}
}
}
15 changes: 15 additions & 0 deletions src/BuildVision/Helpers/BuildEventContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Build.Framework;

namespace BuildVision.Helpers
{
public static class BuildEventContextExtensions
{
public static bool IsBuildEventContextInvalid(this BuildEventContext bec)
{
return (bec == null
|| bec == BuildEventContext.Invalid
|| bec.ProjectContextId == BuildEventContext.InvalidProjectContextId
|| bec.ProjectInstanceId == BuildEventContext.InvalidProjectInstanceId);
}
}
}
15 changes: 15 additions & 0 deletions src/BuildVision/Helpers/BuildMessageEventArgsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BuildVision.Tool.Building;
using Microsoft.Build.Framework;

namespace BuildVision.Helpers
{
public static class BuildMessageEventArgsExtensions
{
public static bool IsUserMessage(this BuildMessageEventArgs message, BuildOutputLogger loggerSender)
{
return (message.Importance == MessageImportance.High && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Minimal))
|| (message.Importance == MessageImportance.Normal && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Normal))
|| (message.Importance == MessageImportance.Low && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Detailed));
}
}
}
1 change: 1 addition & 0 deletions src/BuildVision/Helpers/SolutionProjectsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
using System.Runtime.InteropServices;
using EnvDTE80;
using BuildVision.UI.Helpers;

namespace BuildVision.Helpers
{
Expand Down
2 changes: 1 addition & 1 deletion src/BuildVision/Tool/BuildVisionPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override void OnClose()
private ControlView CreateControlView()
{
var packageContext = (IPackageContext)Package;
var viewModel = new BuildVisionPaneViewModel(new ControlModel(), null);//packageContext.ControlSettings);
var viewModel = new BuildVisionPaneViewModel(new ControlModel(), new UI.Settings.Models.ControlSettings());//packageContext.ControlSettings);
packageContext.ControlSettingsChanged += (settings) =>
{
viewModel.OnControlSettingsChanged(settings, buildInfo => BuildMessages.GetBuildDoneMessage(viewModel.SolutionItem, buildInfo, viewModel.ControlSettings.BuildMessagesSettings));
Expand Down
21 changes: 2 additions & 19 deletions src/BuildVision/Tool/Building/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class BuildContext : IBuildInfo, IBuildDistributor
private const int BuildInProcessQuantumSleep = 50;
private const string CancelBuildCommand = "Build.Cancel";

private IVsStatusbar _dteStatusBar;
private readonly object _buildingProjectsLockObject;
private readonly object _buildProcessLockObject = new object();
private readonly IVsItemLocatorService _locatorService;
Expand Down Expand Up @@ -101,13 +100,6 @@ public BuildContext(IVsItemLocatorService locatorService, IStatusBarNotification
_commandEvents.AfterExecute += CommandEvents_AfterExecute;
}

public async Task InitializeAsync(Microsoft.VisualStudio.Shell.IAsyncServiceProvider provider, CancellationToken cancellationToken)
{
_dteStatusBar = await provider.GetServiceAsync(typeof(IVsStatusbar)) as IVsStatusbar;
if (_dteStatusBar == null)
TraceManager.TraceError("Unable to get IVsStatusbar instance.");
}

public void OverrideBuildProperties(BuildActions? buildAction = null, BuildScopes? buildScope = null)
{
BuildAction = buildAction ?? BuildAction;
Expand Down Expand Up @@ -158,22 +150,13 @@ private void RegisterLogger()

private bool VerifyLoggerBuildEvent(BuildOutputLogger loggerSender, BuildEventArgs eventArgs, ErrorLevel errorLevel)
{
var bec = eventArgs.BuildEventContext;
bool becIsInvalid = (bec == null
|| bec == BuildEventContext.Invalid
|| bec.ProjectContextId == BuildEventContext.InvalidProjectContextId
|| bec.ProjectInstanceId == BuildEventContext.InvalidProjectInstanceId);
if (becIsInvalid)
if (eventArgs.BuildEventContext.IsBuildEventContextInvalid())
return false;

if (errorLevel == ErrorLevel.Message)
{
var messageEventArgs = (BuildMessageEventArgs)eventArgs;
bool isUserMessage = (messageEventArgs.Importance == MessageImportance.High && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Minimal))
|| (messageEventArgs.Importance == MessageImportance.Normal && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Normal))
|| (messageEventArgs.Importance == MessageImportance.Low && loggerSender.IsVerbosityAtLeast(LoggerVerbosity.Detailed));

if (!isUserMessage)
if (!messageEventArgs.IsUserMessage(loggerSender))
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/BuildVision/Tool/Building/BuildOutputLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static RegisterLoggerResult Register(Guid loggerId, LoggerVerbosity logge
| BindingFlags.Public
| BindingFlags.Instance;

BuildManager buildManager = BuildManager.DefaultBuildManager;
var buildManager = Microsoft.Build.Execution.BuildManager.DefaultBuildManager;
Type buildHostType = buildManager.GetType().Assembly.GetType("Microsoft.Build.BackEnd.IBuildComponentHost");
PropertyInfo loggingSeviceProperty = buildHostType.GetProperty("LoggingService", InterfacePropertyFlags);

Expand Down Expand Up @@ -98,4 +98,4 @@ public static RegisterLoggerResult Register(Guid loggerId, LoggerVerbosity logge
}
}
}
}
}

0 comments on commit 3a680f8

Please sign in to comment.