Skip to content

Commit

Permalink
update to make use of asyncpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKert committed Dec 27, 2018
1 parent 55c7f2d commit 3bb9a5d
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 194 deletions.
3 changes: 2 additions & 1 deletion src/BuildVision.UI/Contracts/IBuildDistributor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BuildVision.Contracts;
using System;
using System.Threading.Tasks;

namespace BuildVision.UI.Contracts
{
Expand All @@ -13,6 +14,6 @@ public interface IBuildDistributor
event EventHandler<BuildProjectEventArgs> OnBuildProjectDone;
event EventHandler<BuildErrorRaisedEventArgs> OnErrorRaised;

void CancelBuild();
Task CancelBuildAsync();
}
}
6 changes: 4 additions & 2 deletions src/BuildVision/BuildVision.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<Reference Include="System.Xaml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\BuildVisionPackage.Package.cs" />
<Compile Include="Core\BuildVisionPackage.cs" />
<Compile Include="Helpers\ProjectItemExtensions.cs" />
<Compile Include="Helpers\SolutionProjectsExtensions.cs" />
<Compile Include="Helpers\ProjectExtensions.cs" />
Expand Down Expand Up @@ -121,7 +121,6 @@
<Compile Include="Tool\Tool.cs" />
<Compile Include="Tool\ToolWindow.cs" />
<Compile Include="Core\PackageGuids.cs" />
<Compile Include="Core\BuildVisionPackage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Core\PackageIds.cs" />
<Compile Include="Helpers\ToolWindowManager.cs" />
Expand Down Expand Up @@ -223,6 +222,9 @@
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.12.0">
<Version>12.0.30111</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime">
<Version>14.3.26929</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
<Version>15.9.3032</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
98 changes: 0 additions & 98 deletions src/BuildVision/Core/BuildVisionPackage.Package.cs

This file was deleted.

108 changes: 78 additions & 30 deletions src/BuildVision/Core/BuildVisionPackage.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,97 @@
using System;

using System.ComponentModel.Design;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using BuildVision.Common;
using BuildVision.Tool;
using BuildVision.Tool.Building;
using BuildVision.UI;
using BuildVision.UI.Common.Logging;
using BuildVision.UI.Settings.Models;
using BuildVision.Views.Settings;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.Settings;
using BuildVision.Common;
using BuildVision.Tool.Building;
using BuildVision.UI.Settings.Models;
using BuildVision.UI.ViewModels;
using BuildVision.Tool;
using BuildVision.UI.Common.Logging;
using System.Windows;
using Task = System.Threading.Tasks.Task;

namespace BuildVision.Core
{
public partial class BuildVisionPackage : IPackageContext
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
// a package.
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
// This attribute is used to register the informations needed to show the this package
// in the Help/About dialog of Visual Studio. Resources are defined in VSPackage.resx.
//[InstalledProductRegistration("#110", "#112", BuildVisionVersion.PackageVersion, IconResourceID = 400)]
// This attribute is needed to let the shell know that this package exposes some menus.
[ProvideMenuResource("Menus.ctmenu", 1)]
// This attribute registers a tool window exposed by this package.
[ProvideToolWindow(typeof(ToolWindow))]
[Guid(PackageGuids.GuidBuildVisionPackageString)]
[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)]
// TODO: ProvideOptionPage keywords.
[ProvideOptionPage(typeof(GeneralSettingsDialogPage), settingsCategoryName, "General", 0, 0, true)]
[ProvideOptionPage(typeof(WindowSettingsDialogPage), settingsCategoryName, "Tool Window", 0, 0, true)]
[ProvideOptionPage(typeof(GridSettingsDialogPage), settingsCategoryName, "Projects Grid", 0, 0, true)]
[ProvideOptionPage(typeof(BuildMessagesSettingsDialogPage), settingsCategoryName, "Build Messages", 0, 0, true)]
[ProvideOptionPage(typeof(ProjectItemSettingsDialogPage), settingsCategoryName, "Project Item", 0, 0, true)]
public sealed partial class BuildVisionPackage : AsyncPackage, IPackageContext
{
private const string settingsCategoryName = "BuildVision";
private const string settingsPropertyName = "Settings";
private DTE _dte;

public ControlSettings ControlSettings { get; set; }

public DTE2 GetDTE2() => (DTE2)GetService(typeof(DTE));

public DTE GetDTE() => (DTE)GetService(typeof(DTE));
public BuildVisionPackage()
{
string hello = string.Format("{0} {1}", Resources.ProductName, "BuildVisionVersion.PackageVersion");
TraceManager.Trace(hello, EventLogEntryType.Information);
}

public IVsUIShell GetUIShell() => (IVsUIShell)GetService(typeof(IVsUIShell));
public event Action<ControlSettings> ControlSettingsChanged = delegate { };

public IVsSolution GetSolution() => (IVsSolution)GetService(typeof(IVsSolution));
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);

public IVsStatusbar GetStatusBar() => (IVsStatusbar)GetService(typeof(SVsStatusbar));
_dte = await GetServiceAsync(typeof(DTE)) as DTE;
if (await GetServiceAsync(typeof(IMenuCommandService)) is OleMenuCommandService mcs)
{
var toolwndCommandId = new CommandID(PackageGuids.GuidBuildVisionCmdSet, (int) PackageIds.CmdIdBuildVisionToolWindow);
var menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandId);
mcs.AddCommand(menuToolWin);
}

public ToolWindowPane GetToolWindow() => GetWindowPane(typeof(ToolWindow));
ControlSettings = LoadSettings(this);
var toolWindow = GetWindowPane(typeof(ToolWindow));
IPackageContext packageContext = this;
var viewModel = ToolWindow.GetViewModel(toolWindow);
var buildContext = new BuildContext(packageContext, _dte, _dte.Events.BuildEvents, _dte.Events.WindowEvents, _dte.Events.CommandEvents, viewModel);
var tool = new Tool.Tool(packageContext, _dte, (IVsWindowFrame) toolWindow.Frame, buildContext, buildContext, viewModel);
}

private void ToolInitialize()
private void ShowToolWindow(object sender, EventArgs e)
{
try
{
ControlSettings = LoadSettings(this);
var toolWindow = GetToolWindow();
IPackageContext packageContext = this;
var viewModel = ToolWindow.GetViewModel(toolWindow);
var buildContext = new BuildContext(packageContext, viewModel);
var tool = new Tool.Tool(packageContext, buildContext, buildContext, viewModel);
// Get the instance number 0 of this tool window. This window is single instance so this instance
// is actually the only one.
// The last flag is set to true so that if the tool window does not exists it will be created.
var window = FindToolWindow(typeof(ToolWindow), 0, true);
if (window == null || window.Frame == null)
throw new InvalidOperationException(Resources.CanNotCreateWindow);

var windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
catch (Exception ex)
{
Expand Down Expand Up @@ -81,16 +128,13 @@ private static ControlSettings LoadSettings(IServiceProvider serviceProvider)
}
catch (Exception ex)
{
ex.Trace("Error when trying to load settings: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
ex.Trace("Error when trying to load settings: " + ex.Message, EventLogEntryType.Error);
MessageBox.Show("An error occurred when trying to load current settings. To make sure everything is still working the settings are set to default.");
}

return new ControlSettings();
}

/// <remarks>
/// Settings are stored under "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\[12.0Exp]\BuildVision\".
/// </remarks>
private static void SaveSettings(ControlSettings settings, IServiceProvider serviceProvider)
{
var store = GetWritableSettingsStore(serviceProvider);
Expand All @@ -102,13 +146,17 @@ private static void SaveSettings(ControlSettings settings, IServiceProvider serv
store.SetString(settingsCategoryName, settingsPropertyName, value);
}

public async Task ExecuteCommandAsync(string commandName)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);
_dte.ExecuteCommand(commandName);
}

private static WritableSettingsStore GetWritableSettingsStore(IServiceProvider serviceProvider)
{
var shellSettingsManager = new ShellSettingsManager(serviceProvider);
var writableSettingsStore = shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
return writableSettingsStore;
}

public event Action<ControlSettings> ControlSettingsChanged = delegate { };
}
}
17 changes: 4 additions & 13 deletions src/BuildVision/Core/IPackageContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;

using EnvDTE;

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE80;
using BuildVision.UI.Settings.Models;

using Task = System.Threading.Tasks.Task;

namespace BuildVision.Core
{
public interface IPackageContext
Expand All @@ -15,13 +12,7 @@ public interface IPackageContext
void SaveSettings();
event Action<ControlSettings> ControlSettingsChanged;
void NotifyControlSettingsChanged();

DTE GetDTE();
DTE2 GetDTE2();
ToolWindowPane GetToolWindow();
IVsUIShell GetUIShell();
IVsSolution GetSolution();
IVsStatusbar GetStatusBar();
void ShowOptionPage(Type optionsPageType);
Task ExecuteCommandAsync(string command);
}
}
}
9 changes: 4 additions & 5 deletions src/BuildVision/Helpers/ToolWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ public class ToolWindowManager
private readonly IVsWindowFrame _windowFrame;
private readonly Window _window;

public ToolWindowManager(IPackageContext packageContext)
public ToolWindowManager(DTE dte, IVsWindowFrame windowFrame)
{
_dte = packageContext.GetDTE();
if (_dte == null)
throw new InvalidOperationException("Unable to get DTE instance.");

_windowFrame = (IVsWindowFrame)packageContext.GetToolWindow().Frame;
_windowFrame = windowFrame;
if (_windowFrame == null)
throw new InvalidOperationException("Unable to get IVsWindowFrame instance.");

_window = GetWindowInstance(packageContext.GetDTE(), typeof(ToolWindow).GUID);
_window = GetWindowInstance(_dte, typeof(ToolWindow).GUID);
if (_window == null)
throw new InvalidOperationException("Unable to get Window instance.");
}
Expand Down Expand Up @@ -113,4 +112,4 @@ private static Window GetWindowInstance(DTE dte, Guid windowGuid)
return null;
}
}
}
}
Loading

0 comments on commit 3bb9a5d

Please sign in to comment.