Skip to content

Instantly share code, notes, and snippets.

@johnsimons
Created November 9, 2015 04:57
Show Gist options
  • Select an option

  • Save johnsimons/376f1709d31900f1bfed to your computer and use it in GitHub Desktop.

Select an option

Save johnsimons/376f1709d31900f1bfed to your computer and use it in GitHub Desktop.
Finding latest production version for a major release
var repo = PackageRepositoryFactory.Default.CreateRepository("https://www.nuget.org/api/v2/");
var pkg = repo.FindPackage(packageID,
new VersionSpec
{
MaxVersion = new SemanticVersion(4, 0, 0, 0),
IsMaxInclusive = false,
MinVersion = new SemanticVersion(3, 0, 0, 0),
IsMinInclusive = true
}, false, false);
Console.Out.WriteLine("version={0}", pkg.Version);
@johnsimons

Copy link
Copy Markdown
Author

I wonder how I write this code to use the nuget v3 feed.

@emgarten

emgarten commented Nov 9, 2015

Copy link
Copy Markdown

For v3 it is similar, you create a source using the factory, a version range, and then find the highest package in that range.

Install the package for v3:
install-package Nuget.Protocol.VisualStudio

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.VisualStudio;
using NuGet.Versioning;

namespace ConsoleApplication26
{
    class Program
    {
        static void Main(string[] args)
        {
            var token = CancellationToken.None;

            var task = GetHighestMinor("nuget.versioning", 3, CancellationToken.None);
            task.Wait();
        }

        static async Task GetHighestMinor(string packageId, int major, CancellationToken token)
        {
            // Create a v3 repository
            var nugetV3 = Repository.Factory.GetVisualStudio("https://api.nuget.org/v3/index.json");

            // Use the same resource as the UI
            var resource = await nugetV3.GetResourceAsync<UIMetadataResource>(token);

            // Find all stable packages from the source
            var packages = await resource.GetMetadata(
                packageId, 
                includePrerelease: false, 
                includeUnlisted: false, 
                token: token);

            // Create the min and max versions for the range
            var minVersion = new NuGetVersion(major, 0, 0);
            var maxVersion = new NuGetVersion(major + 1, 0, 0);

            // Create a VersionRange which is the same as VersionSpec
            var range = new VersionRange(
                minVersion: minVersion,
                includeMinVersion: true,
                maxVersion: maxVersion,
                includeMaxVersion: false);

            // Find the highest package in the range
            var match = packages.Where(package => range.Satisfies(package.Identity.Version))
                .Max(package => package.Identity.Version);

            Console.Out.WriteLine("version={0}", match?.Version.ToString() ?? "Not Found");
        }
    }
}

@johnsimons

Copy link
Copy Markdown
Author

Thanks @emgarten

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment