Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch away from deprecated NuGet API #716

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<PackageVersion Include="Moq" Version="4.10.1" />
<PackageVersion Include="NerdBank.GitVersioning" Version="3.3.37" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nuget.VisualStudio" Version="6.0.0" />
<PackageVersion Include="Nuget.VisualStudio" Version="17.7.0" />
<PackageVersion Include="NuGet.VisualStudio.Contracts" Version="17.7.0" />

<PackageVersion Include="System.Collections.Immutable" Version="7.0.0" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
Expand Down
19 changes: 11 additions & 8 deletions src/LibraryManager.Vsix/Commands/RestoreOnBuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
Expand All @@ -13,18 +15,18 @@
using Microsoft.Web.LibraryManager.Vsix.Contracts;
using Microsoft.Web.LibraryManager.Vsix.Shared;
using NuGet.VisualStudio;
using Task = System.Threading.Tasks.Task;
using NuGet.VisualStudio.Contracts;

namespace Microsoft.Web.LibraryManager.Vsix.Commands
{
internal sealed class RestoreOnBuildCommand
{
private bool _isPackageInstalled;
private readonly IComponentModel _componentModel;
private readonly AsyncPackage _package;
private readonly LibraryManagerPackage _package;
private readonly IDependenciesFactory _dependenciesFactory;

private RestoreOnBuildCommand(AsyncPackage package, OleMenuCommandService commandService, IDependenciesFactory dependenciesFactory)
private RestoreOnBuildCommand(LibraryManagerPackage package, OleMenuCommandService commandService, IDependenciesFactory dependenciesFactory)
{
_package = package;
_componentModel = VsHelpers.GetService<SComponentModel, IComponentModel>();
Expand All @@ -44,7 +46,7 @@ private IServiceProvider ServiceProvider
get { return _package; }
}

public static void Initialize(AsyncPackage package, OleMenuCommandService commandService, IDependenciesFactory dependenciesFactory)
public static void Initialize(LibraryManagerPackage package, OleMenuCommandService commandService, IDependenciesFactory dependenciesFactory)
{
Instance = new RestoreOnBuildCommand(package, commandService, dependenciesFactory);
}
Expand All @@ -66,7 +68,7 @@ private async Task BeforeQueryStatusAsync(object sender, EventArgs e)
button.Visible = true;
button.Enabled = KnownUIContexts.SolutionExistsAndNotBuildingAndNotDebuggingContext.IsActive;

_isPackageInstalled = IsPackageInstalled(item.ContainingProject);
_isPackageInstalled = await IsPackageInstalledAsync(item.ContainingProject);

if (_isPackageInstalled)
{
Expand Down Expand Up @@ -165,11 +167,12 @@ private bool UserWantsToInstall()
return answer == 6; // 6 = Yes
}

private bool IsPackageInstalled(Project project)
private async Task<bool> IsPackageInstalledAsync(Project project)
{
IVsPackageInstallerServices installerServices = _componentModel.GetService<IVsPackageInstallerServices>();
Guid projectGuid = VsHelpers.GetProjectGuid(project);
InstalledPackagesResult installedPackages = await _package.NugetProjectService.GetInstalledPackagesAsync(projectGuid, CancellationToken.None);

return installerServices.IsPackageInstalled(project, Constants.MainNuGetPackageId);
return installedPackages.Packages.Any(p => p.Id == Constants.MainNuGetPackageId);
}
}
}
22 changes: 21 additions & 1 deletion src/LibraryManager.Vsix/LibraryManagerPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.ServiceBroker;
using Microsoft.Web.LibraryManager.Vsix.Commands;
using Microsoft.Web.LibraryManager.Vsix.Contracts;
using Microsoft.Web.LibraryManager.Vsix.Shared;
using NuGet.VisualStudio.Contracts;
using Tasks = System.Threading.Tasks;

namespace Microsoft.Web.LibraryManager.Vsix
Expand All @@ -32,7 +35,7 @@ namespace Microsoft.Web.LibraryManager.Vsix
"ActiveProjectFlavor:" + Constants.WebsiteProject,
"ActiveProjectCapability:" + Constants.DotNetCoreWebCapability,
"HierSingleSelectionName:" + Constants.ConfigFileName + "$" })]
[ProvideUIContextRule(PackageGuids.guidUiContextString,
[ProvideUIContextRule(PackageGuids.guidUiContextString,
name: Vsix.Name,
expression: "(WAP | WebSite | DotNetCoreWeb )",
termNames: new string[] {
Expand All @@ -53,6 +56,9 @@ internal sealed class LibraryManagerPackage : AsyncPackage
[Import]
internal IDependenciesFactory DependenciesFactory { get; private set; }

[SuppressMessage("Reliability", "ISB001:Dispose of proxies", Justification = "It is in Dispose()")]
public INuGetProjectService NugetProjectService { get; private set; }

protected override async Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Expand All @@ -61,6 +67,13 @@ protected override async Tasks.Task InitializeAsync(CancellationToken cancellati
Assumes.Present(componentModel);
componentModel.DefaultCompositionService.SatisfyImportsOnce(this);

IAsyncServiceProvider asyncServiceProvider = this;
IBrokeredServiceContainer brokeredServiceContainer = await asyncServiceProvider.GetServiceAsync<SVsBrokeredServiceContainer, IBrokeredServiceContainer>();
ServiceHub.Framework.IServiceBroker serviceBroker = brokeredServiceContainer.GetFullAccessServiceBroker();
#pragma warning disable ISB001 // Dispose of proxies
NugetProjectService = await serviceBroker.GetProxyAsync<INuGetProjectService>(NuGetServices.NuGetProjectServiceV1);
#pragma warning restore ISB001 // Dispose of proxies

var commandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

if (commandService != null && LibraryCommandService != null)
Expand All @@ -73,5 +86,12 @@ protected override async Tasks.Task InitializeAsync(CancellationToken cancellati
ManageLibrariesCommand.Initialize(this, commandService, LibraryCommandService, DependenciesFactory);
}
}

protected override void Dispose(bool disposing)
{
(NugetProjectService as IDisposable)?.Dispose();

base.Dispose(disposing);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@
<PackageReference Include="Microsoft.WebTools.Languages.Json.Editor" />
<PackageReference Include="Microsoft.WebTools.Languages.Json.VS" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Nuget.VisualStudio" />
<PackageReference Include="NuGet.VisualStudio" />
<PackageReference Include="NuGet.VisualStudio.Contracts" />
</ItemGroup>
<ItemGroup>
<VSCTCompile Include="Commands\CommandTable\VSCommandTable.en.vsct">
Expand Down
12 changes: 12 additions & 0 deletions src/LibraryManager.Vsix/Shared/VsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,17 @@ private static void DeleteEmptyFolders(HashSet<ProjectItem> folders)
}
}
}

public static Guid GetProjectGuid(Project project)
{
string uniqueName = project.UniqueName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do some sort of return result checking in any of these? I'd assume there are HRs returned and object could be checked for null.

IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
solution.GetProjectOfUniqueName(uniqueName, out IVsHierarchy hierarchy);
hierarchy.GetGuidProperty(
(uint)VSConstants.VSITEMID.Root,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out Guid projectGuid);
return projectGuid;
}
}
}
Loading