/// <summary> /// Gets the latest version of the specified NuGet package. /// </summary> /// <param name="client">The <see cref="NuGetClient"/> instance.</param> /// <param name="packageId">The ID of the NuGet package.</param> /// <param name="includePrerelease"> /// Indicates whether to consider pre-release versions of the package when looking for the latest version. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <returns> /// The NuGet package as a <see cref="Stream"/>; or <c>null</c> if the specific package was /// not found at any of the sources. /// </returns> public static async Task <Stream?> GetLatestPackageAsync(this NuGetClient client, string packageId, bool includePrerelease = false, CancellationToken cancellationToken = default) { NuGetVersion?latestVersion = await client.GetPackageLatestVersionAsync(packageId, includePrerelease, cancellationToken); if (latestVersion is null) { return(null); } return(await client.GetPackageAsync(packageId, latestVersion.Value, cancellationToken)); }
/// <summary> /// Downloads the latest version of a NuGet package to a directory. /// </summary> /// <param name="client">The <see cref="NuGetClient"/> instance.</param> /// <param name="packageId">The ID of the NuGet package.</param> /// <param name="directory">The directory to save the NuGet package to.</param> /// <param name="fileName"> /// Optional file name for the downloaded package. If not specified, then the name is /// <c>[package id].[version].nupkg</c>. /// </param> /// <param name="overwrite"> /// Indicates whether to overwrite the package file, if it already exists (default: false). /// </param> /// <param name="includePrerelease"> /// Indicates whether to consider pre-release versions of the package when calculating the /// version of the latest package. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <returns> /// A <see cref="FileInfo"/> instance denoting the downloaded file; <c>null</c> if the /// package could not be downloaded and saved. /// </returns> public static async Task <FileInfo?> DownloadLatestPackageAsync(this NuGetClient client, string packageId, string directory, string?fileName = null, bool overwrite = false, bool includePrerelease = false, CancellationToken cancellationToken = default) { NuGetVersion?latestVersion = await client.GetPackageLatestVersionAsync(packageId, includePrerelease, cancellationToken); if (latestVersion is null) { return(null); } return(await client.DownloadPackageAsync(packageId, latestVersion.Value, directory, fileName, overwrite, cancellationToken)); }