private bool CheckDeepLicenseAgreements(NuGet.IPackage package) { this.dependencyWalker.Clear(); this.dependencyWalker.WalkGraph(new PackageName(package.Id, package.Version.Version)); List <PackageInfo> packageGraph = this.dependencyWalker.VisitedPackages.ToList(); List <NuGet.IPackage> installedPackages = this.manager.LocalRepository.GetPackages().ToList(); foreach (PackageInfo visitedPackage in packageGraph) { // Skip the ones that are already installed if (installedPackages.Any(installed => installed.Id == visitedPackage.Id && installed.Version.Version >= visitedPackage.Version)) { continue; } if (!this.CheckLicenseAgreement(visitedPackage)) { return(false); } } return(true); }
/// <summary> /// Retrieves information about the specified package. /// </summary> /// <param name="name"></param> /// <returns></returns> public PackageInfo GetPackage(PackageName name) { PackageInfo result; lock (this.cacheLock) { if (this.nameToPackage.TryGetValue(name, out result)) { return(result); } NuGet.IPackage nugetPackage = this.GetNuGetPackage(name); if (nugetPackage == null) { result = null; } else { result = this.WrapPackage(nugetPackage); } this.nameToPackage[name] = result; } return(result); }
public void InstallPackage(NuGet.IPackage package, bool ignoreDependencies, bool allowPrereleaseVersions) { if (InstallPackageCallback != null) { InstallPackageCallback(package, ignoreDependencies, allowPrereleaseVersions); } }
public void UninstallPackage(NuGet.IPackage package, bool forceRemove, bool removeDependencies) { if (UninstallPackageCallback != null) { UninstallPackageCallback(package, forceRemove, removeDependencies); } }
private void InstallPackage(PackageName packageName, bool skipLicense) { NuGet.IPackage installPackage = this.cache.GetNuGetPackage(packageName); if (installPackage == null) { throw new InvalidOperationException(string.Format( "Unable to install package '{0}', because no matching package could be found.", packageName)); } // Check license terms if (!skipLicense && !this.CheckDeepLicenseAgreements(installPackage)) { return; } Logs.Editor.Write("Installing package '{0}'...", packageName); Logs.Editor.PushIndent(); try { // Request NuGet to install the package this.manager.InstallPackage(installPackage, false, false); } finally { Logs.Editor.PopIndent(); } }
/// <summary> /// Updates the specified package to the latest available version. /// </summary> /// <param name="packageName"></param> public void UpdatePackage(PackageName packageName) { if (!this.manager.LocalRepository.FindPackagesById(packageName.Id).Any()) { throw new InvalidOperationException(string.Format( "Can't update package '{0}', because it is not installed.", packageName)); } NuGet.IPackage latestPackage = this.cache.GetNuGetPackage(packageName.VersionInvariant); if (latestPackage == null) { throw new InvalidOperationException(string.Format( "Unable to update package '{0}', because no matching package could be found.", packageName)); } // Check license terms if (!this.CheckDeepLicenseAgreements(latestPackage)) { return; } Logs.Editor.Write("Updating package '{0}'...", packageName); Logs.Editor.PushIndent(); try { this.manager.UpdatePackage(latestPackage, true, false); } finally { Logs.Editor.PopIndent(); } }
public void UpdatePackage(NuGet.IPackage newPackage, bool updateDependencies, bool allowPrereleaseVersions) { if (UpdatePackageCallback != null) { UpdatePackageCallback(newPackage, updateDependencies, allowPrereleaseVersions); } }
private PackageInfo CreatePackageInfo(NuGet.IPackage package) { PackageInfo info = new PackageInfo(new PackageName(package.Id, package.Version.Version)); // Retrieve package data info.Title = package.Title; info.Summary = package.Summary; info.Description = package.Description; info.ReleaseNotes = package.ReleaseNotes; info.ProjectUrl = package.ProjectUrl; info.IconUrl = package.IconUrl; info.DownloadCount = package.DownloadCount; info.PublishDate = package.Published.HasValue ? package.Published.Value.DateTime : DateTime.MinValue; info.Authors = package.Authors; info.Tags = package.Tags != null?package.Tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty <string>(); // Retrieve the matching set of dependencies. For now, don't support different sets and just pick the first one. var matchingDependencySet = package.DependencySets.FirstOrDefault(); if (matchingDependencySet != null) { info.Dependencies = matchingDependencySet.Dependencies.Select(d => new PackageName(d.Id, (d.VersionSpec != null && d.VersionSpec.MinVersion != null) ? d.VersionSpec.MinVersion.Version : null)); } return(info); }
public bool CanUninstallPackage(PackageName packageName) { NuGet.IPackage uninstallPackage = this.manager.LocalRepository .FindPackagesById(packageName.Id) .FirstOrDefault(p => p.Version.Version == packageName.Version); if (uninstallPackage == null) { return(false); } bool allowed = true; this.manager.WhatIf = true; this.manager.Logger = null; try { this.manager.UninstallPackage( uninstallPackage, false, true); } catch (Exception) { allowed = false; } this.manager.Logger = this.logger; this.manager.WhatIf = false; return(allowed); }
private NuGet.IPackage FindPackageInfo(PackageName packageRef) { // Find a direct version match if (packageRef.Version != null) { // Query locally first, since we're looking for a specific version number anyway. foreach (NuGet.IPackage package in this.manager.LocalRepository.FindPackagesById(packageRef.Id)) { if (package.Version.Version == packageRef.Version) { return(package); } } // Nothing found? Query online then. try { foreach (NuGet.IPackage package in this.GetRepositoryPackages(packageRef.Id)) { if (package.Version.Version == packageRef.Version) { return(package); } } } catch (Exception) { return(null); } } // Find the newest available version online else { NuGet.IPackage[] data = null; try { IEnumerable <NuGet.IPackage> query = this.GetRepositoryPackages(packageRef.Id); data = query.ToArray(); } catch (Exception) { return(null); } var packageQuery = data.Where(p => p.IsListed() && p.IsReleaseVersion()); NuGet.IPackage newestPackage = packageQuery .OrderByDescending(p => p.Version.Version) .FirstOrDefault(); if (newestPackage != null) { return(newestPackage); } } // Nothing was found return(null); }
private Dictionary <string, string> CreateFileMapping(NuGet.IPackage package) { Dictionary <string, string> fileMapping = new Dictionary <string, string>(); bool isDualityPackage = package.Tags != null && package.Tags.Contains("Duality"); bool isPluginPackage = isDualityPackage && package.Tags.Contains("Plugin"); string binaryBaseDir = this.pluginTargetDir; string contentBaseDir = this.dataTargetDir; if (!isPluginPackage || !isDualityPackage) { binaryBaseDir = ""; } foreach (var f in package.GetFiles() .Where(f => f.TargetFramework == null || f.TargetFramework.Version < Environment.Version) .OrderByDescending(f => f.TargetFramework == null ? new Version() : f.TargetFramework.Version) .OrderByDescending(f => f.TargetFramework == null)) { // Determine where the file needs to go string targetPath = f.EffectivePath; string baseDir = f.Path; while (baseDir.Contains(Path.DirectorySeparatorChar) || baseDir.Contains(Path.AltDirectorySeparatorChar)) { baseDir = Path.GetDirectoryName(baseDir); } if (string.Equals(baseDir, "lib", StringComparison.InvariantCultureIgnoreCase)) { targetPath = Path.Combine(binaryBaseDir, targetPath); } else if (string.Equals(baseDir, "content", StringComparison.InvariantCultureIgnoreCase)) { targetPath = Path.Combine(contentBaseDir, targetPath); } else { continue; } // Add a file mapping entry linking target path to package path if (fileMapping.ContainsKey(targetPath)) { continue; } fileMapping[targetPath] = f.Path; } return(fileMapping); }
private void InstallPackage(PackageInfo package, bool skipLicense) { NuGet.IPackage newPackage = this.FindPackageInfo(package.PackageName); // Check license terms if (!skipLicense && !this.CheckDeepLicenseAgreements(newPackage)) { return; } // Request NuGet to install the package this.manager.InstallPackage(newPackage, false, false); }
public void UpdatePackage(LocalPackage package, Version specificVersion = null) { // Due to a bug in NuGet 2.8.2, specific-version downgrades are limited to the package itself, // without updating its dependencies. Otherwise, some of them might be uninstalled without // being reinstalled properly. this.uninstallQueue = null; bool isDowngrade = specificVersion != null && specificVersion < package.Version; NuGet.IPackage newPackage = this.FindPackageInfo(new PackageName(package.Id, specificVersion), false); this.manager.UpdatePackage(newPackage, !isDowngrade, false); this.uninstallQueue = new List <LocalPackage>(); }
public void UpdatePackage(LocalPackage package) { NuGet.IPackage newPackage = this.FindPackageInfo(new PackageName(package.Id)); // Check license terms if (!this.CheckDeepLicenseAgreements(newPackage)) { return; } this.uninstallQueue = null; this.manager.UpdatePackage(newPackage, true, false); this.uninstallQueue = new List <LocalPackage>(); }
private PackageInfo CreatePackageInfo(NuGet.IPackage package) { PackageInfo info = new PackageInfo(package.Id, package.Version.Version); info.Title = package.Title; info.Summary = package.Summary; info.Description = package.Description; info.ProjectUrl = package.ProjectUrl; info.IconUrl = package.IconUrl; info.DownloadCount = package.DownloadCount; info.PublishDate = package.Published.HasValue ? package.Published.Value.DateTime : DateTime.MinValue; info.Authors = package.Authors; info.Tags = package.Tags != null?package.Tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) : Enumerable.Empty <string>(); return(info); }
private bool IsUserAvailable(NuGet.IPackage package) { // Filter unlisted, non-release and special versions if (!package.IsReleaseVersion()) { return(false); } if (!package.IsListed()) { return(false); } if (package.Version != new SemanticVersion(package.Version.Version)) { return(false); } return(true); }
private PackageInfo WrapPackage(NuGet.IPackage package) { PackageName name = new PackageName(package.Id, package.Version.Version); lock (this.cacheLock) { PackageInfo info; if (this.uniquePackageWrap.TryGetValue(name, out info)) { return(info); } info = new PackageInfo(package); this.uniquePackageWrap.Add(name, info); return(info); } }
private bool CheckLicenseAgreement(NuGet.IPackage package) { if (package.RequireLicenseAcceptance) { // On the very first install, do not display additional license agreement dialogs // because all the packages being installed are the default ones. if (this.firstInstall) { return(true); } bool agreed; if (!this.licenseAcceptedCache.TryGetValue(package, out agreed) || !agreed) { PackageLicenseAgreementEventArgs args = new PackageLicenseAgreementEventArgs( new PackageName(package.Id, package.Version.Version), package.LicenseUrl, package.RequireLicenseAcceptance); if (this.PackageLicenseAcceptRequired != null) { this.PackageLicenseAcceptRequired(this, args); } else { DisplayDefaultLicenseAcceptDialog(args); } agreed = args.IsLicenseAccepted; this.licenseAcceptedCache[package] = agreed; } if (!agreed) { return(false); } } return(true); }
private bool CheckDeepLicenseAgreements(NuGet.IPackage package) { var deepDependencyCountDict = this.GetDeepDependencyCount(new[] { this.CreatePackageInfo(package) }); NuGet.IPackage[] deepPackages = deepDependencyCountDict.Keys.Select(i => this.FindPackageInfo(i.PackageName.VersionInvariant)).ToArray(); foreach (NuGet.IPackage p in deepPackages) { // Skip the ones that are already installed if (this.manager.LocalRepository.GetPackages().Any(l => l.Id == p.Id && l.Version == p.Version)) { continue; } if (!this.CheckLicenseAgreement(p)) { return(false); } } return(true); }
private NuGet.IPackage FindPackage(PackageName packageRef) { try { // Find a specific version. We're not looking for listed packages only. if (packageRef.Version != null) { // First try an indexed lookup. This may fail for freshly released packages. foreach (NuGet.IPackage package in this.GetRemotePackages(packageRef.Id)) { if (!package.IsReleaseVersion()) { continue; } if (package.Version.Version != packageRef.Version) { continue; } return(package); } // If that fails, enumerate all packages and select the one we need. // // Note: Make sure to include OrderByDescending. Without it, non-indexed // packages will not be returned from the query. IQueryable <NuGet.IPackage> query = this.repository.GetPackages() .Where(p => p.Id == packageRef.Id) .OrderByDescending(p => p.Version); foreach (NuGet.IPackage package in query) { if (!package.IsReleaseVersion()) { continue; } if (package.Version.Version != packageRef.Version) { continue; } return(package); } // No matching package was found return(null); } // Find the newest available, listed version online. else { // Enumerate all package versions - do not rely on an indexed // lookup to get the latest, as the index might not be up-to-date. // // Note: Make sure to include OrderByDescending. Without it, non-indexed // packages will not be returned from the query. IQueryable <NuGet.IPackage> query = this.repository.GetPackages() .Where(p => p.Id == packageRef.Id) .OrderByDescending(p => p.Version); // Note: IQueryable LINQ expressions will actually be transformed into // queries that are executed server-side. Unfortunately for us, the server // will order versions as if they were strings, meaning that 1.0.10 < 1.0.9. // To fix this, we'll have to iterate over them all and find the highest one // manually. We'll still include the OrderByDescending, so we avoid running // into a supposed caching mechanism that doesn't return non-indexed packages // and appears to be active when just filtering by ID. Version latestVersion = new Version(0, 0); NuGet.IPackage latestPackage = null; foreach (NuGet.IPackage package in query) { if (!this.IsUserAvailable(package)) { continue; } if (package.Version.Version > latestVersion) { latestVersion = package.Version.Version; latestPackage = package; } } return(latestPackage); } } catch (Exception e) { Log.Editor.WriteWarning("Error querying NuGet package repository: {0}", Log.Exception(e)); return(null); } }
private PackageInfo QueryPackageInfo(PackageName packageRef, bool findMaxVersionBelow) { NuGet.IPackage package = this.FindPackageInfo(packageRef, findMaxVersionBelow); return(package != null?this.CreatePackageInfo(package) : null); }
public void InstallPackage(PackageInfo package) { // Request NuGet to install the package NuGet.IPackage newPackage = this.FindPackageInfo(package.PackageName, false); this.manager.InstallPackage(newPackage, false, false); }
protected override bool IsProjectPackage(NuGet.IPackage package) { IsProjectPackageIsCalled = true; return(IsProjectPackageReturnsValue); }
public PackageInfo QueryPackageInfo(PackageName packageRef) { NuGet.IPackage package = this.FindPackageInfo(packageRef); return(package != null?this.CreatePackageInfo(package) : null); }
private static bool IsDualityPackage(NuGet.IPackage package) { return (package.Tags != null && package.Tags.Contains(DualityTag)); }
private void UninstallPackage(PackageName packageName, bool force) { Logs.Editor.Write("Uninstalling package '{0}'...", packageName); Logs.Editor.PushIndent(); try { // Find the local package that we'll uninstall NuGet.IPackage uninstallPackage = this.manager.LocalRepository .FindPackagesById(packageName.Id) .FirstOrDefault(p => p.Version.Version == packageName.Version); if (uninstallPackage == null) { return; } // Find all dependencies of the package we want to uninstall PackageName uninstallPackageName = new PackageName(uninstallPackage.Id, uninstallPackage.Version.Version); PackageInfo uninstallPackageInfo = this.GetPackage(uninstallPackageName); List <PackageInfo> uninstallDependencies = new List <PackageInfo>(); if (uninstallPackageInfo != null) { this.dependencyWalker.Clear(); this.dependencyWalker.WalkGraph(uninstallPackageInfo); uninstallDependencies.AddRange(this.dependencyWalker.VisitedPackages); uninstallDependencies.RemoveAll(package => package.Id == uninstallPackageInfo.Id); } // Filter out all dependencies that are used by other Duality packages foreach (LocalPackage otherPackage in this.LocalSetup.Packages) { if (otherPackage.Id == uninstallPackage.Id) { continue; } PackageInfo otherPackageInfo = otherPackage.Info ?? this.GetPackage(otherPackage.Name); if (otherPackageInfo == null) { continue; } this.dependencyWalker.Clear(); this.dependencyWalker.IgnorePackage(uninstallPackage.Id); this.dependencyWalker.WalkGraph(otherPackageInfo); foreach (PackageInfo dependency in this.dependencyWalker.VisitedPackages) { // Don't check versions, as dependencies are usually not resolved // with an exact version match. uninstallDependencies.RemoveAll(item => item.Id == dependency.Id); } } // Uninstall the package itself this.manager.UninstallPackage( uninstallPackage, force); // Uninstall its dependencies that are no longer used, in reverse dependency order // to avoid stumbling over dependencies that they might have between each other. this.OrderByDependencies(uninstallDependencies); uninstallDependencies.Reverse(); foreach (PackageInfo package in uninstallDependencies) { List <NuGet.IPackage> matchingNuGetPackages = this.manager.LocalRepository .FindPackagesById(package.Id) .ToList(); foreach (NuGet.IPackage nugetPackage in matchingNuGetPackages) { this.manager.UninstallPackage(nugetPackage, force, false); } } } finally { Logs.Editor.PopIndent(); } }
private Dictionary <string, string> CreateFileMapping(NuGet.IPackage package) { Dictionary <string, string> fileMapping = new Dictionary <string, string>(); bool isDualityPackage = IsDualityPackage(package); bool isPluginPackage = isDualityPackage && package.Tags.Contains(PluginTag); string folderFriendlyPackageName = package.Id; string binaryBaseDir = this.env.TargetPluginPath; string contentBaseDir = this.env.TargetDataPath; string sourceBaseDir = Path.Combine(this.env.TargetSourcePath, folderFriendlyPackageName); if (!isPluginPackage || !isDualityPackage) { binaryBaseDir = this.env.RootPath; contentBaseDir = this.env.RootPath; } IPackageFile[] packageFiles; try { packageFiles = package.GetFiles() .Where(f => f.TargetFramework == null || f.TargetFramework.Version < Environment.Version) .OrderByDescending(f => f.TargetFramework == null ? new Version() : f.TargetFramework.Version) .OrderByDescending(f => f.TargetFramework == null) .ToArray(); } catch (DirectoryNotFoundException) { // We'll run into this exception when uninstalling a package without any files. return(fileMapping); } foreach (IPackageFile f in packageFiles) { // Determine where the file needs to go string targetPath = f.EffectivePath; string baseDir = f.Path; while (baseDir.Contains(Path.DirectorySeparatorChar) || baseDir.Contains(Path.AltDirectorySeparatorChar)) { baseDir = Path.GetDirectoryName(baseDir); } if (string.Equals(baseDir, "lib", StringComparison.InvariantCultureIgnoreCase)) { targetPath = Path.Combine(binaryBaseDir, targetPath); } else if (string.Equals(baseDir, "content", StringComparison.InvariantCultureIgnoreCase)) { targetPath = Path.Combine(contentBaseDir, targetPath); } else if (string.Equals(baseDir, "source", StringComparison.InvariantCultureIgnoreCase)) { if (targetPath.StartsWith("source") && targetPath.Length > "source".Length) { targetPath = targetPath.Remove(0, "source".Length + 1); } targetPath = Path.Combine(sourceBaseDir, targetPath); } else { continue; } // Add a file mapping entry linking target path to package path if (fileMapping.ContainsKey(targetPath)) { continue; } fileMapping[targetPath] = f.Path; } return(fileMapping); }
public PackageInfo QueryPackageInfo(string packageId, Version packageVersion = null) { NuGet.IPackage package = this.FindPackageInfo(packageId, packageVersion); return(package != null?this.CreatePackageInfo(package) : null); }
public NuGetPackage(Log log, IPackageRepository repo, NuGet.IPackage package) : base(log) { _repo = repo; _package = package; }
private NuGet.IPackage FindPackage(PackageName packageRef) { try { // Find a specific version. We're not looking for listed packages only. if (packageRef.Version != null) { // First try an indexed lookup. This may fail for freshly released packages. foreach (NuGet.IPackage package in this.GetRemotePackages(packageRef.Id)) { if (!package.IsReleaseVersion()) { continue; } if (package.Version.Version != packageRef.Version) { continue; } return(package); } // If that fails, enumerate all packages and select the one we need. // // Note: Make sure to include OrderByDescending. Without it, non-indexed // packages will not be returned from the query. IQueryable <NuGet.IPackage> query = this.repository.GetPackages() .Where(p => p.Id == packageRef.Id) .OrderByDescending(p => p.Version); foreach (NuGet.IPackage package in query) { if (!package.IsReleaseVersion()) { continue; } if (package.Version.Version != packageRef.Version) { continue; } return(package); } // No matching package was found return(null); } // Find the newest available, listed version online. else { // Do an indexed lookup. This may fail for freshly released packages, // but turns out to be more robust otherwise. Our previous approach // of querying non-indexed NuGet API via search sometimes failed with // the server returning no results on the n-th query. Version latestVersion = new Version(0, 0); NuGet.IPackage latestPackage = null; foreach (NuGet.IPackage package in this.GetRemotePackages(packageRef.Id)) { if (!this.IsUserAvailable(package)) { continue; } if (package.Version.Version > latestVersion) { latestVersion = package.Version.Version; latestPackage = package; } } return(latestPackage); } } catch (Exception e) { Logs.Editor.WriteWarning("Error querying NuGet package repository: {0}", LogFormat.Exception(e)); return(null); } }