I like to use Nuget pretty extensively. It is great for distributing open source libraries to the public, but it is also a great tool for managing your own code libraries, and sharing them across projects.
There are times when I cannot simply run the NuGet utility on a single project file. Sometimes, I need to create my own NuSpec file, for example if I am including the build outputs of multiple projects, and generate a package from that. At that point, it becomes important to know the landscape of your NuGet dependencies in your projects, so you can properly reference them in a NuSpec file.
The solution is really pretty simple — each project’s dependencies are stored in a file named packages.config at the root directory of the project. This configuration file contains an XML document that lists your each dependency’s ID, version, and target framework.
="1.0"="utf-8"
<packages>
<package id="Onion.SolutionParser.Parser"
version="1.0.0.0" targetFramework="net45" />
</packages>
So all you need to do is deserialize the XML to an object, and you can catalog your dependencies, write them into a NuSpec file, or do whatever else you need to do with them.
public class NugetPackageConfigItem
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("version")]
public string Version { get; set; }
[XmlAttribute("targetFramework")]
public string TargetFramework { get; set; }
}
[XmlRoot("packages")]
public class NugetPackageConfigCollection
{
[XmlElement("package")]
public NugetPackageConfigItem[] Packages { get; set; }
public static NugetPackageConfigCollection Deserialize(string path)
{
using (var stream = File.OpenRead(path))
{
var serializer = new XmlSerializer(typeof(NugetPackageConfigCollection));
var collection = serializer.Deserialize(stream) as NugetPackageConfigCollection;
return collection;
}
}
}
Hope this tip was helpful and non-obvious. Happy coding.