-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathLocalServerApiCalls.cs
More file actions
1129 lines (969 loc) · 51.6 KB
/
LocalServerApiCalls.cs
File metadata and controls
1129 lines (969 loc) · 51.6 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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NuGet.Versioning;
using System.Text.RegularExpressions;
using System.Xml;
using System.Net;
using System.Management.Automation;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
internal class LocalServerAPICalls : ServerApiCall
{
#region Members
public override PSRepositoryInfo Repository { get; set; }
private readonly PSCmdlet _cmdletPassedIn;
private readonly FindResponseType _localServerFindResponseType = FindResponseType.ResponseHashtable;
private readonly string _fileTypeKey = "filetype";
#endregion
#region Constructor
public LocalServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn, NetworkCredential networkCredential) : base (repository, networkCredential)
{
this.Repository = repository;
_cmdletPassedIn = cmdletPassedIn;
}
#endregion
#region Overridden Methods
public override Task<FindResults> FindVersionAsync(string packageName, string version, ResourceType type, ConcurrentQueue<ErrorRecord> errorMsgs, ConcurrentQueue<string> warningMsgs, ConcurrentQueue<string> debugMsgs, ConcurrentQueue<string> verboseMsgs)
{
throw new NotImplementedException();
}
public override Task<FindResults> FindVersionGlobbingAsync(string packageName, VersionRange versionRange, bool includePrerelease, ResourceType type, bool getOnlyLatest, ConcurrentQueue<ErrorRecord> errorMsgs, ConcurrentQueue<string> warningMsgs, ConcurrentQueue<string> debugMsgs, ConcurrentQueue<string> verboseMsgs)
{
throw new NotImplementedException();
}
/// <summary>
/// Find method which allows for searching for all packages from a repository and returns latest version for each.
/// Examples: Search -Repository PSGallery
/// API call:
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion
/// </summary>
public override FindResults FindAll(bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindAll()");
return FindTagsHelper(tags: Utils.EmptyStrArray, includePrerelease, out errRecord);
}
/// <summary>
/// Find method which allows for searching for packages with tag from a repository and returns latest version for each.
/// Examples: Search -Tag "JSON" -Repository PSGallery
/// API call:
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm=tag:JSON&includePrerelease=true
/// </summary>
public override FindResults FindTags(string[] tags, bool includePrerelease, ResourceType _type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindTags()");
FindResults tagFindResults = FindTagsHelper(tags, includePrerelease, out errRecord);
if (tagFindResults.IsFindResultsEmpty())
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package(s) with Tags '{String.Join(", ", tags)}' could not be found in repository '{Repository.Name}'."),
"FindTagsPackageNotFound",
ErrorCategory.ObjectNotFound,
this);
}
return tagFindResults;
}
/// <summary>
/// Find method which allows for searching for all packages that have specified Command or DSCResource name.
/// </summary>
public override FindResults FindCommandOrDscResource(string[] tags, bool includePrerelease, bool isSearchingForCommands, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindCommandOrDscResource()");
string[] cmdsOrDSCs = GetCmdsOrDSCTags(tags: tags, isSearchingForCommands: isSearchingForCommands);
FindResults cmdOrDSCFindResults = FindTagsHelper(cmdsOrDSCs, includePrerelease, out errRecord);
if (cmdOrDSCFindResults.IsFindResultsEmpty())
{
string paramName = isSearchingForCommands ? "Command Name(s)" : "DSCResource Name(s)";
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package(s) with {paramName} '{String.Join(", ", tags)}' could not be found in repository '{Repository.Name}'."),
"FindCmdOrDSCNamesPackageNotFound",
ErrorCategory.ObjectNotFound,
this);
}
return cmdOrDSCFindResults;
}
/// <summary>
/// Find method which allows for searching for single name and returns latest version.
/// Name: no wildcard support
/// Examples: Search "PowerShellGet"
/// API call:
/// - No prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
/// - Include prerelease: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease depending on user preference)
/// </summary>
public override FindResults FindName(string packageName, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
////_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindName()");
return FindNameHelper(packageName, Utils.EmptyStrArray, includePrerelease, type, out errRecord);
}
public override Task<FindResults> FindNameAsync(string packageName, bool includePrerelease, ResourceType type, ConcurrentQueue<ErrorRecord> errorMsgs, ConcurrentQueue<string> warningMsgs, ConcurrentQueue<string> debugMsgs, ConcurrentQueue<string> verboseMsgs)
{
throw new NotImplementedException();
}
/// <summary>
/// Find method which allows for searching for single name and tag and returns latest version.
/// Name: no wildcard support
/// Examples: Search "PowerShellGet" -Tag "Provider"
/// Implementation Note: Need to filter further for latest version (prerelease or non-prerelease depending on user preference)
/// </summary>
public override FindResults FindNameWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
////_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindNameWithTag()");
return FindNameHelper(packageName, tags, includePrerelease, type, out errRecord);
}
/// <summary>
/// Find method which allows for searching for single name with wildcards and returns latest version.
/// Name: supports wildcards
/// Examples: Search "PowerShell*"
/// API call:
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='az*'
/// Implementation Note: filter additionally and verify ONLY package name was a match.
/// </summary>
public override FindResults FindNameGlobbing(string packageName, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindNameGlobbing()");
return FindNameGlobbingHelper(packageName, Utils.EmptyStrArray, includePrerelease, type, out errRecord);
}
/// <summary>
/// Find method which allows for searching for single name with wildcards and tag and returns latest version.
/// Name: supports wildcards
/// Examples: Search "PowerShell*" -Tag "Provider"
/// Implementation Note: filter additionally and verify ONLY package name was a match.
/// </summary>
public override FindResults FindNameGlobbingWithTag(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindNameGlobbingWithTag()");
return FindNameGlobbingHelper(packageName, tags, includePrerelease, type, out errRecord);
}
/// <summary>
/// Find method which allows for searching for single name with version range.
/// Name: no wildcard support
/// Version: supports wildcards
/// Examples: Search "PowerShellGet" "[3.0.0.0, 5.0.0.0]"
/// Search "PowerShellGet" "3.*"
/// API Call: http://www.powershellgallery.com/api/v2/FindPackagesById()?id='PowerShellGet'
/// Implementation note: Returns all versions, including prerelease ones. Later (in the API client side) we'll do filtering on the versions to satisfy what user provided.
/// </summary>
public override FindResults FindVersionGlobbing(string packageName, VersionRange versionRange, bool includePrerelease, ResourceType type, bool getOnlyLatest, out ErrorRecord errRecord)
{
////_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindVersionGlobbing()");
FindResults findResponse = new FindResults();
errRecord = null;
string actualPkgName = packageName;
Hashtable pkgVersionsFound = GetMatchingFilesGivenSpecificName(packageName: packageName, includePrerelease: includePrerelease, versionRange: versionRange, actualName: out actualPkgName, errRecord: out errRecord);
List<NuGetVersion> pkgVersionsList = pkgVersionsFound.Keys.Cast<NuGetVersion>().ToList();
pkgVersionsList.Sort();
List<Hashtable> foundPkgs = new List<Hashtable>();
for (int i = pkgVersionsList.Count - 1; i >= 0; i--)
{
// Versions are present in pkgVersionsList in asc order, whereas we need it in desc so we traverse it in reverse.
NuGetVersion satisfyingVersion = pkgVersionsList[i];
string packagePath = (string) pkgVersionsFound[satisfyingVersion];
Hashtable pkgMetadata = GetMetadataFromNupkg(packageName: actualPkgName, packagePath: packagePath, requiredTags: Utils.EmptyStrArray, errRecord: out errRecord);
if (errRecord != null || pkgMetadata.Count == 0)
{
continue;
}
foundPkgs.Add(pkgMetadata);
}
findResponse = new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: foundPkgs.ToArray(), responseType: _localServerFindResponseType);
return findResponse;
}
/// <summary>
/// Find method which allows for searching for single name with specific version.
/// Name: no wildcard support
/// Version: no wildcard support
/// Examples: Search "PowerShellGet" "2.2.5"
/// API call: http://www.powershellgallery.com/api/v2/Packages(Id='PowerShellGet', Version='2.2.5')
/// </summary>
public override FindResults FindVersion(string packageName, string version, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindVersion()");
return FindVersionHelper(packageName, version, Utils.EmptyStrArray, type, out errRecord);
}
/// <summary>
/// Find method which allows for searching for single name with specific version and tag.
/// Name: no wildcard support
/// Version: no wildcard support
/// Examples: Search "PowerShellGet" "2.2.5" -Tag "Provider"
/// </summary>
public override FindResults FindVersionWithTag(string packageName, string version, string[] tags, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindVersionWithTag()");
return FindVersionHelper(packageName, version, tags, type, out errRecord);
}
/** INSTALL APIS **/
/// <summary>
/// Installs a specific package.
/// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined.
/// Therefore, package version should not be null in this method.
/// Name: no wildcard support.
/// Examples: Install "PowerShellGet"
/// Install "PowerShellGet" -Version "3.0.0"
/// </summary>
public override Stream InstallPackage(string packageName, string packageVersion, bool includePrerelease, out ErrorRecord errRecord)
{
Stream results = new MemoryStream();
if (string.IsNullOrEmpty(packageVersion))
{
errRecord = new ErrorRecord(
exception: new ArgumentNullException($"Package version could not be found for {packageName}"),
"PackageVersionNullOrEmptyError",
ErrorCategory.InvalidArgument,
_cmdletPassedIn);
return results;
}
results = InstallVersion(packageName, packageVersion, out errRecord);
return results;
}
/// <summary>
/// Installs a specific package asynchronously.
/// User may request to install package with or without providing version (as seen in examples below), but prior to calling this method the package is located and package version determined.
/// Therefore, package version should not be null in this method.
/// Name: no wildcard support.
/// Examples: Install "PowerShellGet" -Version "3.5.0-alpha"
/// Install "PowerShellGet" -Version "3.0.0"
/// </summary>
public override Task<Stream> InstallPackageAsync(string packageName, string packageVersion, bool includePrerelease, ConcurrentQueue<ErrorRecord> errorMsgs, ConcurrentQueue<string> warningMsgs, ConcurrentQueue<string> debugMsgs, ConcurrentQueue<string> verboseMsgs)
{
throw new NotImplementedException("InstallPackageAsync is not implemented for LocalServerAPICalls.");
}
#endregion
#region Private Methods
/// <summary>
/// Helper method called by FindName() and FindNameWithTag()
/// </summary>
private FindResults FindNameHelper(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindNameHelper()");
FindResults findResponse = new FindResults();
errRecord = null;
NuGetVersion latestVersion = new NuGetVersion("0.0.0.0");
String latestVersionPath = String.Empty;
string actualPkgName = packageName;
// this regex pattern matches packageName followed by a version (4 digit or 3 with prerelease word)
string regexPattern = $"{packageName}" + @"(\.\d+){1,3}(?:[a-zA-Z0-9-.]+|.\d)?\.nupkg";
//_cmdletPassedIn.WriteDebug($"package file name pattern to be searched for is: {regexPattern}");
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
bool isMatch = Regex.IsMatch(packageFullName, regexPattern, RegexOptions.IgnoreCase);
if (!isMatch)
{
continue;
}
NuGetVersion nugetVersion = GetInfoFromFileName(packageFullName: packageFullName, packageName: packageName, actualName: out actualPkgName, out errRecord);
//_cmdletPassedIn.WriteDebug($"Version parsed as '{nugetVersion}'");
if (errRecord != null)
{
return findResponse;
}
if ((!nugetVersion.IsPrerelease || includePrerelease) && (nugetVersion > latestVersion))
{
if (nugetVersion > latestVersion)
{
latestVersion = nugetVersion;
latestVersionPath = path;
}
}
}
if (String.IsNullOrEmpty(latestVersionPath))
{
// means no package was found with this name
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name {packageName} could not be found in repository '{Repository.Name}'."),
"PackageNotFound",
ErrorCategory.ResourceUnavailable,
this);
return findResponse;
}
Hashtable pkgMetadata = GetMetadataFromNupkg(packageName: actualPkgName, packagePath: latestVersionPath, requiredTags: tags, errRecord: out errRecord);
if (errRecord != null)
{
return findResponse;
}
// this condition will be true, for FindNameWithTags() when package satisfying that criteria is not met
if (pkgMetadata.Count == 0)
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name '{packageName}' and tags '{String.Join(", ", tags)}' could not be found in repository '{Repository.Name}'."),
"PackageNotFound",
ErrorCategory.ResourceUnavailable,
this);
}
findResponse = new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: new Hashtable[]{pkgMetadata}, responseType: _localServerFindResponseType);
return findResponse;
}
/// <summary>
/// Helper method called by FindNameGlobbing() and FindNameGlobbingWithTag()
/// </summary>
private FindResults FindNameGlobbingHelper(string packageName, string[] tags, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindNameGlobbingHelper()");
FindResults findResponse = new FindResults();
List<Hashtable> pkgsFound = new List<Hashtable>();
errRecord = null;
Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: packageName, includePrerelease: includePrerelease);
List<string> pkgNamesList = pkgVersionsFound.Keys.Cast<string>().ToList();
foreach(string pkgFound in pkgNamesList)
{
Hashtable pkgInfo = pkgVersionsFound[pkgFound] as Hashtable;
string pkgPath = pkgInfo["path"] as string;
//_cmdletPassedIn.WriteDebug($"Package '{pkgFound}' found from path '{pkgPath}'");
Hashtable pkgMetadata = GetMetadataFromNupkg(packageName: pkgFound, packagePath: pkgPath, requiredTags: tags, errRecord: out errRecord);
if (errRecord != null || pkgMetadata.Count == 0)
{
continue;
}
pkgsFound.Add(pkgMetadata);
}
findResponse = new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: pkgsFound.ToArray(), responseType: _localServerFindResponseType);
return findResponse;
}
/// <summary>
/// Helper method called by FindVersion() and FindVersionWithTag()
/// </summary>
private FindResults FindVersionHelper(string packageName, string version, string[] tags, ResourceType type, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindVersionHelper()");
FindResults findResponse = new FindResults();
errRecord = null;
if (!NuGetVersion.TryParse(version, out NuGetVersion requiredVersion))
{
errRecord = new ErrorRecord(
new InvalidOperationException($"Version {version} could not be parsed into a valid NuGetVersion"),
"FindVersionFailure",
ErrorCategory.InvalidData,
this);
return findResponse;
}
// this regex pattern matches packageName followed by the requested version
string regexPattern = $"{packageName}.{requiredVersion.ToNormalizedString()}" + @".nupkg";
//_cmdletPassedIn.WriteDebug($"pattern is: {regexPattern}");
string pkgPath = String.Empty;
string actualPkgName = String.Empty;
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
bool isMatch = Regex.IsMatch(packageFullName, regexPattern, RegexOptions.IgnoreCase);
if (!isMatch)
{
continue;
}
NuGetVersion nugetVersion = GetInfoFromFileName(packageFullName: packageFullName, packageName: packageName, actualName: out actualPkgName, out errRecord);
//_cmdletPassedIn.WriteDebug($"Version parsed as '{nugetVersion}'");
if (errRecord != null)
{
return findResponse;
}
if (nugetVersion == requiredVersion)
{
//_cmdletPassedIn.WriteDebug("Found matching version");
string pkgFullName = $"{actualPkgName}.{nugetVersion.ToString()}.nupkg";
pkgPath = path;
break;
}
}
if (String.IsNullOrEmpty(pkgPath))
{
// means no package was found with this name, version (and possibly tags).
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name '{packageName}', version '{version}' and tags '{String.Join(", ", tags)}' could not be found in repository '{Repository.Name}'."),
"PackageNotFound",
ErrorCategory.ResourceUnavailable,
this);
return findResponse;
}
Hashtable pkgMetadata = GetMetadataFromNupkg(packageName: actualPkgName, packagePath: pkgPath, requiredTags: tags, errRecord: out errRecord);
if (errRecord != null)
{
return findResponse;
}
// this condition will be true, for FindVersionWithTags() when package satisfying that criteria is not met
if (pkgMetadata.Count == 0)
{
errRecord = new ErrorRecord(
new ResourceNotFoundException($"Package with name '{packageName}', and tags '{String.Join(", ", tags)}' could not be found in repository '{Repository.Name}'."),
"PackageNotFound",
ErrorCategory.InvalidResult,
this);
}
findResponse = new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: new Hashtable[]{pkgMetadata}, responseType: _localServerFindResponseType);
return findResponse;
}
/// <summary>
/// Helper method called by FindTags(), FindAll(), and FindCommandOrDSCResource()
/// </summary>
private FindResults FindTagsHelper(string[] tags, bool includePrerelease, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::FindTagsHelper()");
FindResults findResponse = new FindResults();
List<Hashtable> pkgsFound = new List<Hashtable>();
errRecord = null;
Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: String.Empty, includePrerelease: includePrerelease);
List<string> pkgNamesList = pkgVersionsFound.Keys.Cast<string>().ToList();
foreach(string pkgFound in pkgNamesList)
{
Hashtable pkgInfo = pkgVersionsFound[pkgFound] as Hashtable;
NuGetVersion pkgVersion = pkgInfo["version"] as NuGetVersion;
string pkgPath = pkgInfo["path"] as string;
//_cmdletPassedIn.WriteDebug($"Found package '{pkgFound}' from path '{pkgPath}'");
Hashtable pkgMetadata = GetMetadataFromNupkg(packageName: pkgFound, packagePath: pkgPath, requiredTags: tags, errRecord: out errRecord);
if (errRecord != null)
{
return findResponse;
}
// This condition is hit if the package is not a match with respect to tags, in which case we should skip the package and not return from this method.
if (pkgMetadata.Count == 0)
{
continue;
}
pkgsFound.Add(pkgMetadata);
}
findResponse = new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: pkgsFound.ToArray(), responseType: _localServerFindResponseType);
return findResponse;
}
/// <summary>
/// Installs specific package.
/// Name: no wildcard support.
/// Examples: Install "PowerShellGet"
/// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable)
/// if prerelease, call into InstallVersion instead.
/// </summary>
private Stream InstallName(string packageName, bool includePrerelease, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallName()");
FileStream fs = null;
errRecord = null;
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.*", WildcardOptions.IgnoreCase);
NuGetVersion latestVersion = new NuGetVersion("0.0.0.0");
String latestVersionPath = String.Empty;
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
{
//_cmdletPassedIn.WriteDebug($"'{packageName}' found in '{path}'");
string[] packageWithoutName = packageFullName.ToLower().Split(new string[] { $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
string packageVersionAndExtension = packageWithoutName[0];
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
string version = packageVersionAndExtension.Substring(0, extensionDot);
//_cmdletPassedIn.WriteDebug($"Parsing version '{version}' of package '{packageName}'");
NuGetVersion.TryParse(version, out NuGetVersion nugetVersion);
if ((!nugetVersion.IsPrerelease || includePrerelease) && (nugetVersion > latestVersion))
{
latestVersion = nugetVersion;
latestVersionPath = path;
}
}
}
if (String.IsNullOrEmpty(latestVersionPath))
{
errRecord = new ErrorRecord(
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
"InstallNameFailure",
ErrorCategory.ResourceUnavailable,
this);
return fs;
}
try
{
//_cmdletPassedIn.WriteDebug($"Reading file '{latestVersionPath}'");
fs = new FileStream(latestVersionPath, FileMode.Open, FileAccess.Read);
if (fs == null)
{
errRecord = new ErrorRecord(
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
"InstallNameFailure",
ErrorCategory.ResourceUnavailable,
this);
}
}
catch (Exception e)
{
errRecord = new ErrorRecord(
exception: e,
"InstallNameFailure",
ErrorCategory.ReadError,
this);
}
return fs;
}
/// <summary>
/// Installs package with specific name and version.
/// Name: no wildcard support.
/// Version: no wildcard support.
/// Examples: Install "PowerShellGet" -Version "3.0.0.0"
/// Install "PowerShellGet" -Version "3.0.0-beta16"
/// API Call: https://www.powershellgallery.com/api/v2/package/Id/version (version can be prerelease)
/// </summary>
private Stream InstallVersion(string packageName, string version, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::InstallVersion()");
errRecord = null;
FileStream fs = null;
// if 4 digits and last is 0, create 3 digit equiv string
// 4 digit version (where last is 0) is always passed in.
NuGetVersion.TryParse(version, out NuGetVersion pkgVersion);
//_cmdletPassedIn.WriteDebug($"Version parsed as '{pkgVersion}'");
if (pkgVersion.Revision == 0)
{
version = pkgVersion.ToNormalizedString();
}
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.{version}.nupkg*", WildcardOptions.IgnoreCase);
String pkgVersionPath = String.Empty;
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
{
//_cmdletPassedIn.WriteDebug($"Found match with '{path}'");
pkgVersionPath = path;
}
}
if (String.IsNullOrEmpty(pkgVersionPath))
{
errRecord = new ErrorRecord(
new LocalResourceEmpty($"'{packageName}' is not present in repository"),
"InstallNameFailure",
ErrorCategory.ResourceUnavailable,
this);
return fs;
}
try
{
//_cmdletPassedIn.WriteDebug($"Reading file '{pkgVersionPath}'");
fs = new FileStream(pkgVersionPath, FileMode.Open, FileAccess.Read);
if (fs == null)
{
errRecord = new ErrorRecord(
new LocalResourceEmpty("The contents of the package file for specified resource was empty or invalid"),
"InstallNameFailure",
ErrorCategory.ResourceUnavailable,
this);
}
}
catch (Exception e)
{
errRecord = new ErrorRecord(
exception: e,
"InstallVersionFailure",
ErrorCategory.ReadError,
this);
}
return fs;
}
/// <summary>
/// Extract metadata from .nupkg package file.
/// This is called only for packages that are ascertained to be a match for our search criteria.
/// </summary>
private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, string[] requiredTags, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::GetMetadataFromNupkg()");
Hashtable pkgMetadata = new Hashtable(StringComparer.OrdinalIgnoreCase);
errRecord = null;
// create temp directory where we will copy .nupkg to, extract contents, etc.
var tempDiscoveryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
string packageFullName = Path.GetFileName(packagePath);
try
{
var dir = Directory.CreateDirectory(tempDiscoveryPath);
dir.Attributes &= ~FileAttributes.ReadOnly;
// copy .nupkg
string destNupkgPath = Path.Combine(tempDiscoveryPath, packageFullName);
File.Copy(packagePath, destNupkgPath);
// change extension to .zip
string zipFilePath = Path.ChangeExtension(destNupkgPath, ".zip");
File.Move(destNupkgPath, zipFilePath);
// extract from .zip
//_cmdletPassedIn.WriteDebug($"Extracting '{zipFilePath}' to '{tempDiscoveryPath}'");
System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, tempDiscoveryPath);
string psd1FilePath = String.Empty;
string ps1FilePath = String.Empty;
string nuspecFilePath = String.Empty;
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath, out string properCasingPkgName);
List<string> pkgTags = new List<string>();
if (File.Exists(psd1FilePath))
{
//_cmdletPassedIn.WriteDebug($"Attempting to read module manifest file '{psd1FilePath}'");
if (!Utils.TryReadManifestFile(psd1FilePath, out pkgMetadata, out Exception readManifestError))
{
errRecord = new ErrorRecord(
readManifestError,
"GetMetadataFromNupkgFailure",
ErrorCategory.ParserError,
this);
return pkgMetadata;
}
GetPrivateDataFromHashtable(pkgMetadata, out string prereleaseLabel, out Uri licenseUri, out Uri projectUri, out Uri iconUri, out string releaseNotes, out string[] pkgHashTags);
pkgMetadata.Add("Tags", pkgHashTags);
pkgMetadata.Add("Prerelease", prereleaseLabel);
pkgMetadata.Add("LicenseUri", licenseUri);
pkgMetadata.Add("ProjectUri", projectUri);
pkgMetadata.Add("IconUri", iconUri);
pkgMetadata.Add("ReleaseNotes", releaseNotes);
pkgMetadata.Add("Id", properCasingPkgName);
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.ModuleManifest);
pkgTags.AddRange(pkgHashTags);
}
else if (File.Exists(ps1FilePath))
{
//_cmdletPassedIn.WriteDebug($"Attempting to read script file '{ps1FilePath}'");
if (!PSScriptFileInfo.TryTestPSScriptFileInfo(ps1FilePath, out PSScriptFileInfo parsedScript, out ErrorRecord[] errors, out string[] verboseMsgs))
{
errRecord = new ErrorRecord(
new InvalidDataException($"PSScriptFile could not be read properly"),
"GetMetadataFromNupkgFailure",
ErrorCategory.ParserError,
this);
return pkgMetadata;
}
pkgMetadata = parsedScript.ToHashtable();
pkgMetadata.Add("Id", properCasingPkgName);
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.ScriptFile);
pkgTags.AddRange(pkgMetadata["Tags"] as string[]);
}
else if (File.Exists(nuspecFilePath))
{
//_cmdletPassedIn.WriteDebug($"Attempting to read nuspec file '{nuspecFilePath}'");
pkgMetadata = GetHashtableForNuspec(nuspecFilePath, out errRecord);
if (errRecord != null)
{
return pkgMetadata;
}
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.Nuspec);
string nuspecTags = pkgMetadata["tags"] as string;
string[] nuspecTagsArray = nuspecTags.Split(new char[]{' '});
pkgTags.AddRange(nuspecTagsArray);
}
else
{
errRecord = new ErrorRecord(
new InvalidDataException($".nupkg package must contain either .psd1, .ps1, or .nuspec file and none were found"),
"GetMetadataFromNupkgFailure",
ErrorCategory.InvalidData,
this);
return pkgMetadata;
}
// if no RequiredTags are specified for the API, this will return true by default.
bool isTagMatch = DeterminePkgTagsSatisfyRequiredTags(pkgTags: pkgTags.ToArray(), requiredTags: requiredTags);
if (!isTagMatch)
{
return new Hashtable();
}
}
catch (Exception e)
{
errRecord = new ErrorRecord(
new InvalidOperationException($"Temporary folder for installation could not be created or set due to: {e.Message}"),
"GetMetadataFromNupkgFailure",
ErrorCategory.InvalidOperation,
this);
}
finally
{
if (Directory.Exists(tempDiscoveryPath))
{
Utils.DeleteDirectory(tempDiscoveryPath);
}
}
return pkgMetadata;
}
/// <summary>
/// Looks through .nupkg files present in the repository and returns
/// hashtable with those that matches the exact name and prerelease requirements provided.
/// This helper method is called for FindVersionGlobbing()
/// </summary>
private Hashtable GetMatchingFilesGivenSpecificName(string packageName, bool includePrerelease, VersionRange versionRange, out string actualName, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::GetMatchingFilesGivenSpecificName()");
actualName = packageName;
// used for FindVersionGlobbing where we know exact non-wildcard name of the package
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.*", WildcardOptions.IgnoreCase);
Hashtable pkgVersionsFound = new Hashtable(StringComparer.OrdinalIgnoreCase);
errRecord = null;
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
if (!String.IsNullOrEmpty(packageFullName) && pkgNamePattern.IsMatch(packageFullName))
{
NuGetVersion nugetVersion = GetInfoFromFileName(packageFullName: packageFullName, packageName: packageName, out actualName, errRecord: out errRecord);
//_cmdletPassedIn.WriteDebug($"Found package '{packageName}' from path '{path}'");
if (errRecord != null)
{
continue;
}
if ((!nugetVersion.IsPrerelease || includePrerelease) && (versionRange.Satisfies(nugetVersion)))
{
if (!pkgVersionsFound.ContainsKey(nugetVersion))
{
pkgVersionsFound.Add(nugetVersion, path);
}
}
}
}
return pkgVersionsFound;
}
/// <summary>
/// Looks through .nupkg files present in the repository and returns
/// hashtable with those that match the name wildcard pattern and prerelease requirements provided.
/// This helper method is called for FindAll(), FindTags(), FindNameGlobbing() scenarios.
/// </summary>
private Hashtable GetMatchingFilesGivenNamePattern(string packageNameWithWildcard, bool includePrerelease)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::GetMatchingFilesGivenNamePattern()");
bool isNameFilteringRequired = !String.IsNullOrEmpty(packageNameWithWildcard);
// wildcard name possibilities: power*, *get, power*get
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageNameWithWildcard}", WildcardOptions.IgnoreCase);
Regex rx = new Regex(@"\.\d+\.", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Hashtable pkgVersionsFound = new Hashtable(StringComparer.OrdinalIgnoreCase);
foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
{
string packageFullName = Path.GetFileName(path);
MatchCollection matches = rx.Matches(packageFullName);
if (matches.Count == 0)
{
continue;
}
Match match = matches[0];
GroupCollection groups = match.Groups;
if (groups.Count == 0)
{
continue;
}
Capture group = groups[0];
string pkgFoundName = packageFullName.Substring(0, group.Index);
if (isNameFilteringRequired)
{
if (!pkgNamePattern.IsMatch(pkgFoundName))
{
continue;
}
}
string version = packageFullName.Substring(group.Index + 1, packageFullName.LastIndexOf('.') - group.Index - 1);
//_cmdletPassedIn.WriteDebug($"Found package '{pkgFoundName}', version '{version}', from path '{path}'");
if (!NuGetVersion.TryParse(version, out NuGetVersion nugetVersion))
{
continue;
}
if (!nugetVersion.IsPrerelease || includePrerelease)
{
if (!pkgVersionsFound.ContainsKey(pkgFoundName))
{
Hashtable pkgInfo = new Hashtable(StringComparer.OrdinalIgnoreCase);
pkgInfo.Add("version", nugetVersion);
pkgInfo.Add("path", path);
pkgVersionsFound.Add(pkgFoundName, pkgInfo);
}
else
{
Hashtable pkgInfo = pkgVersionsFound[pkgFoundName] as Hashtable;
NuGetVersion existingVersion = pkgInfo["version"] as NuGetVersion;
if (nugetVersion > existingVersion)
{
pkgInfo["version"] = nugetVersion;
pkgInfo["path"] = path;
pkgVersionsFound[pkgFoundName] = pkgInfo;
}
}
}
}
return pkgVersionsFound;
}
/// <summary>
/// Takes .nupkg package file name (i.e like mypackage.1.0.0.nupkg) and parses out version from it.
/// </summary>
private NuGetVersion GetInfoFromFileName(string packageFullName, string packageName, out string actualName, out ErrorRecord errRecord)
{
//_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::GetInfoFromFileName()");
// packageFullName will look like package.1.0.0.nupkg
errRecord = null;
string[] packageWithoutName = packageFullName.ToLower().Split(new string[]{ $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
string packageVersionAndExtension = packageWithoutName[0];
string[] originalFileNameParts = packageFullName.ToLower().Split(new string[]{ $".{packageVersionAndExtension.ToLower()}" }, StringSplitOptions.RemoveEmptyEntries);
actualName = String.IsNullOrEmpty(originalFileNameParts[0]) ? packageName : originalFileNameParts[0];
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
string version = packageVersionAndExtension.Substring(0, extensionDot);
if (!NuGetVersion.TryParse(version, out NuGetVersion nugetVersion))
{
errRecord = new ErrorRecord(
new ArgumentException($"Could not parse version {version} from file {packageFullName}"),
"GetInfoFromFileNameFailure",
ErrorCategory.ParserError,
this);
return null;
}
return nugetVersion;
}
/// <summary>
/// Method that loads file content into XMLDocument. Used when reading .nuspec file.
/// </summary>
private XmlDocument LoadXmlDocument(string filePath, out ErrorRecord errRecord)
{
errRecord = null;
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
try { doc.Load(filePath); }
catch (Exception e)
{
errRecord = new ErrorRecord(
exception: e,
"LoadXmlDocumentFailure",
ErrorCategory.ReadError,
this);
}
return doc;
}
/// <summary>
/// Helper method that compares the tags requests to be present to the tags present in the package.
/// </summary>
private bool DeterminePkgTagsSatisfyRequiredTags(string[] pkgTags, string[] requiredTags)
{
bool isTagMatch = true;
foreach (string tag in requiredTags)
{
if (!pkgTags.Contains(tag, StringComparer.OrdinalIgnoreCase))
{
isTagMatch = false;
break;
}
}
return isTagMatch;
}
/// <summary>
/// Method that reads .nuspec file and parses out metadata information into Hashtable.
/// </summary>
private Hashtable GetHashtableForNuspec(string filePath, out ErrorRecord errRecord)
{
Hashtable nuspecHashtable = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
XmlDocument nuspecXmlDocument = LoadXmlDocument(filePath, out errRecord);
if (errRecord != null)
{
return nuspecHashtable;
}
try
{
XmlNodeList elemList = nuspecXmlDocument.GetElementsByTagName("metadata");
for(int i = 0; i < elemList.Count; i++)