Skip to content

Instantly share code, notes, and snippets.

@filipw
Created May 8, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipw/f3a09a2e779d5271bab0 to your computer and use it in GitHub Desktop.
Save filipw/f3a09a2e779d5271bab0 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace ScriptCs.Dnx
{
public class Program
{
public void Main(string[] args)
{
var file = @"C:\Users\filip\Documents\dev\dummy\packages.config";
var folder = @"C:\Users\filip\Documents\dev\dummy\packages";
var compatibilityProvider = new DefaultCompatibilityProvider();
var folderReader = new PackageFolderReader(folder);
var nupkgFiles = folderReader.GetFiles().Where(x => Path.GetExtension(x).ToLowerInvariant() == ".nupkg");
var packagesConfig = XDocument.Parse(File.ReadAllText(file));
var reader = new PackagesConfigReader(packagesConfig);
var contents = reader.GetPackages();
foreach (var nupkg in nupkgFiles)
{
var stream = folderReader.GetStream(nupkg);
var packageReader = new PackageReader(stream);
var identity = packageReader.GetIdentity();
var packagesConfigReference = contents.FirstOrDefault(x => x.PackageIdentity.Id == identity.Id && x.PackageIdentity.Version == identity.Version);
if (packagesConfigReference == null)
{
break;
}
var packageContents = packageReader.GetLibItems().Where(x => compatibilityProvider.IsCompatible(x.TargetFramework, packagesConfigReference.TargetFramework)).
SelectMany(x => x.Items.Where(i => Path.GetExtension(i).ToLowerInvariant() == ".dll"));
foreach (var packageContent in packageContents)
{
Console.WriteLine();
Console.WriteLine(packageContent); //this will contain PATH to DLL to be loaded
Console.WriteLine("-----");
}
}
Console.ReadLine();
}
}
}
@emgarten
Copy link

emgarten commented May 8, 2015

Try:
var packageContents = NuGetFrameworkUtility.GetNearest(packageReader.GetReferenceItems(), packagesConfigReference.TargetFramework, g => g.TargetFramework)

packageReader.GetReferenceItems() handles the *.dll filtering for you, GetLibItems is the full list of files.

NuGetFrameworkExtensions.GetNearest is another alternative/wrapper for NuGetFrameworkUtility.GetNearest that you could use.

Your check for compatible items is close, but TryGetCompatibleItems from V2 was actually finding the nearest compatible group and returning just those items, instead of all compatible items like the name implies.

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