Created
October 18, 2017 15:37
-
-
Save mrvux/4ef631224d32827dcee51f279bef05d7 to your computer and use it in GitHub Desktop.
Simple local nuget repository cheat sheet, create/add/list/search/install and uninstall packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple example to use a few nuget core functions to manage a local repository | |
* | |
* Example needs System.Windows.Forms reference, as well as Nuget.Core nuget reference. | |
* | |
*/ | |
using NuGet; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace Nuget.PackageSearchTest | |
{ | |
class Program | |
{ | |
[STAThread()] | |
static void Main(string[] args) | |
{ | |
string myLocalRepositoryPath = @"D:\\nuget_repo_test\\"; | |
string myPackageInstallFolder = @"D:\\nuget_install_test\\"; | |
/* If folder does not exist it will do nothing until you try to add something into it, | |
* it will only create directories when the first package gets added | |
*/ | |
IPackageRepository localRepository = PackageRepositoryFactory.Default.CreateRepository(myLocalRepositoryPath); | |
OpenFileDialog dlg = new OpenFileDialog(); | |
if (dlg.ShowDialog() == DialogResult.OK) | |
{ | |
string packagePath = dlg.FileName; | |
/*nupkg is set as zip package, create one and add to repository */ | |
ZipPackage zipPackage = new ZipPackage(packagePath); | |
localRepository.AddPackage(zipPackage); | |
} | |
//This allows to get all packages in your repository | |
IQueryable<IPackage> allPackagesQuery = localRepository.GetPackages(); | |
List<IPackage> allPackages = allPackagesQuery.ToList(); | |
//Display all packages info | |
Console.WriteLine("All packages"); | |
foreach (IPackage package in allPackages) | |
{ | |
Console.WriteLine(package.Id); | |
} | |
//Search for a package | |
string searchPackageID = "SharpDX"; | |
//If you need a specific version, pass null otherwise | |
VersionSpec version = new VersionSpec(new SemanticVersion(4, 0, 1, "", "")); | |
bool allowPreRelease = false; | |
bool allowUnlisted = false; | |
Console.WriteLine("All packages"); | |
List<IPackage> searchPackageResult = localRepository.FindPackages(searchPackageID, version, allowPreRelease, allowUnlisted).ToList(); | |
foreach (IPackage package in searchPackageResult) | |
{ | |
Console.WriteLine(package.Id); | |
} | |
bool ignoreDependencies = true; | |
//Install a package | |
PackageManager packageManager = new PackageManager(localRepository, myPackageInstallFolder); | |
packageManager.InstallPackage(searchPackageResult[0], ignoreDependencies, allowPreRelease); | |
//Uninstall a package (note, this obviously might fail is some dlls are in use) | |
packageManager.UninstallPackage(searchPackageResult[0]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment