//===================================================================== /// <summary> /// Search for packages as needed /// </summary> private async void SearchForPackages() { try { lblAction.Text = "Searching..."; grdAction.Visibility = Visibility.Visible; svDetails.IsEnabled = false; isSearching = true; var packagesFound = await Task.Run(() => { var packages = new List <NuGetPackage>(); // If limiting results to installed packages, make sure they are all there if (installedOnly && startNewSearch) { foreach (var s in searchSources) { packages.AddRange(s.SearchForPackages(projectPackages.Keys.Select(k => "Id:" + k), startNewSearch, includePreRelease)); } } foreach (var s in searchSources) { packages.AddRange(s.SearchForPackages(searchKeywords, startNewSearch, includePreRelease)); } if (installedOnly) { // If any installed packages are not found, add a dummy entry so that it can be removed foreach (var kv in projectPackages) { if (!packages.Any(p => p.Id == kv.Key)) { packages.Add(new NuGetPackage(kv.Key, kv.Value)); } } return(packages.Where(p => projectPackages.ContainsKey(p.Id)).ToList()); } return(packages); }).ConfigureAwait(true); // Combine results if already found in by a prior search or in another source in the current // search. A bit of brute force here but it'll work for what this needs to do. var packageIndex = foundPackages.ToDictionary(k => k.Id, v => v); foreach (var p in packagesFound) { if (packageIndex.TryGetValue(p.Id, out NuGetPackage prior)) { if (NuGetPackage.CompareSemanticVersion(prior.LatestVersion, p.LatestVersion) >= 0) { prior.CombineVersions(p.Versions); } else { p.CombineVersions(prior.Versions); foundPackages[foundPackages.IndexOf(prior)] = p; packageIndex[p.Id] = p; prior = p; } } else { foundPackages.Add(p); packageIndex.Add(p.Id, p); prior = p; } // Set installed package info if it's in the project if (projectPackages.TryGetValue(p.Id, out string version)) { prior.IsInstalled = true; prior.InstalledVersion = version; } } startNewSearch = packagesFound.Count == 0; } catch (Exception ex) { MessageBox.Show("Search failed: " + ex.Message, "NuGet Package Search Failure", MessageBoxButton.OK, MessageBoxImage.Error); } finally { grdAction.Visibility = Visibility.Hidden; isSearching = false; svDetails.IsEnabled = foundPackages.Count != 0; if (lbPackages.SelectedIndex == -1 && foundPackages.Count != 0) { lbPackages.SelectedIndex = 0; } } }