提交 c71b94fd 编写于 作者: C CyrusNajmabadi

Move the 'Install Package' operation to a common location.

上级 cc586042
// 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<TSimpleNameSyntax>
{
private partial class PackageReference
{
/// <summary>
/// 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.
/// </summary>
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<string> _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
......@@ -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
/// <summary>
/// The operation that will actually install the nuget package.
/// </summary>
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;
}
......
......@@ -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);
......
// 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
{
/// <summary>
/// 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.
/// </summary>
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<string> _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
......@@ -91,9 +91,9 @@
<Compile Include="..\..\..\Compilers\Shared\DesktopShim.cs">
<Link>Shared\Utilities\DesktopShim.cs</Link>
</Compile>
<Compile Include="AddPackage\InstallPackageCodeActionOperation.cs" />
<Compile Include="CodeStyle\AbstractCodeStyleDiagnosticAnalyzer.cs" />
<Compile Include="AddImport\CodeActions\PackageReference.InstallPackageAndAddImportCodeAction.cs" />
<Compile Include="AddImport\CodeActions\PackageReference.InstallNugetPackageOperation.cs" />
<Compile Include="AddImport\CodeActions\PackageReference.InstallWithPackageManagerCodeAction.cs" />
<Compile Include="FeaturesResources.Designer.cs">
<AutoGen>True</AutoGen>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册