Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nuget overwrite files

Is it possible to force nuGet packages to overwrite existing files? I need a nice way of applying patches (files, dll, resources) across 3 similar projects and through that this could be a nice solution.

like image 203
keeney Avatar asked Feb 01 '12 17:02

keeney


1 Answers

It can be accomplished with powershell and a package dependencies...

I have a NuGet package that I create for ASP.NET MVC it has all the files that I want to add to the standard Mvc project (DisplayTemplates, EditorTemplate, Extensions, BaseController, etc). Let say its called "Company.WebMvc"

I created another NuGet package called "Company.WebMvc-Prep" That has an Install.ps1 script that deletes anything I don't want from the standard Mvc project. Here is an code example of deleting the existing Scripts files...

param($installPath, $toolsPath, $package, $project)

#Remove Script Files

$scripts = $project.ProjectItems | Where-Object { $_.Name -eq "Scripts" }

if ($scripts) {

    $scripts.ProjectItems | ForEach-Object { $_.Delete() }

}

I then make "Company.WebMvc-Prep" a dependency of "Company.WebMvc". Why? because Install.ps1 (normally) is run after NuGet adds the files from the content folder. I need it to delete the files first then add the new ones. So when you call...

Install-Package Company.WebMvc

Company.WebMvc-Prep is installed first because it is a dependency (runs Install.ps1)

Then Company.WebMvc is installed which adds your content.

Long and short of it is... You Can overwrite existing content on your project via a NuGet package.

P.S. Remember you can also you partial classes to isolate NuGet package code vs. project specific code.

like image 125
bytebender Avatar answered Sep 30 '22 17:09

bytebender