Esempio n. 1
0
        private PackageDef GetPackageDefFromRepo(List <IPackageRepository> repositories, string name, VersionSpecifier version)
        {
            if (name.ToLower().EndsWith(".tappackage"))
            {
                name = Path.GetFileNameWithoutExtension(name);
            }

            var specifier = new PackageSpecifier(name, version, CpuArchitecture.Unspecified, OperatingSystem.Current.ToString());
            var packages  = PackageRepositoryHelpers.GetPackagesFromAllRepos(repositories, specifier, InstalledPackages.Values.ToArray());

            if (packages.Any() == false)
            {
                packages = PackageRepositoryHelpers.GetPackagesFromAllRepos(repositories, specifier);
                if (packages.Any())
                {
                    log.Warning($"Unable to find a version of '{name}' package compatible with currently installed packages. Some installed packages may be upgraded.");
                }
            }

            return(packages.OrderByDescending(pkg => pkg.Version).FirstOrDefault(pkg => ArchitectureHelper.PluginsCompatible(pkg.Architecture, ArchitectureHelper.HostArchitecture)));
        }
Esempio n. 2
0
        internal static PackageDef FindPackage(PackageSpecifier packageReference, bool force, Installation installation, List <IPackageRepository> repositories)
        {
            IPackageIdentifier[] compatibleWith;
            if (!force)
            {
                var tapPackage = installation.GetOpenTapPackage();
                if (tapPackage != null)
                {
                    compatibleWith = new[] { installation.GetOpenTapPackage() }
                }
                ;
                else
                {
                    compatibleWith = new[] { new PackageIdentifier("OpenTAP", PluginManager.GetOpenTapAssembly().SemanticVersion.ToString(), CpuArchitecture.Unspecified, "") }
                };
            }
            else
            {
                compatibleWith = Array.Empty <IPackageIdentifier>();
            }

            var compatiblePackages = PackageRepositoryHelpers.GetPackagesFromAllRepos(repositories, packageReference, compatibleWith);

            // Of the compatible packages, pick the one with the highest version number. If that package is available from several repositories, pick the one with the lowest index in the list in PackageManagerSettings
            PackageDef package = null;

            if (compatiblePackages.Any())
            {
                package = compatiblePackages.GroupBy(p => p.Version).OrderByDescending(g => g.Key).FirstOrDefault()
                          .OrderBy(p => repositories.IndexWhen(e => NormalizeRepoUrl(e.Url) == NormalizeRepoUrl((p.PackageSource as IRepositoryPackageDefSource)?.RepositoryUrl))).FirstOrDefault();
            }

            // If no package was found, try to figure out why
            if (package == null)
            {
                var compatibleVersions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, packageReference.Name, compatibleWith);
                var versions           = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, packageReference.Name);

                // Any packages compatible with opentap and platform
                var filteredVersions = compatibleVersions.Where(v => v.IsPlatformCompatible(packageReference.Architecture, packageReference.OS)).ToList();
                if (filteredVersions.Any())
                {
                    // if the specified version exist, don't say it could not be found.
                    if (versions.Any(v => packageReference.Version.IsCompatible(v.Version)))
                    {
                        throw new ExitCodeException(1, $"Package '{packageReference.Name}' matching version '{packageReference.Version}' is not compatible. Latest compatible version is '{filteredVersions.FirstOrDefault().Version}'.");
                    }
                    else
                    {
                        throw new ExitCodeException(1, $"Package '{packageReference.Name}' matching version '{packageReference.Version}' could not be found. Latest compatible version is '{filteredVersions.FirstOrDefault().Version}'.");
                    }
                }

                // Any compatible with platform but not opentap
                filteredVersions = versions.Where(v => v.IsPlatformCompatible(packageReference.Architecture, packageReference.OS)).ToList();
                if (filteredVersions.Any() && compatibleWith.Any())
                {
                    var opentapPackage = compatibleWith.First();
                    throw new ExitCodeException(1, $"Package '{packageReference.Name}' does not exist in a version compatible with '{opentapPackage.Name}' version '{opentapPackage.Version}'.");
                }

                // Any compatible with opentap but not platform
                if (compatibleVersions.Any())
                {
                    if (packageReference.Version != VersionSpecifier.Any || packageReference.OS != null || packageReference.Architecture != CpuArchitecture.Unspecified)
                    {
                        throw new ExitCodeException(1,
                                                    string.Format("No '{0}' package {1} was found.", packageReference.Name, string.Join(" and ",
                                                                                                                                        new string[] {
                            packageReference.Version != VersionSpecifier.Any ? $"compatible with version '{packageReference.Version}'": null,
                            packageReference.OS != null ? $"compatible with '{packageReference.OS}' operating system" : null,
                            packageReference.Architecture != CpuArchitecture.Unspecified ? $"with '{packageReference.Architecture}' architecture" : null
                        }.Where(x => x != null).ToArray())));
                    }
                    else
                    {
                        throw new ExitCodeException(1, $"Package '{packageReference.Name}' does not exist in a version compatible with this OS and architecture.");
                    }
                }

                // Any version
                if (versions.Any())
                {
                    var opentapPackage = compatibleWith.FirstOrDefault();
                    if (opentapPackage != null)
                    {
                        throw new ExitCodeException(1, $"Package '{packageReference.Name}' does not exist in a version compatible with this OS, architecture and '{opentapPackage.Name}' version '{opentapPackage.Version}'.");
                    }
                    else
                    {
                        throw new ExitCodeException(1, $"Package '{packageReference.Name}' does not exist in a version compatible with this OS and architecture.");
                    }
                }

                throw new ExitCodeException(1, $"Package '{packageReference.Name}' could not be found in any repository.");
            }
            return(package);
        }
Esempio n. 3
0
        private static VersionSpecifier RequiredApiVersion = new VersionSpecifier(3, 1, 0, "", "", VersionMatchBehavior.Compatible | VersionMatchBehavior.AnyPrerelease); // Required for GraphQL

        internal static List <PackageDef> GetPackageNameAndVersionFromAllRepos(List <IPackageRepository> repositories, PackageSpecifier id, params IPackageIdentifier[] compatibleWith)
        {
            var list = new List <PackageDef>();

            try
            {
                string query =
                    @"query Query {
                            packages(distinctName:true" + (id != null ? $",version:\"{id.Version}\",os:\"{id.OS}\",architecture:\"{id.Architecture}\"" : "") + @") {
                            name
                            version
                        }
                    }";

                Parallel.ForEach(repositories, repo =>
                {
                    if (repo is HttpPackageRepository httprepo && httprepo.Version != null && RequiredApiVersion.IsCompatible(httprepo.Version))
                    {
                        var json = httprepo.Query(query);
                        lock (list)
                        {
                            foreach (var item in json["packages"])
                            {
                                list.Add(new PackageDef()
                                {
                                    Name = item["name"].ToString(), Version = SemanticVersion.Parse(item["version"].ToString())
                                });
                            }
                        }
                    }
                    else
                    {
                        var packages = repo.GetPackages(id, compatibleWith);
                        lock (list)
                        {
                            list.AddRange(packages);
                        }
                    }
                });
Esempio n. 4
0
 public static PackageDef[] GetPackages(this IPackageRepository repository, PackageSpecifier package, params IPackageIdentifier[] compatibleWith)
 {
     return(repository.GetPackages(package, TapThread.Current.AbortToken, compatibleWith));
 }
Esempio n. 5
0
        private PackageDef GetPackageDef(Installation targetInstallation)
        {
            // Try a number of methods to obtain the PackageDef in order of precedence
            var        packageRef = new PackageSpecifier(Name, VersionSpecifier.Parse(Version ?? ""), Architecture, OS);
            PackageDef package;

            // a relative or absolute file path
            if (File.Exists(Name))
            {
                package = PackageDef.FromPackage(Name);

                if (package != null)
                {
                    Offline = true;
                    return(package);
                }
            }

            // a currently installed package
            package = targetInstallation.GetPackages().FirstOrDefault(p => p.Name == Name && versionSpec.IsCompatible(p.Version));
            if (package != null)
            {
                return(package);
            }

            if (Offline == false)
            {
                try
                {
                    // a release from repositories
                    package = repositories.SelectMany(x => x.GetPackages(packageRef))
                              .FindMax(p => p.Version);
                    if (package != null)
                    {
                        return(package);
                    }
                }
                catch (System.Net.WebException e)
                {
                    // not connected to the internet
                    log.Error(e.Message);
                    log.Warning("Could not connect to repository. Showing results for local install");
                    DisableHttpRepositories();

                    package = repositories.SelectMany(x => x.GetPackages(packageRef))
                              .FindMax(p => p.Version);
                    if (package != null)
                    {
                        return(package);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(Version))
            {
                log.Warning($"{Name} version {Version} not found.");
            }

            if (Offline == false && string.IsNullOrWhiteSpace(Version))
            {
                // a prerelease from repositories
                packageRef = new PackageSpecifier(Name, VersionSpecifier.Parse("any"), Architecture, OS);
                package    = repositories.SelectMany(x => x.GetPackages(packageRef))
                             .FindMax(p => p.Version);
            }

            return(package);
        }