forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellArtifactInstaller.cs
More file actions
200 lines (165 loc) · 9.87 KB
/
Copy pathPowerShellArtifactInstaller.cs
File metadata and controls
200 lines (165 loc) · 9.87 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PackageManagement.PackageSourceListProvider
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using Microsoft.PackageManagement.Internal.Api;
using Microsoft.PackageManagement.Packaging;
using Microsoft.PackageManagement.Provider.Utility;
using Microsoft.PackageManagement.Internal.Utility.Plugin;
internal static class PowerShellArtifactInstaller
{
internal static void InstallPowershellArtifacts(PackageJson package, string fastPath, PackageSourceListRequest request)
{
var provider1 = PackageSourceListRequest.FindProvider(request, package.Type, request, true);
if (provider1 == null) return;
// As the PowerShellGet may access the -source via $request.Options or PackageSources,
// so we need to fill in both options and sources parameters
var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary<string, string[]>
{
{"Source", new[] {package.Source ?? ""}}
}, new[] { package.Source ?? "" }, package.IsTrustedSource, request);
var psid = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.PowerShellGet);
var pkgs = request.PackageManagementService.FindPackageByCanonicalId(psid, installRequest)
.Where(each => new SemanticVersion(each.Version) == new SemanticVersion(package.Version)).ToArray();
switch (pkgs.Length)
{
case 0:
request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, psid);
break;
case 1:
InstallPackageViaPowerShellGet(package, request, pkgs);
break;
default:
request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, psid);
break;
}
return;
}
private static void InstallPackageViaPowerShellGet(PackageJson packageJson, PackageSourceListRequest request, SoftwareIdentity[] packages)
{
var provider = PackageSourceListRequest.FindProvider(request, packageJson.Type, request, true);
if (provider == null) return;
IHostApi installRequest = request;
if (provider.Name.EqualsIgnoreCase(Constants.ProviderNames.PowerShellGet) && !request.ProviderServices.IsElevated) {
// if we're not elevated, we want powershellget to install to the user scope
installRequest = PackageSourceListRequest.ExtendRequest(
new Dictionary<string, string[]> {
{"Scope", new[] {"CurrentUser"}}
}, null, packageJson.IsTrustedSource, request);
} else {
installRequest = PackageSourceListRequest.ExtendRequest(
new Dictionary<string, string[]> {
{"Destination", new[] {packageJson.Destination ?? ""}}
}, null, packageJson.IsTrustedSource, request);
}
request.Debug("Calling '{0}' provider to install the package '{1}.{2}'", provider.Name, packageJson.Name, packageJson.Version);
var installing = provider.InstallPackage(packages[0], installRequest);
if (installing == null || !installing.Any())
{
request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "InstallPackage");
request.Warning(Resources.Messages.FailToInstallPackage, Constants.ProviderName, packages[0].Name);
return;
}
int packagesRecevied = 0;
foreach (var i in installing)
{
request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
if (request.IsCanceled)
{
installing.Cancel();
}
else
{
request.Verbose(Resources.Messages.SuccessfullyInstalled, "{0}.{1}".format(packageJson.Name, packageJson.Version));
//load provider
if (packageJson.IsPackageProvider)
{
//Per provider development guidance: provider name and module name should be the same otherwise we can not import it.
request.PackageManagementService.ImportPackageProvider(request, packageJson.Name, null, null, null, isRooted: false, force: false);
}
}
packagesRecevied++;
}
request.Verbose(Resources.Messages.NumberOfPackagesRecevied, packagesRecevied, provider.Name, "install-package");
}
internal static void GeInstalledPowershellArtifacts(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary<string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GeInstalledPowershellArtifacts' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion));
var provider = PackageSourceListRequest.FindProvider(request, package.Type, request);
if (provider == null) return;
//calling the PowerShellGet provider
request.Debug("Calling '{0}' provider to get installed packages '{1}.{2}'", provider.Name, package.Name, package.Version);
var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, request).ToArray();
if (packagesInstalled == null || !packagesInstalled.Any())
{
request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "GetInstalledPackages");
return;
}
foreach (var i in packagesInstalled)
{
request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source);
var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, "");
fastPackReftable.AddOrSet(info, i);
// check if the installed version matches with the one specified in the PSL.json.
// If so, we choose PSL.json.
var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version;
request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
}
}
/// <summary>
/// Uninstalls a package
/// </summary>
/// <param name="package">package defined in the PackageSourceList</param>
/// <param name="fastPackageReference"></param>
/// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
/// <param name="fastPackReftable"></param>
internal static void UninstallPowershellArtifacts(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary<string, SoftwareIdentity> fastPackReftable)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "UninstallNuGetPackage' - fastReference='{0}'", fastPackageReference));
var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true);
if (provider != null)
{
request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name);
if (!fastPackReftable.ContainsKey(fastPackageReference))
{
request.WriteError(Internal.ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference);
return;
}
var p = fastPackReftable[fastPackageReference];
//calling NuGet for uninstall
var installing = provider.UninstallPackage(p, request);
foreach (var i in installing)
{
request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename);
if (request.IsCanceled)
{
installing.Cancel();
}
}
}
}
}
}