如何设置 NuGet 包以将内容文件复制到输出构建目录? [英] How to setup a NuGet package to copy content files to output build directory?

查看:32
本文介绍了如何设置 NuGet 包以将内容文件复制到输出构建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

注意:这个问题(现已关闭),因为该问题的范围太广,该问题的第一部分(关于 .dll)已解决,这是一个单独的问题.

我正在开发一个名为 ComputeSharp.NET Standard 2.0 项目 我想作为 NuGet 包发布,但我不知道如何让包将内容文件复制到使用它的项目的输出构建目录.一些信息:

  • 该项目仅针对 .NET Standard 2.0
  • 该项目在同一解决方案中使用了 2 个引用的项目,每个项目都使用其他一些 NuGet 包.这里是我当前解决方案的屏幕.
  • 我的.nuspec在主项目ComputeSharp的文件夹下,结构如下:

<包><元数据><id>ComputeSharp</id>...<依赖项><dependency id="SharpDX.Direct3D12" version="4.2.1-beta0-gab36f12303"/>...</依赖项><内容文件><files include="..ComputeSharp.ShadersRendererTemplates*.mustache" buildAction="Content" copyToOutput="true"/></contentFiles></元数据></包>

  • 为了创建 NuGet 包,我首先在 Release 模式下构建 ComputeSharp,然后在该项目的文件夹中打开一个 cmd,然后运行:

nuget pack ComputeSharp.csproj -Prop Configuration=Release -IncludeReferencedProjects

  • 检查后生成的 NuGet 包如下所示(看起来非常好,因为我可以看到 .mustache 文件出现在包的正确文件夹结构中,位于内容" 目录):

ComputeSharp.x.x.x.nupkg├───相关│ └────……├───内容│ └───渲染器│ └───模板│ └───ShaderTemplate.mustache├───lib│ └───netstandard2.0│ ├───ComputeSharp.dll│ ├───ComputeSharp.Graphics.dll│ └───ComputeSharp.Shaders.dll├───包│ └────……├───[Content_Types].xml└───ComputeSharp.nuspec

<块引用>

问题:一旦我创建了一个测试项目并安装了 NuGet 包,我就可以很好地构建它,但是整个 RendererTemplatesShaderTemplate.mustache 树是未复制到构建目录中,因此我的 lib 无法加载该文件(因为它是相对于 lib 程序集的路径加载的).

我已经阅读了无数的 SO 问题以及文档,并在这里尝试了很多组合(例如设置 ContentType="None" 而不是 "Content">,但结果总是一样的:.mustache 文件存在于包中,但它没有被复制到使用它的项目的构建目录中.还有什么我需要做的吗?在构建项目时,是否让 NuGet 包在输出目录中重新创建该树 + 文件?

感谢您的帮助!

解决方案

我建议使用构建操作嵌入资源"将您需要的文件嵌入到程序集中.这样您就不必依赖 Nuget 来安装它们.相反,在第一次使用时,您可以从程序集中读取它们并将它们复制到文件系统中或直接使用它们.以下是如何从程序集中读取嵌入文件并将其复制到文件系统中:

private void CopyEmbeddedResourceToFile(Assembly assembly, string resourceName, string filePath){var key = GetResourceKey(assembly, resourceName);使用 (流流 = assembly.GetManifestResourceStream(key))使用 (var file = new FileStream(filePath, FileMode.Create)){如果(流==空)throw new ArgumentException($"资源名称 '{resourceName}' 未找到!");流.复制到(文件);}}

NOTE: follow up from this question (now closed), as that one was too broad in scope, and the first part of that question, regarding .dlls, has been resolved, and this is a separate issue.

I'm working on a .NET Standard 2.0 project called ComputeSharp that I'd like to publish as a NuGet package, but I can't figure out how to have the package copy a content file to the output build directory of a project using it. Some info:

  • The project only targets .NET Standard 2.0
  • The project uses 2 referenced projects in the same solution, each using some other NuGet packages. Here is a screen of my current solution.
  • My .nuspec is in the folder of the main project, ComputeSharp, and has the following structure:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ComputeSharp</id>
    ...
    <dependencies>
      <dependency id="SharpDX.Direct3D12" version="4.2.1-beta0-gab36f12303" />
      ...
    </dependencies>
    <contentFiles>
      <files include="..ComputeSharp.ShadersRendererTemplates*.mustache" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
</package>

  • In order to create the NuGet package, I first build ComputeSharp in Release mode, then open a cmd in the folder for that project, and run:

nuget pack ComputeSharp.csproj -Prop Configuration=Release -IncludeReferencedProjects

  • The resulting NuGet package, when inspected, looks like this (which looks perfectly fine, as I can see the .mustache file being present in the right folder structure in the package, under the "content" directory):

ComputeSharp.x.x.x.nupkg
├───rels
│   └───...
├───content
│   └───Renderer
│       └───Templates
│           └───ShaderTemplate.mustache
├───lib
│   └───netstandard2.0
│       ├───ComputeSharp.dll
│       ├───ComputeSharp.Graphics.dll
│       └───ComputeSharp.Shaders.dll
├───package
│   └───...
├───[Content_Types].xml
└───ComputeSharp.nuspec

PROBLEM: once I create a test project and install the NuGet package, I can build it just fine, but the whole RendererTemplatesShaderTemplate.mustache tree is not copied in the build directory, so as a result my lib can't load that file (as it's loaded relative to the path of the lib assembly).

I've read countless SO questions as well as the docs, and tried a bunch of combinations here (eg. setting ContentType="None" instead of "Content", but the result is always the same: the .mustache file is present in the package but it's not copied to the build directory of the project using it. Is there something else I need to do to just have the NuGet package recreate that tree + file in the output directory, when a project is built?

Thank you for your help!

解决方案

I suggest embedding the files you need in the assembly with build action "Embedded resource". That way you don't have to rely on Nuget to install them. Instead, upon first usage, you can read them from the assembly and copy them into the file system or directly consume them. Here is how to read an embedded file from the assembly and copy it into the file system:

private void CopyEmbeddedResourceToFile(Assembly assembly, string resourceName, string filePath)
{
        var key = GetResourceKey(assembly, resourceName);
        using (Stream stream = assembly.GetManifestResourceStream(key))
        using (var file = new FileStream(filePath, FileMode.Create))
        {
            if (stream == null)
                throw new ArgumentException($"Resource name '{resourceName}' not found!");
            stream.CopyTo(file);
        }
}

这篇关于如何设置 NuGet 包以将内容文件复制到输出构建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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