PackageDef GetPackageDefFromInstallation(string name, VersionSpecifier version) { if (name.ToLower().EndsWith(".tappackage")) { name = Path.GetFileNameWithoutExtension(name); } if (InstalledPackages.ContainsKey(name)) { PackageDef package = InstalledPackages[name]; // Check that the installed package is compatible with the required package if (version.IsCompatible(package.Version)) { return(package); } } return(null); }
protected override int LockedExecute(CancellationToken cancellationToken) { if (OS == null) { switch (Environment.OSVersion.Platform) { case PlatformID.MacOSX: OS = "OSX"; break; case PlatformID.Unix: OS = "Linux"; break; default: OS = "Windows"; break; } } List <IPackageRepository> repositories = new List <IPackageRepository>(); if (Installed == false) { if (Repository == null) { repositories.AddRange(PackageManagerSettings.Current.Repositories.Where(p => p.IsEnabled).Select(s => s.Manager)); } else { repositories.AddRange(Repository.Select(s => PackageRepositoryHelpers.DetermineRepositoryType(s))); } } if (Target == null) { Target = FileSystemHelper.GetCurrentInstallationDirectory(); } HashSet <PackageDef> installed = new Installation(Target).GetPackages().ToHashSet(); VersionSpecifier versionSpec = VersionSpecifier.Parse("^"); if (!String.IsNullOrWhiteSpace(Version)) { versionSpec = VersionSpecifier.Parse(Version); } if (string.IsNullOrEmpty(Name)) { var packages = installed.ToList(); packages.AddRange(PackageRepositoryHelpers.GetPackageNameAndVersionFromAllRepos(repositories, new PackageSpecifier("", versionSpec, Architecture, OS))); if (Installed) { packages = packages.Where(p => installed.Any(i => i.Name == p.Name)).ToList(); } PrintReadable(packages, installed); } else { IPackageIdentifier package = installed.FirstOrDefault(p => p.Name == Name); if (Installed) { if (package is null) { log.Info($"{Name} is not installed"); return(-1); } log.Info(package.Version.ToString()); return(0); } List <PackageVersion> versions = null; if (All) { versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name).Distinct().ToList(); var versionsCount = versions.Count; if (versionsCount == 0) // No versions { log.Info($"No versions of '{Name}'."); return(0); } if (Version != null) // Version is specified by user { versions = versions.Where(v => versionSpec.IsCompatible(v.Version)).ToList(); } if (versions.Any() == false && versionsCount > 0) { log.Info($"Package '{Name}' does not exists with version '{Version}'."); log.Info($"Package '{Name}' exists in {versionsCount} other versions, please specify a different version."); return(0); } } else { var opentap = new Installation(Target).GetOpenTapPackage(); versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name, opentap).Distinct().ToList(); versions = versions.Where(s => s.IsPlatformCompatible(Architecture, OS)).ToList(); if (versions.Any() == false) // No compatible versions { versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name).ToList(); if (versions.Any()) { log.Warning($"There are no compatible versions of '{Name}'."); log.Info($"There are {versions.Count} incompatible versions available. Use '--all' to show these."); } else { log.Warning($"Package '{Name}' could not be found in any repository."); } return(0); } versions = versions.Where(v => versionSpec.IsCompatible(v.Version)).ToList(); if (versions.Any() == false) // No versions that are compatible { if (string.IsNullOrEmpty(Version)) { log.Warning($"There are no released versions of '{Name}'."); } else { log.Warning($"Package '{Name}' does not exists with version '{Version}'."); } var anyPrereleaseSpecifier = new VersionSpecifier(versionSpec.Major, versionSpec.Minor, versionSpec.Patch, versionSpec.PreRelease, versionSpec.BuildMetadata, VersionMatchBehavior.AnyPrerelease | versionSpec.MatchBehavior); versions = versions.Where(v => anyPrereleaseSpecifier.IsCompatible(v.Version)).ToList(); if (versions.Any()) { log.Info($"There are {versions.Count} pre-released versions available. Use '--version <pre-release>' (e.g. '--version rc') or '--all' to show these."); } return(0); } } PrintVersionsReadable(package, versions); } return(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); } } });