简体   繁体   English

如何仅使用Nuget和nuspec文件添加文件(如果尚不存在)

[英]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: 那是因为:

  1. The file install.ps1 runs after the package is installed in a project. 在程序包中安装软件包之后,将运行文件install.ps1
  2. Update package will uninstall the old version package including contents. 更新软件包将卸载包含内容的旧版本软件包。

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.

相关问题 iTextSharp仅在尚不存在时才添加水印 - iTextSharp Add Watermark Only If it Doesn't Already Exist 如何在没有任何nuspec文件的情况下将多个项目中的产品打包到一个nuget中? - How to pack the products from multiple projects into one nuget without any nuspec file? NuGet - NU5131:在 nuspec 文件中声明的引用没有被打包 - NuGet - NU5131: References declared in the nuspec file are not being packaged 仅当使用C#不存在文件时,如何将文件添加到文件夹 - How to Add file to the folder only if the file doesnt exist using C# tfs build - nuget 仅打包带有 nuspec 的项目 - tfs build - nuget package only projects with nuspec 如果现有XElement尚不存在,请将其添加到现有XElement中 - Add new XElement to an existing XElement if it doesn't already exist 如果创建文件不存在,如何创建目录? - How do I create directory if it doesn't exist to create a file? 从nuspec文件的不同文件夹中包含的多个csproj创建多目标nuget包 - Create multi-target nuget package from several csproj contained in different folders from nuspec file 当 nuget 打包程序集时,nuspec 文件中的 $version$ 令牌未从 AssemblyInfo.cs 读取 - $version$ token in nuspec file not reading from AssemblyInfo.cs when nuget packaging an assembly “版本”属性未在 nuspec 文件中声明 - The 'Version' attribute is not declared in nuspec file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM