有没有一种方法可以告诉NuGet仅在项目中不存在文件的情况下将文件安装到项目中?(此为提问标题)

4
有没有一种方法可以配置nuGet包,使得仅当项目中不存在该文件时才安装该文件?
具体来说,我的nuGet包含一个定制的配置文件。一旦安装,用户将对文件进行修改。问题是当用户安装我nuGet包的新版本时,配置文件会被替换,从而丢失他们的更改。我想要防止这种情况发生。
2个回答

4
假设您正在使用Visual Studio,并且需要在缺少log4net.config文件时进行部署。
  • Create the "tools" folder under the folder with your *.nuspec file
  • Add to the nuspec file:

         <file src="tools/**/*.*" target="\" /> 
      </files>
    </package>
    

它将把工具文件夹中的文件包含在软件包中

  • place inside the tools folder the file install.ps1 with some code similar to:

    param($installPath, $toolsPath, $package, $project)
    
    $log4netInProjectFolder = $project.ProjectItems | Where-Object { $_.Properties.Item("Filename").Value -eq "log4net.config" }
    
    if ($log4netInProjectFolder -eq $null)
    {
        Write-Host "File with path $deployTarget doesn't exist, deploying default log4net.config"
    
        foreach($item in $filesToMove) 
        {
            Write-Host "Moving " $item.Properties.Item("FullPath").Value
            (Get-Interface $project.ProjectItems "EnvDTE.ProjectItems").AddFromFileCopy($item.Properties.Item("FullPath").Value)
            (Get-Interface $item "EnvDTE.ProjectItem").Delete()
        }   
     }
     else
    {
        Write-Host "File $deployTarget already exists, preserving existing file."
    }
    
使用以下命令创建包(例如):
nuget pack PackageWithMyLogConfig.nuspec -Version 1.0.0

如果您想查看生成的软件包内部内容,请将其重命名为*.zip。然后,您可以使用任何存档管理器查看其中的内容。


1
$filesToMove 是什么? - Quintonn

0

可能是一个老问题,但我还是要说一下,因为我也遇到了这个问题:

可以通过手动更新软件包来解决此问题,使用命令行如 PowerShell 参考 中所示:

Update-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version <string>] [-Source <string>] [-Safe] [-IncludePrerelease] [-Reinstall] [-FileConflictAction] [-WhatIf]    

你需要使用的参数是-FileConflictAction,你必须将其设置为Ignore

编辑:问题是,这对于一个特定的文件不起作用,因此您可能需要考虑重命名包中包含的配置文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接