有没有一种方法可以配置nuGet包,使得仅当项目中不存在该文件时才安装该文件?
具体来说,我的nuGet包含一个定制的配置文件。一旦安装,用户将对文件进行修改。问题是当用户安装我nuGet包的新版本时,配置文件会被替换,从而丢失他们的更改。我想要防止这种情况发生。
具体来说,我的nuGet包含一个定制的配置文件。一旦安装,用户将对文件进行修改。问题是当用户安装我nuGet包的新版本时,配置文件会被替换,从而丢失他们的更改。我想要防止这种情况发生。
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。然后,您可以使用任何存档管理器查看其中的内容。
可能是一个老问题,但我还是要说一下,因为我也遇到了这个问题:
可以通过手动更新软件包来解决此问题,使用命令行如 PowerShell 参考 中所示:
Update-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version <string>] [-Source <string>] [-Safe] [-IncludePrerelease] [-Reinstall] [-FileConflictAction] [-WhatIf]
你需要使用的参数是-FileConflictAction
,你必须将其设置为Ignore
。
编辑:问题是,这对于一个特定的文件不起作用,因此您可能需要考虑重命名包中包含的配置文件。