Click here to Skip to main content
15,849,543 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:

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));

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!


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 first impressions Pin
Giorgio Arata25-Sep-14 23:07
professionalGiorgio Arata25-Sep-14 23:07 
Hallo Artem Smirnov,

I had chance to see Chpokk in action this morning, tried to create a new online C#, just to play around with some C# code, and found available very interesting. For a start, I logged in with my LinkedIn profile and tried to click here and there to see what happen.

I also clicked on Run Tests button and saw what the logging window spit out. And my eyes fell on the fact that among others useful data, the LOG does contain a bunch of complete probably real physical paths (ie, "D:\Users\apphb3c86fc44b17507\app\_PublishedWebsites\ChpokkWeb\UserFiles\Giorgio Arata_LinkedIn\Repositories\NRapidXml\NRapidXml\bin\Debug\NRapidXml.dll") giving to a potential malicious user a concrete idea about your backend architecture.
Is this from your point of view a probable trampoline for security "breach", where your site's data may illegally accessed and/or compromised?

Thanks for your time.

Best regards, Giorgio Arata.

PS: Is the mentioned project open for collaboration and contributes?
GeneralRe: 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.