/// <summary> /// Resolves and returns Package Sources to the client. /// /// Specified sources are passed in via the request object (<c>request.GetSources()</c>). /// /// Sources are returned using <c>request.YieldPackageSource(...)</c> /// </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(Request request) { request.Debug("Calling '{0}::ResolvePackageSources'", PackageProviderName); if (request.Sources.Any()) { // the system is requesting sources that match the values passed. // if the value passed can be a legitimate source, but is not registered, return a package source marked unregistered. var packageSources = ProviderStorage.GetPackageSources(request); if (request.IsCanceled) { return; } foreach (var source in request.Sources.AsNotNull()) { if (packageSources.ContainsKey(source)) { var packageSource = packageSources[source]; // YieldPackageSource returns false when operation was cancelled if (!request.YieldPackageSource(packageSource.Name, packageSource.Location, packageSource.Trusted, packageSource.IsRegistered, packageSource.IsValidated)) { return; } } else { request.Warning("Package Source '{0}' not found.", source); } } } else { // the system is requesting all the registered sources var packageSources = ProviderStorage.GetPackageSources(request); foreach (var entry in packageSources.AsNotNull()) { var packageSource = entry.Value; // YieldPackageSource returns false when operation was cancelled if (!request.YieldPackageSource(packageSource.Name, packageSource.Location, packageSource.Trusted, packageSource.IsRegistered, packageSource.IsValidated)) { return; } } } }
/// <summary> /// Searches package sources given name and version information /// /// Package information must be returned using <c>request.YieldPackage(...)</c> function. /// </summary> /// <param name="name">a name or partial name of the package(s) requested</param> /// <param name="requiredVersion">A specific version of the package. Null or empty if the user did not specify</param> /// <param name="minimumVersion">A minimum version of the package. Null or empty if the user did not specify</param> /// <param name="maximumVersion">A maximum version of the package. Null or empty if the user did not specify</param> /// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</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 FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, Request request) { request.Debug("Calling '{0}::FindPackage' '{1}','{2}','{3}','{4}'", PackageProviderName, requiredVersion, minimumVersion, maximumVersion, id); List <PackageSource> sources; var providerPackageSources = ProviderStorage.GetPackageSources(request); if (request.PackageSources != null && request.PackageSources.Any()) { sources = new List <PackageSource>(); foreach (var userRequestedSource in request.PackageSources) { if (providerPackageSources.ContainsKey(userRequestedSource)) { sources.Add(providerPackageSources[userRequestedSource]); } } } else { sources = providerPackageSources.Select(i => i.Value).ToList(); } var searchTerm = ReplacePowerShellWildcards(name); // Wildcard pattern matching configuration. const WildcardOptions wildcardOptions = WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase; var wildcardPattern = new WildcardPattern(String.IsNullOrEmpty(name) ? "*" : name, wildcardOptions); if (request.IsCanceled) { return; } foreach (var packageSource in sources.AsNotNull()) { var repo = RepositoryFactory.CreateV3(packageSource.Location); var search = repo.GetResource <UISearchResource>(); for (int i = 0; true; i += SearchPageSize) { List <UISearchMetadata> results; try { var task = search.Search(searchTerm, new SearchFilter(), i, SearchPageSize, CancellationToken.None); task.Wait(); results = task.Result.ToList(); } catch (NullReferenceException) { // usually means the source was incorrect, skip to the next source break; } foreach (var result in results.AsNotNull()) { if (!wildcardPattern.IsMatch(result.Identity.Id)) { continue; } var package = new DataServicePackage() { Id = result.Identity.Id, Version = result.Identity.Version.ToString(), Summary = result.Summary, Authors = result.LatestPackageMetadata.Authors, Title = result.Title, IconUrl = result.IconUrl, Owners = result.LatestPackageMetadata.Owners, Description = result.LatestPackageMetadata.Description, Tags = result.LatestPackageMetadata.Tags, LicenseUrl = result.LatestPackageMetadata.LicenseUrl, ProjectUrl = result.LatestPackageMetadata.ProjectUrl, Published = result.LatestPackageMetadata.Published, ReportAbuseUrl = result.LatestPackageMetadata.ReportAbuseUrl }; var fastPath = packageSource.MakeFastPath(result.Identity.Id, result.Identity.Version.ToString()); var packageItem = new PackageItem() { Id = result.Identity.Id, Version = result.Identity.Version.ToString(), FastPath = fastPath, Package = package, IsPackageFile = false, PackageSource = packageSource, FullPath = String.Empty }; // YieldPackage returns false when operation was cancelled if (!request.YieldPackage(packageItem, name)) { return; } } if (!results.Any()) { break; } } } }