Skip to content

Commit

Permalink
Improved: upgraded to VS Async API.
Browse files Browse the repository at this point in the history
  • Loading branch information
yagasoft committed Oct 4, 2021
1 parent e20e6c5 commit 6594217
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 125 deletions.
83 changes: 56 additions & 27 deletions CrmCodeGenerator.VSPackage/CrmCodeGenerator.VSPackagePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
Expand All @@ -17,6 +18,7 @@
using Microsoft.VisualStudio.Shell.Interop;
using WebResourceLinker;
using WebResourceLinkerExt.VSPackage.Helpers;
using Task = System.Threading.Tasks.Task;

#endregion

Expand All @@ -33,18 +35,17 @@ namespace WebResourceLinkerExt.VSPackage
/// </summary>
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
// a package.
[PackageRegistration(UseManagedResourcesOnly = true)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
// This attribute is used to register the information needed to show this package
// in the Help/About dialog of Visual Studio.
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
//this causes the class to load when VS starts [ProvideAutoLoad("ADFC4E64-0397-11D1-9F4E-00A0C911004F")]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
//[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
//[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
// This attribute is needed to let the shell know that this package exposes some menus.
[ProvideMenuResource("Menus.ctmenu", 1)]
[Guid(GuidList.guidWebResLinkExt_VSPackagePkgString)]
public sealed class WebResourceLinkerExt_VSPackagePackage : Package,
IVsSolutionEvents3
public sealed class WebResourceLinkerExt_VSPackagePackage : AsyncPackage, IVsSolutionEvents3
{
/// <summary>
/// Default constructor of the package.
Expand All @@ -68,22 +69,22 @@ public WebResourceLinkerExt_VSPackagePackage()
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Sdk", new Version("9.0.0.0"), "31bf3856ad364e35");
AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Sdk.Deployment", new Version("9.0.0.0"), "31bf3856ad364e35");
AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Tooling.Connector", new Version("4.0.0.0"), "31bf3856ad364e35");
AssemblyHelpers.RedirectAssembly("Microsoft.IdentityModel.Clients.ActiveDirectory",
new Version("3.19.8.16603"), "31bf3856ad364e35");
AssemblyHelpers.RedirectAssembly("Newtonsoft.Json", new Version("10.0.0.0"), "30ad4fe6b2a6aeed");
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);

//AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Sdk", new Version("9.0.0.0"), "31bf3856ad364e35");
//AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Sdk.Deployment", new Version("9.0.0.0"), "31bf3856ad364e35");
//AssemblyHelpers.RedirectAssembly("Microsoft.Xrm.Tooling.Connector", new Version("4.0.0.0"), "31bf3856ad364e35");
//AssemblyHelpers.RedirectAssembly("Microsoft.IdentityModel.Clients.ActiveDirectory",
// new Version("3.19.8.16603"), "31bf3856ad364e35");
//AssemblyHelpers.RedirectAssembly("Newtonsoft.Json", new Version("10.0.0.0"), "30ad4fe6b2a6aeed");

Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", ToString()));
base.Initialize();
await base.InitializeAsync(cancellationToken, progress);

// Add our command handlers for menu (commands must exist in the .vsct file)
var mcs = GetService(typeof (IMenuCommandService)) as OleMenuCommandService;

if (null != mcs)
if (await GetServiceAsync(typeof (IMenuCommandService)) is OleMenuCommandService mcs)
{
//var codeCmd = new CommandID(GuidList.guidWebResLinkExt_VSPackageCmdSet,
// (int) PkgCmdIDList.cmdidCode);
Expand All @@ -92,16 +93,38 @@ protected override void Initialize()

var fileCmd = new CommandID(GuidList.guidWebResLinkExt_VSPackageCmdSet,
(int) PkgCmdIDList.cmdidFile);
var fileItem = new MenuCommand(CodeCallback, fileCmd);
var fileItem = new MenuCommand(
async (o, e) =>
{
try
{
await CodeCallbackAsync(o, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}, fileCmd);
mcs.AddCommand(fileItem);

var relinkCmd = new CommandID(GuidList.guidWebResLinkExt_VSPackageCmdSet,
(int) PkgCmdIDList.cmdidFileRelink);
var relinkItem = new MenuCommand(RelinkCallback, relinkCmd);
var relinkItem = new MenuCommand(
async (o, e) =>
{
try
{
await RelinkCallbackAsync(o, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}, relinkCmd);
mcs.AddCommand(relinkItem);
}

AdviseSolutionEvents();
await AdviseSolutionEventsAsync();
}

protected override void Dispose(bool disposing)
Expand All @@ -114,11 +137,13 @@ protected override void Dispose(bool disposing)
private IVsSolution solution = null;
private uint _handleCookie;

private void AdviseSolutionEvents()
private async Task AdviseSolutionEventsAsync()
{
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);

UnadviseSolutionEvents();

solution = GetService(typeof (SVsSolution)) as IVsSolution;
solution = await GetServiceAsync(typeof (SVsSolution)) as IVsSolution;

if (solution != null)
{
Expand All @@ -128,6 +153,8 @@ private void AdviseSolutionEvents()

private void UnadviseSolutionEvents()
{
ThreadHelper.ThrowIfNotOnUIThread();

if (solution != null)
{
if (_handleCookie != uint.MaxValue)
Expand All @@ -147,25 +174,27 @@ private void UnadviseSolutionEvents()
/// See the Initialize method to see how the menu item is associated to this function using
/// the OleMenuCommandService service and the MenuCommand class.
/// </summary>
private void CodeCallback(object sender, EventArgs args)
private async Task CodeCallbackAsync(object sender, EventArgs args)
{
HandleCodeWindowCommand(false);
await HandleCodeWindowCommandAsync(false);
}

private void RelinkCallback(object sender, EventArgs args)
private async Task RelinkCallbackAsync(object sender, EventArgs args)
{
HandleCodeWindowCommand(true);
await HandleCodeWindowCommandAsync(true);
}

private void HandleCodeWindowCommand(bool relinking)
private async Task HandleCodeWindowCommandAsync(bool relinking)
{
await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);

try
{
Status.Update(">>>>> Starting new session <<<<<");

try
{
var _applicationObject = GetService(typeof (SDTE)) as DTE2;
var _applicationObject = await GetServiceAsync(typeof (SDTE)) as DTE2;

const string linkerDataPath = "WebResLink.dat"; // this is our mapping file, it gets stored in the project
var solutionPath = Path.GetDirectoryName(_applicationObject.Solution.FullName);
Expand Down
Binary file not shown.
Binary file added CrmCodeGenerator.VSPackage/Newtonsoft.Json.dll
Binary file not shown.
35 changes: 31 additions & 4 deletions CrmCodeGenerator.VSPackage/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebResourceLinkerExt.VSPackage")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Ahmed el-Sawalhy (Yagasoft.com)")]
[assembly: AssemblyCompany("Ahmed Elsawalhy (yagasoft.com)")]
[assembly: AssemblyProduct("WebResourceLinkerExt.VSPackage")]
[assembly: AssemblyCopyright("2015 Yagasoft")]
[assembly: AssemblyCopyright("Yagasoft 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
Expand All @@ -29,8 +30,34 @@
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.2.1.3")]
[assembly: AssemblyFileVersion("1.2.1.3")]
[assembly: AssemblyVersion("2.6.1.0")]
[assembly: AssemblyFileVersion("2.6.1.0")]


[assembly: ProvideBindingRedirection(AssemblyName = "Microsoft.Xrm.Sdk",
CodeBase = @"$PackageFolder$\Microsoft.Xrm.Sdk.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "9.0.0.0")]

[assembly: ProvideBindingRedirection(AssemblyName = "Microsoft.Xrm.Sdk.Deployment",
CodeBase = @"$PackageFolder$\Microsoft.Xrm.Sdk.Deployment.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "9.0.0.0")]

[assembly: ProvideBindingRedirection(AssemblyName = "Microsoft.Crm.Sdk.Proxy",
CodeBase = @"$PackageFolder$\Microsoft.Crm.Sdk.Proxy.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "9.0.0.0")]

[assembly: ProvideBindingRedirection(AssemblyName = "Microsoft.Xrm.Tooling.Connector",
CodeBase = @"$PackageFolder$\Microsoft.Xrm.Tooling.Connector.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "4.0.0.0")]

[assembly: ProvideBindingRedirection(AssemblyName = "Microsoft.IdentityModel.Clients.ActiveDirectory",
CodeBase = @"$PackageFolder$\Microsoft.IdentityModel.Clients.ActiveDirectory.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "3.19.8.16603")]

[assembly: ProvideBindingRedirection(AssemblyName = "Newtonsoft.Json",
CodeBase = @"$PackageFolder$\Newtonsoft.Json.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "10.0.0.0")]

[assembly: ProvideBindingRedirection(AssemblyName = "Yagasoft.Libraries.Common",
CodeBase = @"$PackageFolder$\Yagasoft.Libraries.Common.dll",
OldVersionLowerBound = "0.0.0.0", OldVersionUpperBound = "3.0.0.0")]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6594217

Please sign in to comment.