3

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.

4
  • Show the full stack trace. Also, if you need regions inside methods, you might want to split up the method in smaller ones. Commented Oct 16, 2014 at 14:38
  • I've added the stack trace to the question. Not sure about the regions thing - if the code isn't reused anywhere, it seems unnecessary to extract it into a method, but that's a whole other topic! Commented Oct 16, 2014 at 14:59
  • any final solution using latest version of NuGet.Core v2.8.5 is working for automate this process ? Commented Nov 11, 2014 at 9:50
  • I'm not sure I understand your question; the code above works with NuGet.Core v2.8.5 - as indicated by the answer below. Does it work for you? Commented Nov 11, 2014 at 10:42

1 Answer 1

1

Well, I tried a few different ways to do the same thing, but nothing worked. So I noticed that the code versions on http://nuget.codeplex.com/SourceControl were all v2.0 upwards, and decided to check my referenced DLL (which was the stock .NET 4.0 or 4.5 version), and I noticed that my NuGet.Core reference was for version 1.6.3.

So, I used NuGet Package Manager to get the latest version of NuGet.Core (which is v2.8.5 at the time of writing), and now the exact code above works just fine! *

Alanis Morissette would probably say that's ironic.

  • I did have to change the File.Open just before the save, as I'd missed out the file name.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.