Skip to content

Instantly share code, notes, and snippets.

@emgarten
Created November 18, 2015 23:22
Show Gist options
  • Save emgarten/ebf6cddd31151731a517 to your computer and use it in GitHub Desktop.
Save emgarten/ebf6cddd31151731a517 to your computer and use it in GitHub Desktop.
Find package updates available for a packages.config file
// install-package nuget.protocol.visualstudio
using NuGet.Configuration;
using NuGet.Packaging;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.VisualStudio;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
Run().Wait();
}
static async Task Run()
{
// Target
var packagesConfigPath = @"D:\tmp\test\ConsoleApplication1\ConsoleApplication1\packages.config";
var includePrelease = true;
var includeUnlisted = false;
var packagesConfig = new PackagesConfigReader(File.OpenRead(packagesConfigPath));
// Read all nuget.config files
var nugetConfigSettings = Settings.LoadDefaultSettings(
Path.GetDirectoryName(packagesConfigPath),
configFileName: null,
machineWideSettings: null);
// Create source repositories
SourceRepositoryProvider sourceProvider = new SourceRepositoryProvider(
nugetConfigSettings,
Repository.Provider.GetVisualStudio());
var sources = sourceProvider.GetRepositories().ToList();
var token = CancellationToken.None;
foreach (var installedPackage in packagesConfig.GetPackages())
{
Console.WriteLine($"Installed package: {installedPackage.PackageIdentity}");
// Check for updates in each source
foreach (var source in sources)
{
var resource = await source.GetResourceAsync<MetadataResource>(token);
var latestVersion = await resource.GetLatestVersion(
packageId: installedPackage.PackageIdentity.Id,
includePrerelease: includePrelease,
includeUnlisted: includeUnlisted,
token: token);
if (installedPackage.PackageIdentity.Version < latestVersion)
{
Console.WriteLine($"Update available! Version: {latestVersion} Source: {source.PackageSource}");
}
else
{
Console.WriteLine($"No updates available from: {source.PackageSource}");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment