Scripting .NET project migration to Automatic NuGet Package Restore

Spread the love

Background on NuGet Package Restore and Automatic Package Restore
NuGet Package Restore allows you to reference NuGet packages in your project without shipping them with your source code or committing them to source control. The general idea is that the packages are restored – that is, downloaded and installed – into your project when it is build. This offers a number of benefits, including better interaction with source control and smaller code distributions.
NuGet Package Restore was originally implemented using an MSBuild task, so whenever you ran a build in Visual Studio the build step would handle the package restore before continuing with the next steps in the build process. This worked, but had a few downsides. For one, it required MSBuild to work, which conflicts with cross-platform development, build servers, etc. Another issue is that it required a separate .nuget directory be added to your Visual Studio solution, including a copy of NuGet.exe and a targets file.
Migrating Automatic Package Restore
NuGet 2.7 (and later) added support for Automatic NuGet Package Restore without requiring MSBuild. For new projects, this all just automatically works. However, if you had an older project for which you’d manually configured NuGet Package Restore, there is a manual process to be followed:
Remove the .nuget folder from your solution. Make sure the folder itself is also removed from the solution workspace.
Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file. To do so, search for Nuget.targets and remove the entire line where it is referenced. This is really easy – it just takes seconds to do on one project. However, if you’ve got a lot of projects, it’s painful busywork. I was making some updates on the Web Camps Training Kit which has a lot of projects (several demos and hands on labs, each with begin and end state solutions). I wanted a recursive script that dug through all the subfolders and fixed up my projects for me automatically. I asked around on Twitter and heard about IFix, from Terje Sandstrom. It’s a nice solution that handles this problem, but I wanted a recursive script, and if I was going to automate IFix with a recursive script I might as well just write a script that handled everything.
Enough Talk, Show Me The Script
Warning! This script edits all projects and deletes all .nuget and packages directories recursively for the specified path. That means if you ran it from C: it would modify every project on your C drive. Obviously, be careful about the current directory and make sure your affected projects are under source control.
#this automates the steps found here:
#yes, you could code golf this to about 6 characters if you wanted to

$find= @’


foreach ($f in Get-ChildItem -Recurse -Path “.” -Filter *.csproj | sort-object)
$text = Get-Content $f.FullName -Raw
$text.Replace($find, “”) | set-content $f.FullName

Get-ChildItem . -include .nuget,packages -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
Here’s what it does:
Recursively search for .csproj files in a path
Remove the MSBuild target for NuGet package restore
Recurse through the directory structure, removing .nuget and packages directories A few admissions / excuses
Yes, it could be more terse. Get-ChildItem could be gci, Set-Content could be sc, all the switches could use the single letter equivalents. I used to be embarrassed posting verbose PowerShell, but I’ve decided that this is a lot easier to read and there’s no real point to minifying PowerShell.
Yes, this is just doing a simple string replace. I looked at doing things the XML way (XPath or DOM manipulations) or using regular expressions, but decided that I want this to be completely explicit. I don’t want to match any lines unless they’re exactly matching what was written by Visual Studio when I enabled package restore. Because of that, I only want an exact string match. I think that this is a little faster since it’s just a string replace, but I didn’t test that and don’t really care if it isn’t.
Yes, this leaves a blank line in the .csproj file. That makes no difference in the way Visual Studio handles it and it was easier than worrying about it.
Yes, that $find declaration is weird. The string I’m searching for () contains a lot of characters that need to be escaped in PowerShell, and even more if doing a Regex replace in PowerShell (which I tested a bit). So instead I used a here-string, which is kind of like a C# verbatim string literal (e.g. @\serversharefile.txt).
For more information on why you should migrate away from MSBuild based Automatic Package Restore, see these posts:
Migrate away from MSBuild-based NuGet package restore (Xavier Decoster)
The right way to restore NuGet packages (David Ebbo)

X ITM Cloud News


Next Post

Snipping Tool on Vista

Mon Nov 25 , 2019
Spread the love          In a comment on my post about problems running Cropper on Vista, Chris Hammond pointed out the Snipping Tool that’s built into Vista (Programs, Accessories. I wasn’t aware of this tool – it’s really quite cool! It allows you to draw a rectangle or free form shape on […]

Cloud Computing – Consultancy – Development – Hosting – APIs – Legacy Systems

X-ITM Technology helps our customers across the entire enterprise technology stack with differentiated industry solutions. We modernize IT, optimize data architectures, and make everything secure, scalable and orchestrated across public, private and hybrid clouds.

This image has an empty alt attribute; its file name is x-itmdc.jpg

The enterprise technology stack includes ITO; Cloud and Security Services; Applications and Industry IP; Data, Analytics and Engineering Services; and Advisory.

Watch an animation of  X-ITM‘s Enterprise Technology Stack

We combine years of experience running mission-critical systems with the latest digital innovations to deliver better business outcomes and new levels of performance, competitiveness and experiences for our customers and their stakeholders.

X-ITM invests in three key drivers of growth: People, Customers and Operational Execution.

The company’s global scale, talent and innovation platforms serve 6,000 private and public-sector clients in 70 countries.

X-ITM’s extensive partner network helps drive collaboration and leverage technology independence. The company has established more than 200 industry-leading global Partner Network relationships, including 15 strategic partners: Amazon Web Services, AT&T, Dell Technologies, Google Cloud, HCL, HP, HPE, IBM, Micro Focus, Microsoft, Oracle, PwC, SAP, ServiceNow and VMware