[英]How to only add a file if it doesn't already exist using Nuget and the nuspec file
When creating a nuget package and setting up the nuspec file, is there a way to only add the file if it does not already exist? 创建nuget包并设置nuspec文件时,有没有办法仅在不存在的情况下添加该文件?
I don't want the file to be replaced on every update. 我不希望每次更新都替换文件。
I have seen this answer , but it doesn't seem to be complete. 我已经看到了这个答案 ,但是它似乎并不完整。
Let's say I want a file called "example.css" to be included in a new project, but only on first install, how can i achieve that? 假设我希望一个名为“ example.css”的文件包含在一个新项目中,但是仅在首次安装时,我该如何实现?
In my nuspec file i will have this line: 在我的nuspec文件中,我将显示以下行:
<file src="css\example.css" target="content/css" />
But it overwrites anything that is there. 但是它会覆盖那里的所有内容。
How to only add a file if it doesn't already exist using Nuget and the nuspec file
如何仅使用Nuget和nuspec文件添加文件(如果尚不存在)
I have been suffering for this issue since yesterday. 自昨天以来,我一直在为这个问题苦难。 At first, I have the same idea as the accepted answer in the link provided in your question, using
install.ps1
in the Tools
folder with function that if a example.css
file already exists, nuget update will not overwrite it. 首先,我的想法与您问题中提供的链接中的可接受答案相同,使用
Tools
文件夹中的install.ps1
,其功能是,如果example.css
文件已经存在,则nuget update不会覆盖它。 So I tried to create this install.ps1
, but I failed. 因此,我尝试创建此
install.ps1
,但失败了。
That because: 那是因为:
install.ps1
runs after the package is installed in a project. install.ps1
。 Detailed for the reasons 1: 详细原因1:
According to the document Run PowerShell script during NuGetpackage installation and removal : 根据文档在安装和删除NuGetpackage期间运行PowerShell脚本 :
Install.ps1
runs when a package is installed in a project.在项目中安装软件包时,将运行
Install.ps1
。
We could get the hidden information, file Install.ps1
runs after package installed. 我们可以获取隐藏的信息,在安装软件包后运行文件
Install.ps1
。
In order to verify it, I have created a test sample nuget package with .nuspec
: 为了验证它,我使用
.nuspec
创建了一个测试样本nuget包:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyTestPackage</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="bin\Debug\MyTestPackage.dll" target="lib\net462" />
<file src="css\example.css" target="content\css\example.css" />
<file src="Tools\install.ps1" target="Tools\install.ps1" />
</files>
</package>
And the content of install.ps1
: 以及
install.ps1
的内容:
Start-Sleep -Seconds 20
write-host "Hello world!"
Start-Sleep -Seconds 5
Then I added my MyTestPackage.nupkg
to the test project, I found that the content added to the project before install.ps1
was executed . 然后我将
MyTestPackage.nupkg
添加到测试项目,发现执行install.ps1
之前添加到项目中的内容 。
Then I created a version 2.0.0 with different content in the example.css
file (Just for testing), then I updated the package from 1.0.0 to 2.0.0, the example.css
wil be replaced before install.ps1
was executed: 然后,我在
example.css
文件中创建了具有不同内容的2.0.0版本(仅供测试),然后将软件包从1.0.0更新为2.0.0,在执行install.ps1
之前将替换example.css
:
When we update the nuget package, nuget will update the package and overwrite contents first, then executed the file install.ps1
. 当我们更新nuget软件包时,nuget将更新该软件包并首先覆盖内容,然后执行文件
install.ps1
。 So it seems we could not use the file install.ps1
to prevent NuGet overwrite the file example.css
file. 因此,似乎我们不能使用文件
install.ps1
来防止NuGet覆盖文件example.css
文件。
Detailed for the reasons 2: 详细原因2:
If you pay attention to the upgrade process in the animation above, you will find that when upgrading the package, nuget will remove the package including contents first , then re-install the new version package. 如果你留意上面的动画升级过程中,你会发现,升级包时,会的NuGet 首先删除软件包,包括内容,然后重新安装新版本的包。 So, nuget will remove the package and its content first when we update the package, the result of the conditional judgment file existence is always negative.
因此,当我们更新软件包时,nuget会首先删除该软件包及其内容,条件判断文件存在的结果始终为负。 NuGet will add the .css file in the new version nuget package to the project.
NuGet会将新版本nuget包中的.css文件添加到项目中。
If you have to prevent nuget overwrite this/those file(s), As workaround for this issue, you can include this/those file(s) in a new nuget package, this/those file(s) will not be overwritten in the new nuget package when you upgrade your original package. 如果您必须防止nuget覆盖此文件,则可以使用新的nuget包中的此文件,以解决此问题。升级原始程序包时,新的nuget程序包。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.