From c71b94fdad00007e51183f28bbdcc01552a22290 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Tue, 1 Nov 2016 12:21:23 -0700 Subject: [PATCH] Move the 'Install Package' operation to a common location. --- ...eReference.InstallNugetPackageOperation.cs | 74 ------------------- ...ce.InstallPackageAndAddImportCodeAction.cs | 21 +++--- .../PackageReference.ParentCodeAction.cs | 6 +- .../InstallPackageCodeActionOperation.cs | 68 +++++++++++++++++ src/Features/Core/Portable/Features.csproj | 2 +- 5 files changed, 85 insertions(+), 86 deletions(-) delete mode 100644 src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallNugetPackageOperation.cs create mode 100644 src/Features/Core/Portable/AddPackage/InstallPackageCodeActionOperation.cs diff --git a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallNugetPackageOperation.cs b/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallNugetPackageOperation.cs deleted file mode 100644 index dc3b46b357f..00000000000 --- a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallNugetPackageOperation.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.Packaging; -using Microsoft.CodeAnalysis.Shared.Utilities; - -namespace Microsoft.CodeAnalysis.CodeFixes.AddImport -{ - internal abstract partial class AbstractAddImportCodeFixProvider - { - private partial class PackageReference - { - /// - /// Operation responsible purely for installing a nuget package with a specific - /// version, or a the latest version of a nuget package. Is not responsible - /// for adding an import to user code. - /// - private class InstallNugetPackageOperation : CodeActionOperation - { - private readonly Document _document; - private readonly IPackageInstallerService _installerService; - private readonly string _source; - private readonly string _packageName; - private readonly string _versionOpt; - private readonly bool _isLocal; - private readonly List _projectsWithMatchingVersion; - - public InstallNugetPackageOperation( - IPackageInstallerService installerService, - Document document, - string source, - string packageName, - string versionOpt, - bool isLocal) - { - _installerService = installerService; - _document = document; - _source = source; - _packageName = packageName; - _versionOpt = versionOpt; - _isLocal = isLocal; - if (versionOpt != null) - { - const int projectsToShow = 5; - var otherProjects = installerService.GetProjectsWithInstalledPackage( - _document.Project.Solution, packageName, versionOpt).ToList(); - _projectsWithMatchingVersion = otherProjects.Take(projectsToShow).Select(p => p.Name).ToList(); - if (otherProjects.Count > projectsToShow) - { - _projectsWithMatchingVersion.Add("..."); - } - } - } - - public override string Title => _versionOpt == null - ? string.Format(FeaturesResources.Find_and_install_latest_version_of_0, _packageName) - : _isLocal - ? string.Format(FeaturesResources.Use_locally_installed_0_version_1_This_version_used_in_colon_2, _packageName, _versionOpt, string.Join(", ", _projectsWithMatchingVersion)) - : string.Format(FeaturesResources.Install_0_1, _packageName, _versionOpt); - - internal override bool ApplyDuringTests => true; - - internal override bool TryApply(Workspace workspace, IProgressTracker progressTracker, CancellationToken cancellationToken) - { - return _installerService.TryInstallPackage( - workspace, _document.Id, _source, _packageName, _versionOpt, cancellationToken); - } - } - } - } -} \ No newline at end of file diff --git a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallPackageAndAddImportCodeAction.cs b/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallPackageAndAddImportCodeAction.cs index 76144a1bfda..837180445f6 100644 --- a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallPackageAndAddImportCodeAction.cs +++ b/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.InstallPackageAndAddImportCodeAction.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.AddPackage; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.Shared.Utilities; using Microsoft.CodeAnalysis.Text; @@ -31,9 +32,11 @@ private struct InstallPackageAndAddImportData /// /// The operation that will actually install the nuget package. /// - public readonly InstallNugetPackageOperation InstallOperation; + public readonly InstallPackageCodeActionOperation InstallOperation; - public InstallPackageAndAddImportData(Document oldDocument, Document newDocument, InstallNugetPackageOperation installOperation) + public InstallPackageAndAddImportData( + Document oldDocument, Document newDocument, + InstallPackageCodeActionOperation installOperation) { OldDocument = oldDocument; NewDocument = newDocument; @@ -105,22 +108,22 @@ private class InstallPackageAndAddImportOperation : CodeActionOperation private readonly DocumentId _changedDocumentId; private readonly SourceText _oldText; private readonly SourceText _newText; - private readonly InstallNugetPackageOperation _installNugetPackage; + private readonly InstallPackageCodeActionOperation _installPackageOperation; public InstallPackageAndAddImportOperation( DocumentId changedDocumentId, SourceText oldText, - SourceText newText, - InstallNugetPackageOperation item2) + SourceText newText, + InstallPackageCodeActionOperation item2) { _changedDocumentId = changedDocumentId; _oldText = oldText; _newText = newText; - _installNugetPackage = item2; + _installPackageOperation = item2; } - internal override bool ApplyDuringTests => _installNugetPackage.ApplyDuringTests; - public override string Title => _installNugetPackage.Title; + internal override bool ApplyDuringTests => _installPackageOperation.ApplyDuringTests; + public override string Title => _installPackageOperation.Title; internal override bool TryApply(Workspace workspace, IProgressTracker progressTracker, CancellationToken cancellationToken) { @@ -130,7 +133,7 @@ internal override bool TryApply(Workspace workspace, IProgressTracker progressTr // First make the changes to add the import to the document. if (workspace.TryApplyChanges(newSolution, progressTracker)) { - if (_installNugetPackage.TryApply(workspace, progressTracker, cancellationToken)) + if (_installPackageOperation.TryApply(workspace, progressTracker, cancellationToken)) { return true; } diff --git a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.ParentCodeAction.cs b/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.ParentCodeAction.cs index 99e55d36d42..ac64308d5f6 100644 --- a/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.ParentCodeAction.cs +++ b/src/Features/Core/Portable/AddImport/CodeActions/PackageReference.ParentCodeAction.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.AddPackage; using Microsoft.CodeAnalysis.CodeActions; using Roslyn.Utilities; @@ -118,8 +119,9 @@ private class ParentCodeAction : CodeAction.CodeActionWithNestedActions newDocument = await CleanupDocumentAsync( newDocument, cancellationToken).ConfigureAwait(false); - var installOperation = new InstallNugetPackageOperation( - reference._installerService, document, reference._source, reference._packageName, versionOpt, isLocal); + var installOperation = new InstallPackageCodeActionOperation( + reference._installerService, document, reference._source, + reference._packageName, versionOpt, isLocal); return new InstallPackageAndAddImportData( oldDocument, newDocument, installOperation); diff --git a/src/Features/Core/Portable/AddPackage/InstallPackageCodeActionOperation.cs b/src/Features/Core/Portable/AddPackage/InstallPackageCodeActionOperation.cs new file mode 100644 index 00000000000..5018368bfc5 --- /dev/null +++ b/src/Features/Core/Portable/AddPackage/InstallPackageCodeActionOperation.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.Packaging; +using Microsoft.CodeAnalysis.Shared.Utilities; + +namespace Microsoft.CodeAnalysis.AddPackage +{ + /// + /// Operation responsible purely for installing a nuget package with a specific + /// version, or a the latest version of a nuget package. Is not responsible + /// for adding an import to user code. + /// + internal class InstallPackageCodeActionOperation : CodeActionOperation + { + private readonly Document _document; + private readonly IPackageInstallerService _installerService; + private readonly string _source; + private readonly string _packageName; + private readonly string _versionOpt; + private readonly bool _isLocal; + private readonly List _projectsWithMatchingVersion; + + public InstallPackageCodeActionOperation( + IPackageInstallerService installerService, + Document document, + string source, + string packageName, + string versionOpt, + bool isLocal) + { + _installerService = installerService; + _document = document; + _source = source; + _packageName = packageName; + _versionOpt = versionOpt; + _isLocal = isLocal; + if (versionOpt != null) + { + const int projectsToShow = 5; + var otherProjects = installerService.GetProjectsWithInstalledPackage( + _document.Project.Solution, packageName, versionOpt).ToList(); + _projectsWithMatchingVersion = otherProjects.Take(projectsToShow).Select(p => p.Name).ToList(); + if (otherProjects.Count > projectsToShow) + { + _projectsWithMatchingVersion.Add("..."); + } + } + } + + public override string Title => _versionOpt == null + ? string.Format(FeaturesResources.Find_and_install_latest_version_of_0, _packageName) + : _isLocal + ? string.Format(FeaturesResources.Use_locally_installed_0_version_1_This_version_used_in_colon_2, _packageName, _versionOpt, string.Join(", ", _projectsWithMatchingVersion)) + : string.Format(FeaturesResources.Install_0_1, _packageName, _versionOpt); + + internal override bool ApplyDuringTests => true; + + internal override bool TryApply(Workspace workspace, IProgressTracker progressTracker, CancellationToken cancellationToken) + { + return _installerService.TryInstallPackage( + workspace, _document.Id, _source, _packageName, _versionOpt, cancellationToken); + } + } +} \ No newline at end of file diff --git a/src/Features/Core/Portable/Features.csproj b/src/Features/Core/Portable/Features.csproj index 28dd45dac97..61414f18954 100644 --- a/src/Features/Core/Portable/Features.csproj +++ b/src/Features/Core/Portable/Features.csproj @@ -91,9 +91,9 @@ Shared\Utilities\DesktopShim.cs + - True -- GitLab