如何判断的NuGet到包资源文件添加为链接,而不是将它们复制到项目目录而不是、如何判断、链接、文件

2023-09-04 10:55:37 作者:无名氏

要带一些资源文件成的NuGet包,什么人通常会做的,就是下面。

To pack some resource files into a nuget package, what one would normally do, is the following.

把所有的资源文件到内容\一的NuGet包目录。这将通过在 .nuspec 文件中的以下行来指定:

Put all the resource files into the content\ directory of a nuget package. This would be specified by the following line in a .nuspec file:

<files>
  <file src="Project\bin\Release\script.js" target="content\js\script.js" />
<files>

现在,当这的NuGet软件包被安装到 AnotherProject ,以下文件结构出现了:

Now, when this nuget package gets installed into AnotherProject, the following file structure emerges:

Solution.sln
packages\Project.1.0.0\content\js\script.js  // the original resource file
AnotherProject\js\script.js                  // a physical copy 
AnotherProject\AnotherProject.csproj         // <Content /> tag (see below)

包安装过程中, AnotherProject.csproj 与标签注入:

<Content Include="js\script.js" />

这是原始资源的物理副本(这是在包\ 目录)。

我的目的是不要有下 AnotherProject 目录下的资源文件,而是一个链接到原始资源的物理副本包\ 目录。在的csproj,这应该是这样的:

My aim is not to have the physical copy of a resource file in the AnotherProject directory but rather a "link" to the original resource under packages\ directory. In the csproj, this should look like this:

<Content Include="packages\Project.1.0.0\content\js\script.js">
  <Link>js\script.js</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

蛮力解决方案,我宁愿避免

现在,一做了艰辛的道路的解决办法我能想到的是:

Brute force solution that I would rather avoid

Now, one "do it the hard way" workaround I can think of is:

在不把在内容\资源文件,使他们没有得到自动添加, 写 Install.ps1 脚本,将破解的csproj文件结构和手动添加必要的XML块, not putting resource files under content\ so they do not get added automatically, writing Install.ps1 script that would hack the csproj file structure and add the necessary XML piece manually,

不过,这有以下缺点:

在我所有的Nu​​Get软件包需要相同的脚本块在其 Install.ps1 , 在安装我的包时,将有在Visual Studio中一个讨厌的项目重装提示。

推荐答案

由于目前的NuGet不支持此开箱你的选择是要么使用PowerShell或使用自定义的MSBuild目标。

Since NuGet currently does not support this out of the box your options are either to use PowerShell or to use a custom MSBuild target.

的PowerShell

将您的资源内容目录之外的你的NuGet包(因为你已经建议)。 添加在install.ps1使用PowerShell中的文件链接。

您应该能够避免的项目重装提示如果您使用Visual Studio对象模型(EnvDTE)。我想看看Project.ProjectItems.AddFromFile(...),看看是否适合您。

You should be able to avoid the project reload prompt if you use the Visual Studio object model (EnvDTE). I would take a look at Project.ProjectItems.AddFromFile(...) to see if that works for you.

的MSBuild目标

的NuGet支持添加一个import语句为指向一个MSBuild的.props和/或.targets文件的项目。所以,你可以把你的资源投入到工具您的NuGet包的目录,并从一个自定义的MSBuild .props / .targets文件中引用它们。 NuGet supports adding an import statement into a project that points to an MSBuild .props and/or .targets file. So you could put your resources into the tools directory of your NuGet package and reference them from a custom MSBuild .props/.targets file.

通常情况下,定制.props和.targets用于自定义生成过程。但是他们只是MSBuild项目文件,所以你可以为你的资源,将项目添加到这些项目文件。

Typically the custom .props and .targets are used to customise the build process. However they are just MSBuild project files so you could add items for your resources into these project files.

注意.props是进口的,在安装了的NuGet包当项目文件的开头,而.targets是在项目结束导入。

Note that .props are imported at the start of the project file when a NuGet package is installed, whilst .targets are imported at the end of the project.

定制的NuGet

另一种选择,这将需要更多的工作,将是修改的NuGet支持你想做的事情。

Another option, which would take more work, would be to modify NuGet to support what you want to do.