有没有办法告诉 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?

查看:13
本文介绍了有没有办法告诉 nuGet 将文件安装到项目中,前提是该文件尚未在项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法配置一个 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆