Reputation: 6002
Consider a large existing codebase with approx. 150+ solutions and 800+ C# projects. Many are unit tests written using NUnit. All those projects references "nunit.framework.dll" from a "lib" folder that is checked in. There is also a number of 3rd party assemblies in the "lib" folder which has corresponding NuGet packages.
I could manually open 150+ solutions and migrate each reference to to NuGet. However this is proving to be tedious and error prone. I wrote a C# console application to parse csproj files and identify which packages needs to be installed for the respective project. So I know that 300+ projects requires the NUnit package to be installed.
How to I programmatically automate the installation of a package in a solution, matching the exact same behavior as doing so manually within Visual Studio 2013? I looked everywhere, and only found an extension however, it doesn't perform a full install with dependencies etc.
Upvotes: 4
Views: 2945
Reputation: 2554
You can use the following snippet:
Get-Project -All | foreach-object {IF (Get-Content $_.FullName | Select-String 'Reference Include="XXX"') {Install-Package XXX -ProjectName $_.FullName}}
Replace XXX
with your desired package name and run the snippet in Tools->NuGet Package Manager->Package Manager Console
from within Visual Studio.
Upvotes: 3
Reputation: 2666
Create a packages.config
file with just an entry for the NUnit packages
package.config
should look something like this check for correct package name, version and target info
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nunit.framework" version="2.6.3" targetFramework="net45" requireReinstallation="true" />
</packages>
extend the utility you wrote to parse .csproj
files
to edit the csproj file as well and add the below tags
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
packages.config should be copied to all project folders; else if your projects are going to have the same reference you can choose to Add the packages.config as a link
<ItemGroup>
<None Include="$(SolutionDir)packages.config">
<Link>$(SolutionDir)packages.config</Link>
</None>
</ItemGroup>
Once this is done open the solution in visual studio and go to the NuGet console and enter the below command; NuGet will resolve missing dependencies and add them.
update-package
Upvotes: 3