本文整理汇总了C#中NuGet.PackageBuilder.Save方法的典型用法代码示例。如果您正苦于以下问题:C# PackageBuilder.Save方法的具体用法?C# PackageBuilder.Save怎么用?C# PackageBuilder.Save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NuGet.PackageBuilder
的用法示例。
在下文中一共展示了PackageBuilder.Save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SavePackage
public static void SavePackage(IPackageMetadata packageMetadata, IEnumerable<IPackageFile> files, string targetFilePath, bool useTempFile)
{
var builder = new PackageBuilder();
// set metadata
CopyMetadata(packageMetadata, builder);
// add files
builder.Files.AddRange(files);
// create package in the temprary file first in case the operation fails which would
// override existing file with a 0-byte file.
string fileNameToUse = useTempFile ? Path.GetTempFileName() : targetFilePath;
try {
using (Stream stream = File.Create(fileNameToUse)) {
builder.Save(stream);
}
if (useTempFile) {
File.Copy(fileNameToUse, targetFilePath, true);
}
}
finally {
try {
if (useTempFile && File.Exists(fileNameToUse)) {
File.Delete(fileNameToUse);
}
}
catch {
// don't care if this fails
}
}
}
示例2: Main
static void Main(string[] args)
{
Console.WriteLine("Create NuGet Package via Code");
ManifestMetadata metadata = new ManifestMetadata()
{
Authors = "Authors Name",
Version = "1.0.0.0",
Id = "NuGetId",
Description = "NuGet Package Description goes here!",
};
PackageBuilder builder = new PackageBuilder();
var path = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\DemoContent\\";
builder.PopulateFiles(path, new[] { new ManifestFile { Source = "**", Target = "content" } });
builder.Populate(metadata);
using (FileStream stream = File.Open("test.nupkg", FileMode.OpenOrCreate))
{
builder.Save(stream);
}
Console.WriteLine("... and extract NuGet Package via Code");
NuGet.ZipPackage package = new ZipPackage("test.nupkg");
var content = package.GetContentFiles();
Console.WriteLine("Package Id: " + package.Id);
Console.WriteLine("Content-Files-Count: " + content.Count());
Console.ReadLine();
}
示例3: CreateNugetPackage
public virtual void CreateNugetPackage([NotNull] string tempDirectory, [NotNull] string fileName, [NotNull] string sourceFileName)
{
var packageId = Path.GetFileNameWithoutExtension(fileName);
var sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\"?>");
sb.AppendLine("<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">");
sb.AppendLine(" <metadata>");
sb.AppendLine(" <id>" + packageId + "</id>");
sb.AppendLine(" <title>" + packageId + "</title>");
sb.AppendLine(" <version>1.0.0</version>");
sb.AppendLine(" <authors>Sitecore Pathfinder</authors>");
sb.AppendLine(" <requireLicenseAcceptance>false</requireLicenseAcceptance>");
sb.AppendLine(" <description>Generated by Sitecore Pathfinder</description>");
sb.AppendLine(" </metadata>");
sb.AppendLine(" <files>");
sb.AppendLine(" <file src=\"" + sourceFileName + "\" target=\"content\\sitecore.project\\exports.xml\" />");
sb.AppendLine(" </files>");
sb.AppendLine("</package>");
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())))
{
var packageBuilder = new PackageBuilder(stream, tempDirectory);
using (var nupkg = new FileStream(fileName, FileMode.Create))
{
packageBuilder.Save(nupkg);
}
}
}
示例4: BuildNuGetPackage
public void BuildNuGetPackage(NuGetPackSpecification packConfiguration)
{
var packageBuilder = new PackageBuilder(packConfiguration.NuSpec, NullPropertyProvider.Instance, true);
var packagePath = Path.Combine(packConfiguration.OutputDirectory, GetDefaultPackagePath(packageBuilder));
var packageStream = File.Create(packagePath);
packageBuilder.Save(packageStream);
}
示例5: CreateTestPackage
/// <summary>
/// Creates a test package. - Take from NuGet.Core.Tests
/// </summary>
/// <param name="packageId">The id of the created package.</param>
/// <param name="version">The version of the created package.</param>
/// <param name="path">The directory where the package is created.</param>
/// <returns>The full path of the created package file.</returns>
public static string CreateTestPackage(string packageId, string version, string path, Uri licenseUrl = null)
{
var packageBuilder = new PackageBuilder
{
Id = packageId,
Version = new SemanticVersion(version)
};
packageBuilder.Description = string.Format(
CultureInfo.InvariantCulture,
"desc of {0} {1}",
packageId, version);
if (licenseUrl != null)
{
packageBuilder.LicenseUrl = licenseUrl;
}
packageBuilder.Files.Add(CreatePackageFile(@"content\test1.txt"));
packageBuilder.Authors.Add("test author");
var packageFileName = string.Format("{0}.{1}.nupkg", packageId, version);
var packageFileFullPath = Path.Combine(path, packageFileName);
using (var fileStream = File.Create(packageFileFullPath))
{
packageBuilder.Save(fileStream);
}
return packageFileFullPath;
}
示例6: BuildPackage
public void BuildPackage(string baseUrl, ManifestMetadata metadata, ManifestFile[] files)
{
NuGet.PackageBuilder packageBuilder = new NuGet.PackageBuilder();
packageBuilder.Populate(metadata);
packageBuilder.PopulateFiles(baseUrl, files);
var saveDir = Path.Combine(DEFAULT_PACKAGES_SAVE_PATH, packageBuilder.Id, packageBuilder.Version.ToString().Replace('.', '_'));
Directory.CreateDirectory(saveDir);
var saveStream = File.Create(Path.Combine(saveDir, packageBuilder.Id + ".nupkg"));
packageBuilder.Save(saveStream);
}
示例7: ExecuteBuildNuGetPackages
private void ExecuteBuildNuGetPackages(BuildConfiguration configuration)
{
foreach (var buildNuGetPackage in configuration.BuildNuGetPackages)
{
try
{
var properties = new PropertyProvider();
string version = buildNuGetPackage.Version;
if (version == null)
version = VersionRetriever.GetVersion(TargetPath);
properties.Properties["version"] = version;
string manifest = TranslatePath(buildNuGetPackage.Manifest);
var builder = new PackageBuilder(
manifest,
buildNuGetPackage.BasePath ?? TargetDir,
properties,
false
);
string target =
buildNuGetPackage.Target != null
? TranslatePath(buildNuGetPackage.Target)
: Path.Combine(TargetDir, Path.GetFileNameWithoutExtension(manifest) + "." + version + ".nupkg");
bool isExistingPackage = File.Exists(target);
try
{
using (Stream stream = File.Create(target))
{
builder.Save(stream);
}
}
catch
{
if (!isExistingPackage && File.Exists(target))
File.Delete(target);
throw;
}
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
}
}
}
示例8: Main
static void Main()
{
Console.Title = "Nuget Packer";
Console.WriteLine("Prepare...");
string packages = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Packages");
if (Directory.Exists(packages))
Directory.Delete(packages, true);
Directory.CreateDirectory(packages);
Console.WriteLine();
foreach (var directory in Directory.EnumerateDirectories(AppDomain.CurrentDomain.BaseDirectory))
{
try
{
Console.Write("Packing... ");
if(directory.EndsWith("Packages")) continue;
string name = Path.GetFileName(directory) + ".nuspec";
var pack = new PackageBuilder(Directory.EnumerateFiles(directory).First(s => s.EndsWith(name)),
directory, null, true);
Console.Write(pack.Id + "... ");
using (var stream = new FileStream(Path.Combine(packages, pack.Id + ".nupkg"), FileMode.Create))
{
pack.Save(stream);
}
Directory.Delete(directory, true);
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine();
Console.Write("Error: ");
Console.WriteLine(e);
Console.WriteLine("Press enter.");
Console.ReadKey();
}
}
Console.WriteLine();
Console.WriteLine("Finished");
Console.WriteLine("Press Enter...");
Console.ReadKey();
}
示例9: Pack
private void Pack([NotNull] IBuildContext context, [NotNull] string zipFileName)
{
var packageName = Path.GetFileNameWithoutExtension(zipFileName);
var packageId = packageName.GetSafeCodeIdentifier();
var srcFileName = PathHelper.UnmapPath(context.ProjectDirectory, zipFileName);
var targetFileName = "content\\packages\\" + Path.GetFileName(zipFileName);
context.Trace.TraceInformation(Msg.M1016, Texts.Packing, packageName);
var nuspec = new StringWriter();
nuspec.WriteLine("<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">");
nuspec.WriteLine(" <metadata>");
nuspec.WriteLine(" <id>" + packageId + "</id>");
nuspec.WriteLine(" <title>" + packageName + "</title>");
nuspec.WriteLine(" <version>1.0.0</version>");
nuspec.WriteLine(" <authors>Sitecore Pathfinder</authors>");
nuspec.WriteLine(" <requireLicenseAcceptance>false</requireLicenseAcceptance>");
nuspec.WriteLine(" <description>Generated by Sitecore Pathfinder</description>");
nuspec.WriteLine(" </metadata>");
nuspec.WriteLine(" <files>");
nuspec.WriteLine(" <file src=\"" + srcFileName + "\" target=\"" + targetFileName + "\"/>");
nuspec.WriteLine(" </files>");
nuspec.WriteLine("</package>");
var nupkgFileName = Path.Combine(Path.GetDirectoryName(zipFileName) ?? string.Empty, packageId + ".nupkg");
try
{
var byteArray = Encoding.UTF8.GetBytes(nuspec.ToString());
using (var nuspecStream = new MemoryStream(byteArray))
{
var packageBuilder = new PackageBuilder(nuspecStream, context.ProjectDirectory);
using (var nupkg = new FileStream(nupkgFileName, FileMode.Create))
{
packageBuilder.Save(nupkg);
}
}
}
catch (Exception ex)
{
context.Trace.TraceError(Msg.M1003, Texts.Failed_to_create_the_Nupkg_file, ex.Message);
}
}
示例10: CreateTestPackageStream
private static Stream CreateTestPackageStream()
{
var packageStream = new MemoryStream();
var builder = new PackageBuilder
{
Id = "TestPackage",
Version = SemanticVersion.Parse("0.0.0.1"),
Description = "Trivial Description",
Authors = { "AuthorsIsRequiredSayWhaat?" },
};
var file = new Mock<IPackageFile>();
file.Setup(s => s.Path).Returns(@"content\HelloWorld.cs");
file.Setup(s => s.GetStream()).Returns(Stream.Null);
builder.Files.Add(file.Object);
builder.Save(packageStream);
return packageStream;
}
示例11: BuildPackage
public void BuildPackage(string basePath, IList<string> includes, ManifestMetadata metadata, string outFolder, bool overwrite)
{
var package = new PackageBuilder();
package.PopulateFiles(basePath, includes.Select(i => new ManifestFile { Source = i }));
package.Populate(metadata);
var filename = metadata.Id + "." + metadata.Version + ".nupkg";
var output = Path.Combine(outFolder, filename);
if (fileSystem.FileExists(output) && !overwrite)
throw new CommandException("The package file already exists and --overwrite was not specified");
log.InfoFormat("Saving {0} to {1}...", filename, outFolder);
fileSystem.EnsureDirectoryExists(outFolder);
using (var outStream = fileSystem.OpenFile(output, FileMode.Create))
package.Save(outStream);
}
示例12: CreateLocalNupkgFile
/// <summary>
/// Creates a local nupkg file, and stores it in a local folder (<see cref="LocalNuGetPackageFolder"/>).
/// </summary>
/// <returns>The path to the nupkg file</returns>
public static string CreateLocalNupkgFile(string nuspecFilePath, string directory)
{
var packageName = nuspecFilePath.Substring(nuspecFilePath.LastIndexOf(Path.DirectorySeparatorChar) + 1)
.Replace(".nuspec", ".nupkg");
var nupkg = Path.Combine(LocalNuGetPackageFolder, packageName);
// read the nuspec metadata file
using (var stream = new FileStream(nuspecFilePath, FileMode.Open, FileAccess.Read))
{
TraceSource.TraceEvent(TraceEventType.Verbose, "Creating {0}", packageName);
// create the package and save it to disk
var builder = new PackageBuilder(stream, directory);
using (FileStream nupkgStream = File.Open(nupkg, FileMode.Create))
{
builder.Save(nupkgStream);
}
TraceSource.TraceEvent(TraceEventType.Verbose, "{0} was created", nupkg);
}
return nupkg;
}
示例13: BuildNupkgFile
protected virtual void BuildNupkgFile([NotNull] IBuildContext context, [NotNull] string nuspecFileName, [NotNull] string nupkgFileName)
{
var configFileName = Path.Combine(context.ToolsDirectory, context.Configuration.GetString(Constants.Configuration.ProjectConfigFileName));
var nuspec = context.FileSystem.ReadAllText(nuspecFileName);
nuspec = nuspec.Replace("$global.scconfig.json$", configFileName);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(nuspec));
try
{
var packageBuilder = new PackageBuilder(stream, context.ProjectDirectory);
using (var nupkg = new FileStream(nupkgFileName, FileMode.Create))
{
packageBuilder.Save(nupkg);
}
}
catch (Exception ex)
{
context.Trace.TraceError(Msg.D1020, Texts.Failed_to_create_the_Nupkg_file, ex.Message);
}
}
示例14: ManifestMetadata
IPackage IPackageFactory.CreateFromProject(string nupecFile, string csprojFile, string buildConfiguration, bool includeBinOutput)
{
var projectReader = _projectReaderFactory.Create(csprojFile);
var binDir = projectReader.GetBinPath(buildConfiguration);
var assemblyName = projectReader.GetAssemblyName();
var assemblyPath = _fileSystem.CombinePaths(binDir, assemblyName);
var assemblyReader = _assemblyReaderFactory.Create(assemblyPath);
var manifest = new ManifestMetadata()
{
Id = assemblyReader.GetPackageId(),
Title = assemblyReader.GetPackageTitle(),
Owners = assemblyReader.GetCompany(),
Authors = assemblyReader.GetCompany(),
Description = assemblyReader.GetDescription(),
Copyright = assemblyReader.GetCopyright(),
Version = assemblyReader.GetFileVersion()
};
var files = new List<ManifestFile>();
foreach (var dll in _fileSystem.FindFiles(binDir, "*.dll", false))
files.Add(new ManifestFile() { Source = dll, Target = @"lib\net40" });
var packageBuilder = new PackageBuilder();
packageBuilder.Populate(manifest);
packageBuilder.PopulateFiles(string.Empty, files);
var projDir = _fileSystem.GetDirectory(csprojFile);
var packagefile = _fileSystem.CombinePaths(projDir, "packages.config");
var packagePath = _fileSystem.ChangeFileExtension(csprojFile, "nupkg");
using (var stream = _fileSystem.OpenWrite(packagePath))
{
packageBuilder.Save(stream);
}
return new ZipPackage(packagePath);
}
示例15: BuildPackage
public static IPackage BuildPackage(PackageBuilder builder, IFileSystem fileSystem, string outputPath = null)
{
ExcludeFiles(builder.Files);
// Track if the package file was already present on disk
bool isExistingPackage = fileSystem.file_exists(outputPath);
try
{
using (Stream stream = fileSystem.create_file(outputPath))
{
builder.Save(stream);
}
}
catch
{
if (!isExistingPackage && fileSystem.file_exists(outputPath))
{
fileSystem.delete_file(outputPath);
}
throw;
}
return new OptimizedZipPackage(outputPath);
}