Created
November 18, 2015 23:22
-
-
Save emgarten/ebf6cddd31151731a517 to your computer and use it in GitHub Desktop.
Find package updates available for a packages.config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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