@@ -34,6 +34,7 @@ internal class FindHelper
34
34
private bool _includeDependencies = false ;
35
35
private bool _repositoryNameContainsWildcard = true ;
36
36
private NetworkCredential _networkCredential ;
37
+ private Dictionary < string , List < string > > _packagesFound ;
37
38
38
39
#endregion
39
40
@@ -46,6 +47,7 @@ public FindHelper(CancellationToken cancellationToken, PSCmdlet cmdletPassedIn,
46
47
_cancellationToken = cancellationToken ;
47
48
_cmdletPassedIn = cmdletPassedIn ;
48
49
_networkCredential = networkCredential ;
50
+ _packagesFound = new Dictionary < string , List < string > > ( StringComparer . OrdinalIgnoreCase ) ;
49
51
}
50
52
51
53
#endregion
@@ -404,7 +406,7 @@ public IEnumerable<PSCommandResourceInfo> FindByCommandOrDscResource(
404
406
{
405
407
_cmdletPassedIn . WriteVerbose ( errRecord . Exception . Message ) ;
406
408
}
407
-
409
+
408
410
continue ;
409
411
}
410
412
@@ -632,7 +634,6 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
632
634
{
633
635
ErrorRecord errRecord = null ;
634
636
List < PSResourceInfo > parentPkgs = new List < PSResourceInfo > ( ) ;
635
- HashSet < string > pkgsFound = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
636
637
string tagsAsString = String . Empty ;
637
638
638
639
_cmdletPassedIn . WriteDebug ( "In FindHelper::SearchByNames()" ) ;
@@ -677,8 +678,9 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
677
678
if ( foundPkg . Type == _type || _type == ResourceType . None )
678
679
{
679
680
parentPkgs . Add ( foundPkg ) ;
680
- pkgsFound . Add ( String . Format ( "{0}{1}" , foundPkg . Name , foundPkg . Version . ToString ( ) ) ) ;
681
+ TryAddToPackagesFound ( foundPkg ) ;
681
682
_cmdletPassedIn . WriteDebug ( $ "Found package '{ foundPkg . Name } ' version '{ foundPkg . Version } '") ;
683
+
682
684
yield return foundPkg ;
683
685
}
684
686
}
@@ -729,7 +731,8 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
729
731
730
732
PSResourceInfo foundPkg = currentResult . returnedObject ;
731
733
parentPkgs . Add ( foundPkg ) ;
732
- pkgsFound . Add ( String . Format ( "{0}{1}" , foundPkg . Name , foundPkg . Version . ToString ( ) ) ) ;
734
+ TryAddToPackagesFound ( foundPkg ) ;
735
+
733
736
yield return foundPkg ;
734
737
}
735
738
}
@@ -778,7 +781,8 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
778
781
779
782
PSResourceInfo foundPkg = currentResult . returnedObject ;
780
783
parentPkgs . Add ( foundPkg ) ;
781
- pkgsFound . Add ( String . Format ( "{0}{1}" , foundPkg . Name , foundPkg . Version . ToString ( ) ) ) ;
784
+ TryAddToPackagesFound ( foundPkg ) ;
785
+
782
786
yield return foundPkg ;
783
787
}
784
788
}
@@ -833,13 +837,14 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
833
837
"FindVersionConvertToPSResourceFailure" ,
834
838
ErrorCategory . ObjectNotFound ,
835
839
this ) ) ;
836
-
840
+
837
841
continue ;
838
842
}
839
843
840
844
PSResourceInfo foundPkg = currentResult . returnedObject ;
841
845
parentPkgs . Add ( foundPkg ) ;
842
- pkgsFound . Add ( String . Format ( "{0}{1}" , foundPkg . Name , foundPkg . Version . ToString ( ) ) ) ;
846
+ TryAddToPackagesFound ( foundPkg ) ;
847
+
843
848
yield return foundPkg ;
844
849
}
845
850
}
@@ -915,7 +920,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
915
920
&& _versionRange . Satisfies ( version ) )
916
921
{
917
922
parentPkgs . Add ( foundPkg ) ;
918
- pkgsFound . Add ( String . Format ( "{0}{1}" , foundPkg . Name , foundPkg . Version . ToString ( ) ) ) ;
923
+ TryAddToPackagesFound ( foundPkg ) ;
919
924
920
925
yield return foundPkg ;
921
926
}
@@ -936,7 +941,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
936
941
foreach ( PSResourceInfo currentPkg in parentPkgs )
937
942
{
938
943
_cmdletPassedIn . WriteDebug ( $ "Finding dependency packages for '{ currentPkg . Name } '") ;
939
- foreach ( PSResourceInfo pkgDep in FindDependencyPackages ( currentServer , currentResponseUtil , currentPkg , repository , pkgsFound ) )
944
+ foreach ( PSResourceInfo pkgDep in FindDependencyPackages ( currentServer , currentResponseUtil , currentPkg , repository ) )
940
945
{
941
946
yield return pkgDep ;
942
947
}
@@ -962,6 +967,48 @@ private HashSet<string> GetPackageNamesPopulated(string[] pkgNames)
962
967
return pkgsToDiscover ;
963
968
}
964
969
970
+
971
+ private bool TryAddToPackagesFound ( PSResourceInfo foundPkg )
972
+ {
973
+ bool addedToHash = false ;
974
+ string foundPkgName = foundPkg . Name ;
975
+ string foundPkgVersion = Utils . GetNormalizedVersionString ( foundPkg . Version . ToString ( ) , foundPkg . Prerelease ) ;
976
+
977
+ if ( _packagesFound . ContainsKey ( foundPkgName ) )
978
+ {
979
+ List < string > pkgVersions = _packagesFound [ foundPkgName ] as List < string > ;
980
+
981
+ if ( ! pkgVersions . Contains ( foundPkgVersion ) )
982
+ {
983
+ pkgVersions . Add ( foundPkgVersion ) ;
984
+ _packagesFound [ foundPkgName ] = pkgVersions ;
985
+ addedToHash = true ;
986
+ }
987
+ }
988
+ else
989
+ {
990
+ _packagesFound . Add ( foundPkg . Name , new List < string > { foundPkgVersion } ) ;
991
+ addedToHash = true ;
992
+ }
993
+
994
+ _cmdletPassedIn . WriteDebug ( $ "Found package '{ foundPkg . Name } ' version '{ foundPkg . Version } '") ;
995
+
996
+ return addedToHash ;
997
+ }
998
+
999
+ private string FormatPkgVersionString ( PSResourceInfo pkg )
1000
+ {
1001
+ string fullPkgVersion = pkg . Version . ToString ( ) ;
1002
+
1003
+ if ( ! string . IsNullOrWhiteSpace ( pkg . Prerelease ) )
1004
+ {
1005
+ fullPkgVersion += $ "-{ pkg . Prerelease } ";
1006
+ }
1007
+ _cmdletPassedIn . WriteDebug ( $ "Formatted full package version is: '{ fullPkgVersion } '") ;
1008
+
1009
+ return fullPkgVersion ;
1010
+ }
1011
+
965
1012
#endregion
966
1013
967
1014
#region Internal Client Search Methods
@@ -970,8 +1017,7 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
970
1017
ServerApiCall currentServer ,
971
1018
ResponseUtil currentResponseUtil ,
972
1019
PSResourceInfo currentPkg ,
973
- PSRepositoryInfo repository ,
974
- HashSet < string > foundPkgs )
1020
+ PSRepositoryInfo repository )
975
1021
{
976
1022
if ( currentPkg . Dependencies . Length > 0 )
977
1023
{
@@ -1009,15 +1055,26 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
1009
1055
}
1010
1056
1011
1057
depPkg = currentResult . returnedObject ;
1012
- string pkgHashKey = String . Format ( "{0}{1}" , depPkg . Name , depPkg . Version . ToString ( ) ) ;
1013
1058
1014
- if ( ! foundPkgs . Contains ( pkgHashKey ) )
1059
+ if ( ! _packagesFound . ContainsKey ( depPkg . Name ) )
1015
1060
{
1016
- foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository , foundPkgs ) )
1061
+ foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository ) )
1017
1062
{
1018
1063
yield return depRes ;
1019
1064
}
1020
1065
}
1066
+ else
1067
+ {
1068
+ List < string > pkgVersions = _packagesFound [ depPkg . Name ] as List < string > ;
1069
+ // _packagesFound has depPkg.name in it, but the version is not the same
1070
+ if ( ! pkgVersions . Contains ( FormatPkgVersionString ( depPkg ) ) )
1071
+ {
1072
+ foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository ) )
1073
+ {
1074
+ yield return depRes ;
1075
+ }
1076
+ }
1077
+ }
1021
1078
}
1022
1079
else
1023
1080
{
@@ -1066,7 +1123,7 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
1066
1123
if ( foundDep . IsPrerelease ) {
1067
1124
depVersionStr += $ "-{ foundDep . Prerelease } ";
1068
1125
}
1069
-
1126
+
1070
1127
if ( NuGetVersion . TryParse ( depVersionStr , out NuGetVersion depVersion )
1071
1128
&& dep . VersionRange . Satisfies ( depVersion ) )
1072
1129
{
@@ -1078,26 +1135,47 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
1078
1135
{
1079
1136
continue ;
1080
1137
}
1081
-
1082
- string pkgHashKey = String . Format ( "{0}{1}" , depPkg . Name , depPkg . Version . ToString ( ) ) ;
1083
1138
1084
- if ( ! foundPkgs . Contains ( pkgHashKey ) )
1139
+ if ( ! _packagesFound . ContainsKey ( depPkg . Name ) )
1085
1140
{
1086
- foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository , foundPkgs ) )
1141
+ foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository ) )
1087
1142
{
1088
1143
yield return depRes ;
1089
1144
}
1090
1145
}
1146
+ else {
1147
+ List < string > pkgVersions = _packagesFound [ depPkg . Name ] as List < string > ;
1148
+ // _packagesFound has depPkg.name in it, but the version is not the same
1149
+ if ( ! pkgVersions . Contains ( FormatPkgVersionString ( depPkg ) ) )
1150
+ {
1151
+ foreach ( PSResourceInfo depRes in FindDependencyPackages ( currentServer , currentResponseUtil , depPkg , repository ) )
1152
+ {
1153
+ yield return depRes ;
1154
+ }
1155
+ }
1156
+ }
1091
1157
}
1092
1158
}
1093
1159
}
1094
1160
1095
- string currentPkgHashKey = String . Format ( "{0}{1}" , currentPkg . Name , currentPkg . Version . ToString ( ) ) ;
1096
-
1097
- if ( ! foundPkgs . Contains ( currentPkgHashKey ) )
1161
+ if ( ! _packagesFound . ContainsKey ( currentPkg . Name ) )
1098
1162
{
1163
+ TryAddToPackagesFound ( currentPkg ) ;
1164
+
1099
1165
yield return currentPkg ;
1100
1166
}
1167
+ else
1168
+ {
1169
+ List < string > pkgVersions = _packagesFound [ currentPkg . Name ] as List < string > ;
1170
+ // _packagesFound has currentPkg.name in it, but the version is not the same
1171
+ if ( ! pkgVersions . Contains ( FormatPkgVersionString ( currentPkg ) ) )
1172
+ {
1173
+ TryAddToPackagesFound ( currentPkg ) ;
1174
+
1175
+ yield return currentPkg ;
1176
+ }
1177
+ }
1178
+
1101
1179
}
1102
1180
1103
1181
#endregion
0 commit comments