简体   繁体   中英

How to create a NuGet Package using NuGet.Core?

I would like to create a application which makes use of the NuGet Package NuGet.Core. It has a class called PackageBuilder that makes it possible. Is there any sample / tutorial / documentation available?

A really simple example:

  1. create a folder containing the files you want in your package.
  2. Write some code like this:

     ManifestMetadata metadata = new ManifestMetadata() { Authors = "mauvo", Version = "1.0.0.0", Id = "myPackageIdentifier", Description = "A description", }; PackageBuilder builder = new PackageBuilder(); builder.PopulateFiles("folderPath/", new[] {new ManifestFile() {Source = "**"}}); builder.Populate(metadata); using(FileStream stream = File.Open(packagePath, FileMode.OpenOrCreate)) { builder.Save(stream); } 

An improved example based on David's code. Changes:

  • All files in a folder except *.nuspec are added to package.
  • A row defining package file name.

     ManifestMetadata metadata = new ManifestMetadata() { Authors = "mauvo", Version = "1.0.0.0", Id = "myPackageIdentifier", Description = "A description", }; PackageBuilder builder = new PackageBuilder(); var files = Directory.GetFiles(packagePath, "*", SearchOption.AllDirectories) .Where(f => !f.EndsWith(".nuspec")) .Select(f => new ManifestFile { Source = f, Target = f.Replace(path, "") }) .ToList(); builder.PopulateFiles("", files); builder.Populate(metadata); string packageFile = Path.Combine(packagePath, builder.GetFullName()) + ".nupkg"; using(FileStream stream = File.Open(packageFile, FileMode.OpenOrCreate)) { builder.Save(stream); } 

I am not aware of any tutorial or documentation on how to build a NuGet package using NuGet.Core. Instead I would take a look at the source code for NuGet.exe. It has a pack command that builds a package from a .nuspec file. The PackCommand class , which uses the PackageBuilder class, should show you what you need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM