I am writing a console application to create NuGet packages (using the Nuget.Core library) which will be hosted on our internal server to be deployed into our applications. This all works fine when we manually create the packages using the NuGet Package Explorer, but we need to automate this process now.
I have the following code pieced together to automatically build the package, but I get an error on the line builder.Populate(packageMetadata);
The error is:
Value cannot be null.
Parameter name: source
Here's the stack trace:
at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
at NuGet.PackageBuilder.Populate(ManifestMetadata manifestMetadata)
at BuildPackage.Create(String packageName, String path, String description) in c:\Source Code\Visual Studio Projects\Tools\Console Applications\NuGet Package Builder\BuildPackage.cs:line 134
I've had a look at the source code for NuGet (see here), but I can't figure out what is causing it.
A few notes about the code below:
- The first two regions are just to figure out the version number for the new package, you can ignore them
- ReferencePaths.NuGetPackages is a static string for the path of our local NuGet repository
- This code picks up the built DLL and PDB files using the packageName and path provided
Here's the code:
class BuildPackage
{
public static void Create(string packageName, string path, string description)
{
try
{
#region Get the current package version
int major = 0;
int minor = 0;
int packageNo = 0;
List<string> files = Directory.EnumerateFiles(ReferencePaths.NuGetPackages, packageName + "*").ToList();
if (files.Count > 0)
{
foreach (string file in files)
{
string[] versions = file.Replace(ReferencePaths.NuGetPackages, "").Replace(packageName + ".", "").Replace(".nupkg", "").Split('.');
int newMajor = Convert.ToInt32(versions[0]);
int newMinor = Convert.ToInt32(versions[1]);
int newPackageNo = Convert.ToInt32(versions[2]);
// Figure out if this is the latest package
if (newMajor > major ||
(newMajor == major && newMinor > minor) ||
(newMajor == major && newMinor == minor && newPackageNo > packageNo))
{
major = newMajor;
minor = newMinor;
packageNo = newPackageNo;
}
}
}
#endregion Get the current package version
#region Get the new assembly version
FileVersionInfo version = FileVersionInfo.GetVersionInfo(path + packageName + ".dll");
if (version.FileMajorPart > major ||
(version.FileMajorPart == major && version.FileMinorPart > minor))
{
major = version.FileMajorPart;
minor = version.FileMinorPart;
packageNo = 0;
}
else
{
while (File.Exists(ReferencePaths.NuGetPackages + packageName + "." + major.ToString() + "." + minor.ToString() + "." + packageNo.ToString() + ".nupkg"))
{
packageNo++;
}
}
#endregion Get the new assembly version
#region Create the package
string packageVersion = major.ToString() + "." + minor.ToString() + "." + packageNo.ToString();
string newPackageName = packageName + "." + packageVersion + ".nupkg";
ManifestMetadata packageMetadata = new ManifestMetadata();
packageMetadata.Id = packageName;
packageMetadata.Version = packageVersion;
packageMetadata.Authors = "Test";
packageMetadata.Description = description;
List<ManifestFile> manifestFiles = new List<ManifestFile>();
ManifestFile dllFile = new ManifestFile();
dllFile.Source = packageName + ".dll";
dllFile.Target = @"lib\" + packageName + ".dll";
manifestFiles.Add(dllFile);
ManifestFile pdbFile = new ManifestFile();
pdbFile.Source = packageName + ".pdb";
pdbFile.Target = @"lib\" + packageName + ".pdb";
manifestFiles.Add(pdbFile);
PackageBuilder builder = new PackageBuilder();
builder.PopulateFiles(path, manifestFiles);
builder.Populate(packageMetadata);
using (FileStream stream = File.Open(ReferencePaths.NuGetPackages + newPackageName, FileMode.OpenOrCreate))
{
builder.Save(stream);
}
#endregion Create the package
Console.WriteLine("New package created: " + newPackageName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Does anyone know what could be wrong? Do I need some more info in the ManifestMetadata? I tried filling out all the properties I could think of, but it didn't help.