diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index 916e5a793..c8c6f7a37 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -70,7 +70,7 @@ public FindHelper(CancellationToken cancellationToken, PSCmdlet cmdletPassedIn) #region Public methods - public IEnumerable FindByResourceName( + public List FindByResourceName( string[] name, ResourceType type, string version, @@ -86,12 +86,14 @@ public IEnumerable FindByResourceName( _tag = tag; _credential = credential; _includeDependencies = includeDependencies; - - Dbg.Assert(name.Length != 0, "Name length cannot be 0"); - _pkgsLeftToFind = name.ToList(); - List repositoriesToSearch; + List packagesFound = new List(); + + if (name.Length == 0) + { + _cmdletPassedIn.WriteVerbose("No name was provided to FindByResourceName."); + } //determine if repository array of names of repositories input to be searched contains wildcard if (repository != null) @@ -126,7 +128,7 @@ public IEnumerable FindByResourceName( "ErrorLoadingRepositoryStoreFile", ErrorCategory.InvalidArgument, this)); - yield break; + return packagesFound; } // loop through repositoriesToSearch and if PSGallery or PoshTestGallery add its Scripts endpoint repo @@ -185,9 +187,11 @@ public IEnumerable FindByResourceName( repositoryUri: repositoriesToSearch[i].Uri, repositoryCredentialInfo: repositoriesToSearch[i].CredentialInfo)) { - yield return pkg; + packagesFound.Add(pkg); } } + + return packagesFound; } #endregion @@ -351,7 +355,7 @@ private IEnumerable FindFromPackageSourceSearchAPI( if (!pkgName.Contains("*")) { // case: searching for specific package name i.e "Carbon" - IEnumerable retrievedPkgs = null; + List retrievedPkgs = null; try { // GetMetadataAsync() API returns all versions for a specific non-wildcard package name @@ -362,7 +366,7 @@ private IEnumerable FindFromPackageSourceSearchAPI( includeUnlisted: false, sourceCacheContext: sourceContext, log: NullLogger.Instance, - token: _cancellationToken).GetAwaiter().GetResult(); + token: _cancellationToken).GetAwaiter().GetResult().ToList(); } catch (HttpRequestException ex) { @@ -384,7 +388,7 @@ private IEnumerable FindFromPackageSourceSearchAPI( yield break; } - foundPackagesMetadata.AddRange(retrievedPkgs.ToList()); + foundPackagesMetadata.AddRange(retrievedPkgs); // _pkgsLeftToFind.Remove(pkgName); @@ -405,7 +409,7 @@ private IEnumerable FindFromPackageSourceSearchAPI( yield break; } // case: searching for name containing wildcard i.e "Carbon.*" - IEnumerable wildcardPkgs = null; + List wildcardPkgs = null; try { // SearchAsync() API returns the latest version only for all packages that match the wild-card name @@ -415,17 +419,19 @@ private IEnumerable FindFromPackageSourceSearchAPI( skip: 0, take: SearchAsyncMaxTake, log: NullLogger.Instance, - cancellationToken: _cancellationToken).GetAwaiter().GetResult(); + cancellationToken: _cancellationToken).GetAwaiter().GetResult().ToList(); + if (wildcardPkgs.Count() > SearchAsyncMaxReturned) { // get the rest of the packages - wildcardPkgs = wildcardPkgs.Concat(pkgSearchResource.SearchAsync( - searchTerm: pkgName, - filters: searchFilter, - skip: SearchAsyncMaxTake, - take: GalleryMax, - log: NullLogger.Instance, - cancellationToken: _cancellationToken).GetAwaiter().GetResult()); + wildcardPkgs.AddRange( + pkgSearchResource.SearchAsync( + searchTerm: pkgName, + filters: searchFilter, + skip: SearchAsyncMaxTake, + take: GalleryMax, + log: NullLogger.Instance, + cancellationToken: _cancellationToken).GetAwaiter().GetResult()); } } catch (HttpRequestException ex) @@ -448,7 +454,7 @@ private IEnumerable FindFromPackageSourceSearchAPI( // perhaps validate in Find-PSResource, and use debugassert here? WildcardPattern nameWildcardPattern = new WildcardPattern(pkgName, WildcardOptions.IgnoreCase); foundPackagesMetadata.AddRange(wildcardPkgs.Where( - p => nameWildcardPattern.IsMatch(p.Identity.Id)).ToList()); + p => nameWildcardPattern.IsMatch(p.Identity.Id))); if (!_repositoryNameContainsWildcard) { @@ -605,13 +611,13 @@ SourceCacheContext sourceCacheContext { foreach(var dep in currentPkg.Dependencies) { - IEnumerable depPkgs = packageMetadataResource.GetMetadataAsync( + List depPkgs = packageMetadataResource.GetMetadataAsync( packageId: dep.Name, includePrerelease: _prerelease, includeUnlisted: false, sourceCacheContext: sourceCacheContext, log: NullLogger.Instance, - token: _cancellationToken).GetAwaiter().GetResult(); + token: _cancellationToken).GetAwaiter().GetResult().ToList(); if (depPkgs.Count() > 0) { diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 35fea684c..4a6217d67 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -192,7 +192,7 @@ private List ProcessRepositories( var isLocalRepo = repo.Uri.AbsoluteUri.StartsWith(Uri.UriSchemeFile + Uri.SchemeDelimiter, StringComparison.OrdinalIgnoreCase); // Finds parent packages and dependencies - IEnumerable pkgsFromRepoToInstall = findHelper.FindByResourceName( + List pkgsFromRepoToInstall = findHelper.FindByResourceName( name: _pkgNamesToInstall.ToArray(), type: ResourceType.None, version: _versionRange != null ? _versionRange.OriginalString : null, @@ -200,9 +200,9 @@ private List ProcessRepositories( tag: null, repository: new string[] { repoName }, credential: credential, - includeDependencies: !skipDependencyCheck); + includeDependencies: !skipDependencyCheck).ToList(); - if (!pkgsFromRepoToInstall.Any()) + if (pkgsFromRepoToInstall.Count == 0) { _cmdletPassedIn.WriteVerbose(string.Format("None of the specified resources were found in the '{0}' repository.", repoName)); // Check in the next repository @@ -225,7 +225,7 @@ private List ProcessRepositories( pkgsFromRepoToInstall = FilterByInstalledPkgs(pkgsFromRepoToInstall); } - if (!pkgsFromRepoToInstall.Any()) + if (pkgsFromRepoToInstall.Count == 0) { continue; } @@ -261,7 +261,7 @@ private List ProcessRepositories( } // Check if any of the pkg versions are already installed, if they are we'll remove them from the list of packages to install - private IEnumerable FilterByInstalledPkgs(IEnumerable packages) + private List FilterByInstalledPkgs(List packages) { // Create list of installation paths to search. List _pathsToSearch = new List(); @@ -277,21 +277,24 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable(); - foreach (var pkg in packages) + var filteredPackages = new HashSet(packages); + + List pkgNames = new List(); + foreach (PSResourceInfo pkg in packages) { - filteredPackages.Add(pkg.Name, pkg); + pkgNames.Add(pkg.Name); } GetHelper getHelper = new GetHelper(_cmdletPassedIn); // Get currently installed packages. // selectPrereleaseOnly is false because even if Prerelease is true we want to include both stable and prerelease, never select prerelease only. - IEnumerable pkgsAlreadyInstalled = getHelper.GetPackagesFromPath( - name: filteredPackages.Keys.ToArray(), + List pkgsAlreadyInstalled = getHelper.GetPackagesFromPath( + name: pkgNames.ToArray(), versionRange: _versionRange, pathsToSearch: _pathsToSearch, - selectPrereleaseOnly: false); - if (!pkgsAlreadyInstalled.Any()) + selectPrereleaseOnly: false).ToList(); + + if (pkgsAlreadyInstalled.Count == 0) { return packages; } @@ -304,15 +307,15 @@ private IEnumerable FilterByInstalledPkgs(IEnumerable x.Equals(pkg.Name, StringComparison.InvariantCultureIgnoreCase)); } - return filteredPackages.Values.ToArray(); + return filteredPackages.ToList(); } private List InstallPackage( - IEnumerable pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well) + List pkgsToInstall, // those found to be required to be installed (includes Dependency packages as well) string repoName, string repoUri, PSCredentialInfo repoCredentialInfo, @@ -698,6 +701,7 @@ private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable) versionRange: VersionRange.All, pathsToSearch: _pathsToSearch, selectPrereleaseOnly: false); + // user parsed metadata hash List listOfCmdlets = new List(); foreach (var cmdletName in parsedMetadataHashtable["CmdletsToExport"] as object[]) diff --git a/src/code/UninstallPSResource.cs b/src/code/UninstallPSResource.cs index 96a15d362..d97f19459 100644 --- a/src/code/UninstallPSResource.cs +++ b/src/code/UninstallPSResource.cs @@ -329,13 +329,13 @@ private bool CheckIfDependency(string pkgName, out ErrorRecord errorRecord) // Results is a collection of PSModuleInfo objects that contain a property listing module dependencies, "RequiredModules". // RequiredModules is collection of PSModuleInfo objects that need to be iterated through to see if any of them are the pkg we're trying to uninstall // If we anything from the final call gets returned, there is a dependency on this pkg. - IEnumerable pkgsWithRequiredModules = new List(); + List pkgsWithRequiredModules = new List(); errorRecord = null; try { - pkgsWithRequiredModules = results.Where( + pkgsWithRequiredModules = (results.Where( pkg => ((ReadOnlyCollection)pkg.Properties["RequiredModules"].Value).Where( - rm => rm.Name.Equals(pkgName, StringComparison.InvariantCultureIgnoreCase)).Any()); + rm => rm.Name.Equals(pkgName, StringComparison.InvariantCultureIgnoreCase)).Any())).ToList(); } catch (Exception e) {