title: Programatically creating a NuGet package for a local repository
body: <p>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.</p>
  
  <p>I have the following code pieced together to automatically build the package, but I get an error on the line <code>builder.Populate(packageMetadata);</code></p>
  
  <p>The error is:</p>
  
  <pre><code>Value cannot be null.
  
  Parameter name: source
  </code></pre>
  
  <p>Here's the stack trace:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I've had a look at the source code for NuGet (see <a href="http://nuget.codeplex.com/SourceControl/latest#src/Core/Authoring/PackageBuilder.cs" rel="nofollow">here</a>), but I can't figure out what is causing it.</p>
  
  <p>A few notes about the code below:</p>
  
  <ul>
  <li>The first two regions are just to figure out the version number for the new package, you can ignore them </li>
  <li>ReferencePaths.NuGetPackages is a static string for the path of our local NuGet repository </li>
  <li>This code picks up the built DLL and PDB files using the packageName and path provided</li>
  </ul>
  
  <p>Here's the code:</p>
  
  <pre><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&lt;string&gt; files = Directory.EnumerateFiles(ReferencePaths.NuGetPackages, packageName + "*").ToList();
  
              if (files.Count &gt; 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 &gt; major ||
                          (newMajor == major &amp;&amp; newMinor &gt; minor) ||
                          (newMajor == major &amp;&amp; newMinor == minor &amp;&amp; newPackageNo &gt; 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 &gt; major ||
                  (version.FileMajorPart == major &amp;&amp; version.FileMinorPart &gt; 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&lt;ManifestFile&gt; manifestFiles = new List&lt;ManifestFile&gt;();
              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);
          }
      }
  }
  </code></pre>
  
  <p>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.</p>