private static bool IsVersionAcceptable(PredicatePackageAcceptable predicatePackageAcceptable, IConstraint constraint, string name, ConfigPackageBucket config) { // todo: to load aliase package. var versions = new[] { config.VersionNormalized }; foreach (var version in versions) { if (predicatePackageAcceptable != null && !predicatePackageAcceptable(VersionParser.ParseStability(version), name)) { continue; } if (constraint != null && !constraint.Matches(new Constraint("==", version))) { continue; } return(true); } return(false); }
/// <inheritdoc /> public IPackage[] WhatProvides(string name, PredicatePackageAcceptable predicatePackageAcceptable = null) { // skip platform packages, root package and bucket-plugin-api if (Regex.IsMatch(name, RepositoryPlatform.RegexPlatform) || name == ConfigBucketBase.RootPackage || name == PluginManager.PluginRequire) { return(Array.Empty <IPackage>()); } AssertLoadProviderMapping(); if (string.IsNullOrEmpty(providersUri)) { return(Array.Empty <IPackage>()); } // package does not exist in this repo if (!providers.TryGetValue(name, out ConfigMetadata metadata)) { return(Array.Empty <IPackage>()); } var hash = metadata["sha256"]; var requestUri = providersUri.Replace("%package%", name).Replace("%hash%", hash); var cacheKey = $"provider-{name.Replace('/', '$')}.json"; ConfigPackages data = null; if (!string.IsNullOrEmpty(hash) && cache.TryReadSha256(cacheKey, out string content, hash)) { data = JsonFile.Parse <ConfigPackages>(content); } if (!data) { data = FetchFile <ConfigPackages>(requestUri, cacheKey, hash); } var result = new Dictionary <string, IPackage>(); var versionsToLoad = new Dictionary <int, ConfigPackageBucket>(); foreach (var versions in data.Packages) { foreach (var version in versions.Value) { var packageConfig = version.Value; var uid = packageConfig.Uid; var normalizedName = packageConfig.Name.ToLower(); // only load the actual named package, not other packages // that might find themselves in the same file if (normalizedName != name) { continue; } if (versionsToLoad.ContainsKey(uid)) { continue; } if (string.IsNullOrEmpty(packageConfig.VersionNormalized)) { packageConfig.VersionNormalized = versionParser.Normalize(packageConfig.Version); } if (IsVersionAcceptable(predicatePackageAcceptable, null, normalizedName, packageConfig)) { versionsToLoad[uid] = packageConfig; } } } // load acceptable packages in the providers var loadedPackages = CreatePackages(versionsToLoad.Values, typeof(IPackageComplete)); var uids = versionsToLoad.Keys.ToArray(); var index = 0; foreach (var package in loadedPackages) { package.SetRepository(this); var uid = uids[index++]; if (package is PackageAlias packageAlias) { var aliased = packageAlias.GetAliasOf(); aliased.SetRepository(this); result[uid.ToString()] = aliased; result[$"{uid}-alias"] = package; } else { result[uid.ToString()] = package; } } return(result.Values.ToArray()); }