我正在編寫一個控制檯應用程序來創建NuGet包(使用Nuget.Core庫),該包將託管在我們的內部服務器上以部署到我們的應用程序中。當我們使用NuGet包資源管理器手動創建包時,這一切都正常,但我們需要現在自動執行此過程。以編程方式爲本地存儲庫創建NuGet包
我有下面的代碼拼湊來自動創建包,但我上線的錯誤builder.Populate(packageMetadata);
的錯誤是:
Value cannot be null.
Parameter name: source
這裏的堆棧跟蹤:
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
我看過NuGet的源代碼(請參閱here),但我無法弄清楚是什麼導致它。
關於下面的代碼的幾個注意事項:
- 前兩個區域都只是找出新包的版本號,你可以忽略它們
- ReferencePaths.NuGetPackages是一個靜態的字符串我們當地的NuGet庫的路徑
- 該代碼拿起提供
這裏內置DLL和使用的packageName PDB文件和路徑是代碼:
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);
}
}
}
有誰知道什麼可能是錯的?我需要ManifestMetadata中的更多信息嗎?我嘗試填寫所有我能想到的屬性,但它沒有幫助。
顯示完整的堆棧跟蹤。另外,如果您需要區域內的區域,您可能需要將方法拆分爲較小的區域。 – CodeCaster 2014-10-16 14:38:10
我已將堆棧跟蹤添加到問題中。不確定地區的事情 - 如果代碼沒有在任何地方重複使用,似乎沒有必要將它提取到一個方法中,但這是另一個話題! – 2014-10-16 14:59:31
使用最新版本的NuGet.Core v2.8.5的任何最終解決方案都可以使這個過程自動化? – Kiquenet 2014-11-11 09:50:49