Is there a way to tell nuGet to install a file into the project only if the file is not already in the project?

Is there a way to configure the nuGet package so that the file is installed inside the project only if it doesn't already exist in the project?

Specifically, my nuGet package contains a custom config file. After installation, the user will make changes 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 from happening.

+3


source to share


2 answers


Suppose you are using Visual Studio and need to expand log4net.config if it is missing.

  • Create a folder "tools" in the folder with your * .nuspec file
  • Add to nuspec file:

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

It will include the files in the tools folder in the package

  • put the install.ps1 file in the tools folder 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."
    }
    
          



Create a package using (for example):

nuget pack PackageWithMyLogConfig.nuspec -Version 1.0.0

      

If you want to look inside the generated package, rename it to * .zip. Then you can preview what's inside using any archive manager.

+2


source


Probably an old question, but doesn't care here as I also had this problem:

This can be done by manually updating the packages using the command line, as shown in the PowerShell help :

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

      



The parameter you need to use is -FileConflictAction

, which you have to set to Ignore

.

Edit: The problem is that this doesn't work for only one specific file, so you might have to rename the config file included in the package.

0


source







All Articles