仅当文件尚未在项目中时,是否可以告诉nuGet将文件安装到项目中? [英] Is there a way to tell nuGet to install a file, into a project, only if the file is not already in the project?
问题描述
是否有一种配置nuGet软件包的方法,以便仅在项目中不存在文件的情况下,才在项目内部安装文件?
Is there a way to configure a nuGet package so that a file is installed, inside a project, only if it does not already exist in the project?
具体地说,我的nuGet软件包包含一个自定义配置文件.安装后,用户将对文件进行修改.问题在于,当用户安装新版本的nuGet软件包时,配置文件将被替换-从而丢失了所做的更改.我想防止这种情况.
Specifically, my nuGet package contains a custom config file. Once installed, the user will make modifications to the file. The problem is that the config file gets replaced when the user installs a new version of my nuGet package -- thus, losing their changes. I want to prevent this.
推荐答案
让我们假设您正在使用Visual Studio,并且如果缺少log4net.config,则需要对其进行部署.
Let's assume that you are using Visual Studio and need to deploy log4net.config if it is missing.
- 使用* .nuspec文件在文件夹下创建工具"文件夹
-
添加到nuspec文件:
- Create the "tools" folder under the folder with your *.nuspec file
Add to the nuspec file:
<file src="tools/**/*.*" target="\" />
</files>
</package>
它将工具文件夹中的文件包含在软件包中
It will include the files in the tools folder into the package
-
在工具文件夹中放置文件install.ps1,其代码类似于:
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.然后,您可以使用任何存档管理器查看内部内容.
If you want to look inside the generated package, rename it to *.zip. Then you can view what's inside using any archive manager.
这篇关于仅当文件尚未在项目中时,是否可以告诉nuGet将文件安装到项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!