private bool InstallSinglePackage(PackageItem pkgItem, NuGetRequest request, ProgressTracker progressTracker) { bool packageWasInstalled = false; PackageItem packageToBeInstalled; request.Debug(Messages.DebugInfoCallMethod, "NuGetFilesFeed3", "InstallSinglePackage"); try { if (pkgItem == null || pkgItem.PackageSource == null || pkgItem.PackageSource.Repository == null) { return(false); } // If the source location exists as a directory then we try to get the file location and provide to the packagelocal if (Directory.Exists(pkgItem.PackageSource.Location)) { var fileLocation = pkgItem.PackageSource.Repository.FindPackage(new NuGetSearchContext() { PackageInfo = new PackageEntryInfo(pkgItem.Id), RequiredVersion = new Provider.Utility.SemanticVersion(pkgItem.Version) }, request).FullFilePath; packageToBeInstalled = NuGetClient.InstallPackageLocal(pkgItem.Id, pkgItem.Version, request, pkgItem.PackageSource, fileLocation, progressTracker); } else { string httpquery = MakeDownloadUri(pkgItem); // wait for the result from installpackage packageToBeInstalled = NuGetClient.InstallPackage(pkgItem.Id, pkgItem.Version, request, pkgItem.PackageSource, httpquery, pkgItem.Package.PackageHash, pkgItem.Package.PackageHashAlgorithm, progressTracker); } // Package is installed successfully if (packageToBeInstalled != null) { // if this is a http repository, return metadata from online if (!pkgItem.PackageSource.Repository.IsFile) { request.YieldPackage(pkgItem, packageToBeInstalled.PackageSource.Name, packageToBeInstalled.FullPath); } else { request.YieldPackage(packageToBeInstalled, packageToBeInstalled.PackageSource.Name, packageToBeInstalled.FullPath); } packageWasInstalled = true; } return(packageWasInstalled); } finally { request.Debug(Messages.DebugInfoReturnCall, "NuGetFilesFeed3", "InstallSinglePackage"); } }
/// <summary> /// Finds a package given a local filename /// </summary> /// <param name="file"></param> /// <param name="id"></param> /// <param name="request"></param> public void FindPackageByFile(string file, int id, NuGetRequest request) { // Nice-to-have put a debug message in that tells what's going on. request.Debug("Calling '{0}::FindPackageByFile' '{1}','{2}'", PackageProviderName, file, id); var pkgItem = request.GetPackageByFilePath(Path.GetFullPath(file)); if (pkgItem != null) { request.YieldPackage(pkgItem, file); } }
/// <summary> /// Finds packages given a locally-accessible filename /// /// Package information must be returned using <c>request.YieldPackage(...)</c> function. /// </summary> /// <param name="file">the full path to the file to determine if it is a package</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 PackageManagement that contains functions that can be used to interact with its Provider</param> public void FindPackageByFile(string file, int id, NuGetRequest request) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug(Resources.Messages.DebugInfoCallMethod3, PackageProviderName, file, id); var pkgItem = request.GetPackageByFilePath(Path.GetFullPath(file)); if (pkgItem != null) { request.YieldPackage(pkgItem, file); } }
/// <summary> /// Returns the packages that are installed /// </summary> /// <param name="name">the package name to match. Empty or null means match everything</param> /// <param name="requiredVersion"> /// the specific version asked for. If this parameter is specified (ie, not null or empty /// string) then the minimum and maximum values are ignored /// </param> /// <param name="minimumVersion"> /// the minimum version of packages to return . If the <code>requiredVersion</code> parameter /// is specified (ie, not null or empty string) this should be ignored /// </param> /// <param name="maximumVersion"> /// the maximum version of packages to return . If the <code>requiredVersion</code> parameter /// is specified (ie, not null or empty string) this should be ignored /// </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 GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, NuGetRequest request) { // Nice-to-have put a debug message in that tells what's going on. request.Debug("Calling '{0}::GetInstalledPackages' '{1}','{2}','{3}','{4}'", PackageProviderName, name, requiredVersion, minimumVersion, maximumVersion); if (requiredVersion != null) { minimumVersion = null; maximumVersion = null; } else { minimumVersion = minimumVersion.FixVersion(); maximumVersion = maximumVersion.FixVersion(); } if (!IsValidVersionRange(minimumVersion, maximumVersion)) { request.Error(ErrorCategory.InvalidArgument, minimumVersion + maximumVersion, Constants.Messages.InvalidVersionRange, minimumVersion, maximumVersion); return; } // look in the destination directory for directories that contain nupkg files. var subdirs = Directory.EnumerateDirectories(request.Destination); foreach (var subdir in subdirs) { var nupkgs = Directory.EnumerateFileSystemEntries(subdir, "*.nupkg", SearchOption.TopDirectoryOnly); foreach (var pkgFile in nupkgs) { var pkgItem = request.GetPackageByFilePath(pkgFile); if (pkgItem != null && pkgItem.IsInstalled) { if (pkgItem.Id.Equals(name, StringComparison.CurrentCultureIgnoreCase)) { if (!string.IsNullOrWhiteSpace(requiredVersion)) { if (pkgItem.Package.Version != new SemanticVersion(requiredVersion)) { continue; } } else { if (!string.IsNullOrWhiteSpace(minimumVersion) && pkgItem.Package.Version < new SemanticVersion(minimumVersion)) { continue; } if (!string.IsNullOrWhiteSpace(maximumVersion) && pkgItem.Package.Version < new SemanticVersion(maximumVersion)) { continue; } } request.YieldPackage(pkgItem, name); break; } if (string.IsNullOrEmpty(name) || pkgItem.Id.IndexOf(name, StringComparison.CurrentCultureIgnoreCase) > -1) { if (!request.YieldPackage(pkgItem, name)) { return; } } } } } }
private bool DownloadSinglePackage(PackageItem pkgItem, NuGetRequest request, string destLocation, ProgressTracker progressTracker) { try { request.Debug(Messages.DebugInfoCallMethod, "NuGetFilesFeed3", "DownloadSinglePackage"); if (string.IsNullOrWhiteSpace(pkgItem.PackageFilename) || pkgItem.PackageSource == null || pkgItem.PackageSource.Location == null || (pkgItem.PackageSource.IsSourceAFile && pkgItem.Package == null)) { request.WriteError(ErrorCategory.ObjectNotFound, pkgItem.Id, Constants.Messages.UnableToResolvePackage, pkgItem.Id); return(false); } // this is if the user says -force bool force = request.GetOptionValue("Force") != null; // combine the path and the file name destLocation = Path.Combine(destLocation, pkgItem.PackageFilename); // if the file already exists if (File.Exists(destLocation)) { // if no force, just return if (!force) { request.Verbose(Constants.Messages.SkippedDownloadedPackage, pkgItem.Id); request.YieldPackage(pkgItem, pkgItem.PackageSource.Name); return(true); } // here we know it is forced, so delete FileUtility.DeleteFile(destLocation, isThrow: false); // if after we try delete, it is still there, tells the user we can't perform the action if (File.Exists(destLocation)) { request.WriteError(ErrorCategory.ResourceUnavailable, destLocation, Constants.Messages.UnableToOverwriteExistingFile, destLocation); return(false); } } bool downloadSuccessful = false; try { // if no repository, we can't do anything if (pkgItem.PackageSource.Repository == null) { return(false); } if (pkgItem.PackageSource.Repository.IsFile) { using (var input = File.OpenRead(pkgItem.Package.FullFilePath)) { using (var output = new FileStream(destLocation, FileMode.Create, FileAccess.Write, FileShare.Read)) { input.CopyTo(output); downloadSuccessful = true; } } } else { string httpquery = MakeDownloadUri(pkgItem); if (!String.IsNullOrEmpty(httpquery)) { downloadSuccessful = NuGetClient.DownloadPackage(pkgItem.Id, pkgItem.Version, destLocation, httpquery, request, pkgItem.PackageSource, progressTracker); } else { downloadSuccessful = false; request.Warning(Messages.FailedToCreateDownloadUri, pkgItem.Id, pkgItem.Version); } } } catch (Exception ex) { ex.Dump(request); return(false); } if (downloadSuccessful) { request.Verbose(Resources.Messages.SuccessfullyDownloaded, pkgItem.Id); // provide the directory we save to to yieldpackage request.YieldPackage(pkgItem, pkgItem.PackageSource.Name, Path.GetDirectoryName(destLocation)); } return(downloadSuccessful); } finally { request.Debug(Messages.DebugInfoReturnCall, "NuGetFilesFeed3", "DownloadSinglePackage"); } }