Click here to Skip to main content
15,849,768 members
Articles / All Topics

Installing a NuGet Package Programmatically

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
22 Sep 2014CPOL1 min read 11.1K   5   2
How to install a Nuget package programmatically

I've been struggling for some time to make NuGet work for my online .NET IDE project, Chpokk. The good news is that there turned out to be a command line tool, NuGet.exe, which can be used on a build server, so I though I'd just use its functionality. The bad news is that it turned out that it only downloaded packages, not modifying the project. Then the good news is that there's ProjectManager class, which can handle the required modifications to the project. And of course, there's the bad news -- while this guy downloads the packages, it doesn't copy the unzipped files to the package folder from the temporary location. Fortunately, the final news was good -- the ProjectManager class proved to be extensible enough to make it work. Although I haven't tested some advanced stuff, like ps scripts or config transforms, it works for most situations, and extending it would be pretty simple.

Here's the main part of the code:

C#
public void InstallPackage(string packageId, string projectPath, string targetFolder = null) {
	var packagePathResolver = new DefaultPackagePathResolver(targetFolder);
	var packagesFolderFileSystem = new PhysicalFileSystem(targetFolder);
	var projectSystem = new BetterThanMSBuildProjectSystem(projectPath) 
	{ Logger = _console };
	var localRepository = new BetterThanLocalPackageRepository
	(packagePathResolver, packagesFolderFileSystem, projectSystem);
	var projectManager = new ProjectManager
	(_packageRepository, packagePathResolver, projectSystem,
     localRepository) {Logger = _console};
 
	projectManager.PackageReferenceAdded += (sender, args) => args.Package.GetLibFiles()
    .Each(file => SaveAssemblyFile(args.InstallPath, file));
	projectManager.AddPackageReference(packageId);
	projectSystem.Save();
}

The most work is done by the ProjectManager class. I had to add three extensions though. First, in the PackageReferenceAdded handler, I make sure that the assembly files are saved to install path. Second, I extend the MSBuildProjectSystem class in order to add the content files to the project. Last, I'm subclassing the LocalPackageRepository class. This class is used, in particular, for checking whether a package is already installed. The implementation just checked that either a *.nuspec or *.nupkg file existed.

You can find a working project here. Download the project and build a console executable that, when run, installs the NUnit (or another, if you add an argument) package to the "testPackages" folder and adds a reference to the project.

Happy Nuggeting!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer GeekSoft
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestionchpokk.apphb.com first impressions Pin
Giorgio Arata25-Sep-14 23:07
professionalGiorgio Arata25-Sep-14 23:07 
GeneralRe: chpokk.apphb.com first impressions Pin
Artem Smirnov12-Dec-14 5:29
professionalArtem Smirnov12-Dec-14 5:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.