This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChocolateyProvider.cs
522 lines (460 loc) · 25.7 KB
/
ChocolateyProvider.cs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//
// 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.ChocolateyPrototype {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common;
using NuGet;
using Sdk;
using Constants = Sdk.Constants;
/// <summary>
/// Chocolatey Package provider for PackageManagement.
/// Important notes:
/// - Required Methods: Not all methods are required; some package providers do not support some features. If the
/// methods isn't used or implemented it should be removed (or commented out)
/// - Error Handling: Avoid throwing exceptions from these methods. To properly return errors to the user, use the
/// request.Error(...) method to notify the user of an error conditionm and then return.
/// - Communicating with the HOST and CORE: each method takes a IRequest
/// - use the <code><![CDATA[ .As<Request>() ]]></code> extension method to strongly-type it to the Request type (which
/// calls upon the duck-typer to generate a strongly-typed wrapper). The strongly-typed wrapper also implements
/// several helper functions to make using the request object easier.
/// </summary>
public class ChocolateyProvider {
internal const string ProviderName = "Chocolatey";
internal static readonly Dictionary<string, string[]> Features = new Dictionary<string, string[]> {
{Constants.Features.SupportedSchemes, new[] {"http", "https", "file"}},
{Constants.Features.SupportedExtensions, new[] {"nupkg"}},
{Constants.Features.MagicSignatures, new[] {Constants.Signatures.Zip}}
};
/// <summary>
/// The name of this Package Provider
/// </summary>
public string PackageProviderName {
get {
return ProviderName;
}
}
// public string ProviderVersion { get { return "1.2.3.4"; } }
// --- Finds packages ---------------------------------------------------------------------------------------------------
internal bool IsValidVersionRange(string minimumVersion, string maximumVersion) {
if (!(String.IsNullOrEmpty(minimumVersion) || String.IsNullOrEmpty(maximumVersion))) {
if (new SemanticVersion(minimumVersion) > new SemanticVersion(maximumVersion)) {
return false;
}
}
return true;
}
/// <summary>
/// Performs one-time initialization of the PROVIDER.
/// </summary>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void InitializeProvider(ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::InitializeProvider'", PackageProviderName);
}
/// <summary>
/// Returns a collection of strings to the client advertizing features this provider supports.
/// </summary>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void GetFeatures(ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::GetFeatures' ", PackageProviderName);
request.Yield(Features);
}
public void GetDynamicOptions(string category, ChocolateyRequest request) {
request.Debug("Calling '{0}::GetDynamicOptions' '{1}'", PackageProviderName, category);
switch ((category ?? string.Empty).ToLowerInvariant()) {
case "package":
request.YieldDynamicOption("FilterOnTag", Constants.OptionType.StringArray, false);
request.YieldDynamicOption("Contains", Constants.OptionType.String, false);
request.YieldDynamicOption("AllowPrereleaseVersions", Constants.OptionType.Switch, false);
break;
case "source":
request.YieldDynamicOption("ConfigFile", Constants.OptionType.String, false);
request.YieldDynamicOption("SkipValidate", Constants.OptionType.Switch, false);
break;
case "install":
request.YieldDynamicOption("SkipDependencies", Constants.OptionType.Switch, false);
request.YieldDynamicOption("ContinueOnFailure", Constants.OptionType.Switch, false);
request.YieldDynamicOption("ExcludeVersion", Constants.OptionType.Switch, false);
request.YieldDynamicOption("ForceX86", Constants.OptionType.Switch, false);
request.YieldDynamicOption("PackageSaveMode", Constants.OptionType.String, false, new[] {
"nuspec", "nupkg", "nuspec;nupkg"
});
break;
}
}
/// <summary>
/// This is called when the user is adding (or updating) a package source
/// If this PROVIDER doesn't support user-defined package sources, remove this method.
/// </summary>
/// <param name="name">
/// The name of the package source. If this parameter is null or empty the PROVIDER should use the
/// location as the name (if the PROVIDER actually stores names of package sources)
/// </param>
/// <param name="location">
/// The location (ie, directory, URL, etc) of the package source. If this is null or empty, the
/// PROVIDER should use the name as the location (if valid)
/// </param>
/// <param name="trusted">
/// A boolean indicating that the user trusts this package source. Packages returned from this source
/// should be marked as 'trusted'
/// </param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void AddPackageSource(string name, string location, bool trusted, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::AddPackageSource' '{1}','{2}','{3}'", PackageProviderName, name, location,
trusted);
// if they didn't pass in a name, use the location as a name. (if you support that kind of thing)
name = string.IsNullOrEmpty(name) ? location : name;
// let's make sure that they've given us everything we need.
if (string.IsNullOrEmpty(name)) {
request.Error(ErrorCategory.InvalidArgument, Constants.Parameters.Name,
Constants.Messages.MissingRequiredParameter, Constants.Parameters.Name);
// we're done here.
return;
}
if (string.IsNullOrEmpty(location)) {
request.Error(ErrorCategory.InvalidArgument, Constants.Parameters.Location,
Constants.Messages.MissingRequiredParameter, Constants.Parameters.Location);
// we're done here.
return;
}
// if this is supposed to be an update, there will be a dynamic parameter set for IsUpdatePackageSource
var isUpdate = request.GetOptionValue(Constants.Parameters.IsUpdate).IsTrue();
// if your source supports credentials you get get them too:
// string username =request.Username;
// SecureString password = request.Password;
// feel free to send back an error here if your provider requires credentials for package sources.
// check first that we're not clobbering an existing source, unless this is an update
var src = request.FindRegisteredSource(name);
if (src != null && !isUpdate) {
// tell the user that there's one here already
request.Error(ErrorCategory.InvalidArgument, name ?? location, Constants.Messages.PackageSourceExists,
name ?? location);
// we're done here.
return;
}
// conversely, if it didn't find one, and it is an update, that's bad too:
if (src == null && isUpdate) {
// you can't find that package source? Tell that to the user
request.Error(ErrorCategory.ObjectNotFound, name ?? location, Constants.Messages.UnableToResolveSource,
name ?? location);
// we're done here.
return;
}
// ok, we know that we're ok to save this source
// next we check if the location is valid (if we support that kind of thing)
var validated = false;
if (!request.SkipValidate) {
// the user has not opted to skip validating the package source location, so check that it's valid (talk to the url, or check if it's a valid directory, etc)
// todo: insert code to check if the source is valid
validated = request.ValidateSourceLocation(location);
if (!validated) {
request.Error(ErrorCategory.InvalidData, name ?? location, Constants.Messages.SourceLocationNotValid,
location);
// we're done here.
return;
}
// we passed validation!
}
// it's good to check just before you actaully write something to see if the user has cancelled the operation
if (request.IsCanceled) {
return;
}
// looking good -- store the package source
// todo: create the package source (and store it whereever you store it)
request.Verbose("Storing package source {0}", name);
// actually yielded by the implementation.
request.AddPackageSource(name, location, trusted, validated);
// and, before you go, Yield the package source back to the caller.
if (!request.YieldPackageSource(name, location, trusted, true /*since we just registered it*/, validated)) {
// always check the return value of a yield, since if it returns false, you don't keep returning data
// this can happen if they have cancelled the operation.
}
// all done!
}
/// <summary>
/// </summary>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void ResolvePackageSources(ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::ResolvePackageSources'", PackageProviderName);
foreach (var source in request.SelectedSources) {
request.YieldPackageSource(source.Name, source.Location, source.Trusted, source.IsRegistered,
source.IsValidated);
}
}
/// <summary>
/// Removes/Unregisters a package source
/// </summary>
/// <param name="name">The name or location of a package source to remove.</param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void RemovePackageSource(string name, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::RemovePackageSource' '{1}'", PackageProviderName, name);
var src = request.FindRegisteredSource(name);
if (src == null) {
request.Warning(Constants.Messages.UnableToResolveSource, name);
return;
}
request.RemovePackageSource(src.Name);
request.YieldPackageSource(src.Name, src.Location, src.Trusted, false, src.IsValidated);
}
/// <summary>
/// </summary>
/// <param name="name"></param>
/// <param name="requiredVersion"></param>
/// <param name="minimumVersion"></param>
/// <param name="maximumVersion"></param>
/// <param name="id"></param>
/// <param name="request"></param>
/// <returns></returns>
public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion,
int id, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::FindPackage' '{1}','{2}','{3}','{4}','{5}'", PackageProviderName, name,
requiredVersion, minimumVersion, maximumVersion, id);
requiredVersion = requiredVersion.FixVersion();
if (!string.IsNullOrEmpty(requiredVersion)) {
if (request.FindByCanonicalId && requiredVersion.IndexOfAny("()[],".ToCharArray()) == -1) {
// when resolving packages via a canonical id, treat a lone version (ie 1.0 -> 1.0 <= x) string as a nuget version range:
minimumVersion = requiredVersion;
maximumVersion = null;
requiredVersion = null;
} else {
minimumVersion = null;
maximumVersion = null;
}
} else {
minimumVersion = minimumVersion.FixVersion();
maximumVersion = maximumVersion.FixVersion();
}
if (!IsValidVersionRange(minimumVersion, maximumVersion)) {
request.Error(ErrorCategory.InvalidArgument, minimumVersion + maximumVersion,
Constants.Messages.InvalidVersionRange, minimumVersion, maximumVersion);
return;
}
// get the package by ID first.
// if there are any packages, yield and return
if (!string.IsNullOrWhiteSpace(name)) {
if (
request.YieldPackages(
request.GetPackageById(name, requiredVersion, minimumVersion, maximumVersion), name) ||
request.FoundPackageById) {
// if we actaully found some by that id, but didn't make it past the filter, we're done.
return;
}
}
// have we been cancelled?
if (request.IsCanceled) {
return;
}
// Try searching for matches and returning those.
request.YieldPackages(request.SearchForPackages(name, requiredVersion, minimumVersion, maximumVersion), name);
request.Debug("Finished '{0}::FindPackage' '{1}','{2}','{3}','{4}','{5}'", PackageProviderName, name,
requiredVersion, minimumVersion, maximumVersion, id);
}
/// <summary>
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void InstallPackage(string fastPackageReference, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::InstallPackage' '{1}'", PackageProviderName, fastPackageReference);
var pkgRef = request.GetPackageByFastpath(fastPackageReference);
if (pkgRef == null) {
request.Error(ErrorCategory.InvalidArgument, fastPackageReference,
Constants.Messages.UnableToResolvePackage);
return;
}
// need to make sure that the original package's sources list is tried first.
request.OriginalSources = pkgRef.Sources;
var dependencies = request.GetUninstalledPackageDependencies(pkgRef).Reverse().ToArray();
var progressId = 0;
if (dependencies.Length > 0) {
progressId = request.StartProgress(0, "Installing package '{0}'", pkgRef.GetCanonicalId(request));
}
var n = 0;
foreach (var d in dependencies) {
request.Progress(progressId, (n*100/(dependencies.Length + 1)) + 1, "Installing dependent package '{0}'",
d.GetCanonicalId(request));
if (!request.InstallSinglePackage(d)) {
request.Error(ErrorCategory.InvalidResult, pkgRef.GetCanonicalId(request),
Constants.Messages.DependentPackageFailedInstall, d.GetCanonicalId(request));
return;
}
n++;
request.Progress(progressId, (n*100/(dependencies.Length + 1)), "Installed dependent package '{0}'",
d.GetCanonicalId(request));
}
// got this far, let's install the package we came here for.
if (!request.InstallSinglePackage(pkgRef)) {
// package itself didn't install.
// roll that back out everything we did install.
// and get out of here.
request.Error(ErrorCategory.InvalidResult, pkgRef.GetCanonicalId(request),
Constants.Messages.PackageFailedInstall, pkgRef.GetCanonicalId(request));
request.CompleteProgress(progressId, false);
}
request.CompleteProgress(progressId, true);
}
/// <summary>
/// Uninstalls a package
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
public void UninstallPackage(string fastPackageReference, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::UninstallPackage' '{1}'", PackageProviderName, fastPackageReference);
var pkg = request.GetPackageByFastpath(fastPackageReference);
request.UninstallPackage(pkg);
}
/// <summary>
/// Downloads a remote package file to a local location.
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="location"></param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with the
/// CORE and HOST
/// </param>
/// <returns></returns>
public void DownloadPackage(string fastPackageReference, string location, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::DownloadPackage' '{1}','{2}'", PackageProviderName, fastPackageReference,
location);
var pkgRef = request.GetPackageByFastpath(fastPackageReference);
if (pkgRef == null) {
request.Error(ErrorCategory.InvalidArgument, fastPackageReference,
Constants.Messages.UnableToResolvePackage);
return;
}
// cheap and easy copy to location.
using (var input = pkgRef.Package.GetStream()) {
using (var output = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.Read)) {
input.CopyTo(output);
}
}
}
/// <summary>
/// Finds a package given a local filename
/// </summary>
/// <param name="file"></param>
/// <param name="id"></param>
/// <param name="request"></param>
public void FindPackageByFile(string file, int id, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::FindPackageByFile' '{1}','{2}'", PackageProviderName, file, id);
var pkgItem = request.GetPackageByFilePath(Path.GetFullPath(file));
if (pkgItem != null) {
request.YieldPackage(pkgItem, file);
}
}
/// <summary>
/// Returns the packages that are installed
/// </summary>
/// <param name="name">the package name to match. Empty or null means match everything</param>
/// <param name="requiredVersion">
/// the specific version asked for. If this parameter is specified (ie, not null or empty
/// string) then the minimum and maximum values are ignored
/// </param>
/// <param name="minimumVersion">
/// the minimum version of packages to return . If the <code>requiredVersion</code> parameter
/// is specified (ie, not null or empty string) this should be ignored
/// </param>
/// <param name="maximumVersion">
/// the maximum version of packages to return . If the <code>requiredVersion</code> parameter
/// is specified (ie, not null or empty string) this should be ignored
/// </param>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with
/// the CORE and HOST
/// </param>
public void GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, ChocolateyRequest request) {
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::GetInstalledPackages' '{1}','{2}','{3}','{4}'", PackageProviderName, name,
requiredVersion, minimumVersion, maximumVersion);
if (requiredVersion != null) {
minimumVersion = null;
maximumVersion = null;
} else {
minimumVersion = minimumVersion.FixVersion();
maximumVersion = maximumVersion.FixVersion();
}
if (!IsValidVersionRange(minimumVersion, maximumVersion)) {
request.Error(ErrorCategory.InvalidArgument, minimumVersion + maximumVersion,
Constants.Messages.InvalidVersionRange, minimumVersion, maximumVersion);
return;
}
// look in the destination directory for directories that contain nupkg files.
var subdirs = Directory.EnumerateDirectories(request.Destination);
foreach (var subdir in subdirs) {
var nupkgs = Directory.EnumerateFileSystemEntries(subdir, "*.nupkg", SearchOption.TopDirectoryOnly);
foreach (var pkgFile in nupkgs) {
var pkgItem = request.GetPackageByFilePath(pkgFile);
if (pkgItem != null && pkgItem.IsInstalled) {
if (pkgItem.Id.Equals(name, StringComparison.CurrentCultureIgnoreCase)) {
if (!string.IsNullOrWhiteSpace(requiredVersion)) {
if (pkgItem.Package.Version != new SemanticVersion(requiredVersion)) {
continue;
}
} else {
if (!string.IsNullOrWhiteSpace(minimumVersion) &&
pkgItem.Package.Version < new SemanticVersion(minimumVersion)) {
continue;
}
if (!string.IsNullOrWhiteSpace(maximumVersion) &&
pkgItem.Package.Version < new SemanticVersion(maximumVersion)) {
continue;
}
}
request.YieldPackage(pkgItem, name);
break;
}
if (string.IsNullOrEmpty(name) ||
pkgItem.Id.IndexOf(name, StringComparison.CurrentCultureIgnoreCase) > -1) {
if (!request.YieldPackage(pkgItem, name)) {
return;
}
}
}
}
}
}
}
}