以编程方式为本地存储库创建NuGet包 [英] Programatically creating a NuGet package for a local repository

查看:57
本文介绍了以编程方式为本地存储库创建NuGet包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个控制台应用程序以创建NuGet包(使用Nuget.Core库),这些包将托管在我们的内部服务器上,并部署到我们的应用程序中。当我们使用NuGet软件包资源管理器手动创建软件包时,所有这些都可以正常工作,但是我们现在需要使该过程自动化。

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.

我将以下代码组合在一起以自动生成包,但在 builder.Populate(packageMetadata);上出现错误:

I have the following code pieced together to automatically build the package, but I get an error on the line 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的源代码(参见此处),但我不知道

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:


  • 前两个区域只是为了弄清楚新软件包的版本号,您可以忽略它们

  • ReferencePaths.NuGetPackages是我们l路径的静态字符串。 ocal NuGet存储库

  • 此代码使用提供的packageName和路径来拾取生成的DLL和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中获取更多信息吗?我尝试填写我可能想到的所有属性,但这无济于事。

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.

推荐答案

好吧,我尝试了一些做同一件事的不同方法,但没有任何效果。因此,我注意到 http://nuget.codeplex.com/SourceControl 上的代码版本均为v2。 0向上,并决定检查我所引用的DLL(原始的.NET 4.0或4.5版本),我注意到我的NuGet.Core引用适用于版本1.6.3。

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.

因此,我使用了NuGet软件包管理器来获取最新版本的NuGet.Core(在撰写本文时为v2.8.5),现在上面的确切代码可以正常工作! *

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可能会说讽刺。

Alanis Morissette would probably say that's ironic.


  • 我确实不得不在保存之前更改 File.Open ,因为我错过了文件名。

  • I did have to change the File.Open just before the save, as I'd missed out the file name.

这篇关于以编程方式为本地存储库创建NuGet包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆