using NuGet.PackageManagement; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using NuGet.Client; using NuGet.Configuration; using NuGet.PackagingCore; using NuGet.Frameworks; using NuGet.Versioning; using NuGet.Resolver; using NuGet.Packaging; using System.Diagnostics; using System.Threading; namespace ResolverTestApp { public class Program { //[Import] //public SourceRepositoryProvider SourceRepositoryProvider; [ImportMany] public Lazy[] ResourceProviders; static void Main(string[] args) { // Import Dependencies var p = new Program(); // hold onto the container, otherwise the lazy objects will be disposed! var container = p.Initialize(); // Json.NET is already installed List installed = new List(); installed.Add(new PackageReference(new PackageIdentity("Newtonsoft.Json", NuGetVersion.Parse("6.0.5")), NuGetFramework.Parse("portable-net40+win8"))); // build the repo provider instead of importing it so that it has only v3 var repositoryProvider = new SourceRepositoryProvider(new V3OnlyPackageSourceProvider(), p.ResourceProviders.ToArray()); // package to install var target = new PackageIdentity("WindowsAzure.Storage", NuGetVersion.Parse("4.0.1")); // project target framework var framework = NuGetFramework.Parse("net451"); // build repos var repos = repositoryProvider.GetRepositories(); Stopwatch timer = new Stopwatch(); // get a distinct set of packages from all repos HashSet packages = new HashSet(PackageDependencyInfo.Comparer); // find all needed packages from online foreach (var repo in repos) { // get the resolver data resource var depInfo = repo.GetResource(); // resources can always be null if (depInfo != null) { timer.Restart(); var task = depInfo.ResolvePackages(new PackageIdentity[] { target }, framework, true); task.Wait(); foreach (var pkg in task.Result) { packages.Add(pkg); } timer.Stop(); Console.WriteLine("Online fetch time: " + timer.Elapsed); } } timer.Restart(); // find the best set to install PackageResolver resolver = new PackageResolver(DependencyBehavior.Lowest); var toInstall = resolver.Resolve(new PackageIdentity[] { target }, packages, installed, CancellationToken.None); timer.Stop(); Console.WriteLine("Resolve time: " + timer.Elapsed); Console.WriteLine("------------------------"); foreach (var pkg in toInstall) { Console.WriteLine(pkg.Id + " " + pkg.Version.ToNormalizedString()); } } private CompositionContainer Initialize() { string assemblyName = Assembly.GetEntryAssembly().FullName; using (var catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly().Location), new AssemblyCatalog(Assembly.Load(assemblyName)), new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.dll"))) { var container = new CompositionContainer(catalog); container.ComposeParts(this); return container; } } /// /// Provider that only returns V3 as a source /// private class V3OnlyPackageSourceProvider : IPackageSourceProvider { public void DisablePackageSource(NuGet.Configuration.PackageSource source) { throw new NotImplementedException(); } public bool IsPackageSourceEnabled(NuGet.Configuration.PackageSource source) { return true; } public IEnumerable LoadPackageSources() { return new List() { new NuGet.Configuration.PackageSource("https://az320820.vo.msecnd.net/ver3-preview/index.json", "v3") }; } public event EventHandler PackageSourcesSaved; public void SavePackageSources(IEnumerable sources) { throw new NotImplementedException(); } public string ActivePackageSourceName { get { throw new NotImplementedException(); } } public void SaveActivePackageSource(PackageSource source) { throw new NotImplementedException(); } } } }