-
Notifications
You must be signed in to change notification settings - Fork 744
Expand file tree
/
Copy pathAddBindingRedirectCommand.cs
More file actions
95 lines (81 loc) · 3.66 KB
/
AddBindingRedirectCommand.cs
File metadata and controls
95 lines (81 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using NuGet.PackageManagement.VisualStudio;
using NuGet.VisualStudio;
namespace NuGet.PackageManagement.PowerShellCmdlets
{
[Cmdlet(VerbsCommon.Add, "BindingRedirect")]
[OutputType(typeof(AssemblyBinding))]
public class AddBindingRedirectCommand : NuGetPowerShellBaseCommand
{
private readonly AsyncLazy<IVsFrameworkMultiTargeting> _frameworkMultiTargeting;
public AddBindingRedirectCommand()
{
_frameworkMultiTargeting = new AsyncLazy<IVsFrameworkMultiTargeting>(
() => ServiceLocator.GetGlobalServiceAsync<SVsFrameworkMultiTargeting, IVsFrameworkMultiTargeting>(),
NuGetUIThreadHelper.JoinableTaskFactory);
}
[Parameter(Position = 0, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "PowerShell API requirement")]
public string[] ProjectName { get; set; }
/// <summary>
/// logging time disabled for tab command
/// </summary>
protected override bool IsLoggingTimeDisabled
{
get
{
return true;
}
}
protected override void ProcessRecordCore()
{
NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
{
CheckSolutionState();
var projects = new List<IVsProjectAdapter>();
// if no project specified, use default
if (ProjectName == null)
{
var defaultProject = await GetDefaultProjectAsync();
// if no default project (empty solution), throw terminating
if (defaultProject == null)
{
ErrorHandler.ThrowNoCompatibleProjectsTerminatingError();
}
projects.Add(defaultProject);
}
else
{
// get matching projects, expanding wildcards
projects.AddRange(await GetProjectsByNameAsync(ProjectName));
}
// Create a new app domain so we don't load the assemblies into the host app domain
var domain = AppDomain.CreateDomain("domain");
try
{
var frameworkMultiTargeting = await _frameworkMultiTargeting.GetValueAsync();
foreach (var project in projects)
{
var projectAssembliesCache = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
var redirects = await RuntimeHelpers.AddBindingRedirectsAsync(VsSolutionManager, project, domain, projectAssembliesCache, frameworkMultiTargeting, this);
// Print out what we did
WriteObject(redirects, enumerateCollection: true);
}
}
finally
{
AppDomain.Unload(domain);
}
});
}
}
}