Last active
December 14, 2016 14:04
-
-
Save nairdo/4531980 to your computer and use it in GitHub Desktop.
From the NuGet Package Explorer's MainWindow.xaml.cs and the PackageViewModel PackageFolder class...
This file contains hidden or 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
/// | |
/// Example building a package (from the NuGet PackageBuilderTest.cs class) | |
/// | |
// Act | |
var builder = new PackageBuilder { Id = "test", Version = new SemanticVersion("1.0"), Description = "test" }; | |
builder.Authors.Add("test"); | |
foreach (var name in fileNames) | |
{ | |
builder.Files.Add(CreatePackageFile(name)); | |
} | |
// Assert | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
builder.Save(stream); | |
var zipPackage = new ZipPackage(() => new MemoryStream(stream.ToArray()), enableCaching: false); | |
Assert.Equal(@"content\images\bread&butter.jpg", zipPackage.GetFiles().ElementAt(0).Path); | |
Assert.Equal(@"content\images\logo123?#78.png", zipPackage.GetFiles().ElementAt(1).Path); | |
Assert.Equal(@"lib\C#\test.dll", zipPackage.GetFiles().ElementAt(2).Path); | |
Assert.Equal(@"lib\name with spaces.dll", zipPackage.GetFiles().ElementAt(3).Path); | |
Assert.Equal(@"lib\regular.file.dll", zipPackage.GetFiles().ElementAt(4).Path); | |
} | |
/// | |
/// Example writing a .nuspec (manifest file)... | |
/// | |
string fullpath = yourPackageName + NuGet.Constants.ManifestExtension; | |
using (Stream fileStream = File.Create(fullpath)) | |
{ | |
Manifest manifest = Manifest.Create(PackageMetadata); | |
if (includeFilesSection) | |
{ | |
string tempPath = Path.GetTempPath(); | |
manifest.Files = new List<ManifestFile>(); | |
manifest.Files.AddRange(RootFolder.GetFiles().Select( | |
f => new ManifestFile | |
{ | |
Source = String.IsNullOrEmpty(f.OriginalPath) || f.OriginalPath.StartsWith(tempPath, StringComparison.OrdinalIgnoreCase) ? f.Path : PathUtility.RelativePathTo(rootPath, f.OriginalPath), | |
Target = f.Path | |
}) | |
); | |
} | |
manifest.Save(fileStream); | |
} | |
/// | |
// Opening a local package... | |
/// | |
private bool OpenLocalPackageCore(string packagePath) | |
{ | |
IPackage package = null; | |
try | |
{ | |
string extension = Path.GetExtension(packagePath); | |
if (extension.Equals(Constants.PackageExtension, StringComparison.OrdinalIgnoreCase)) | |
{ | |
package = new ZipPackage(packagePath); | |
} | |
else if (extension.Equals(Constants.ManifestExtension, StringComparison.OrdinalIgnoreCase)) | |
{ | |
var builder = new PackageBuilder(packagePath); | |
package = builder.Build(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
UIServices.Show(ex.Message, MessageLevel.Error); | |
return false; | |
} | |
if (package != null) | |
{ | |
LoadPackage(package, packagePath, PackageType.LocalPackage); | |
return true; | |
} | |
return false; | |
} | |
/// | |
/// adding a file to get a PackageFile... | |
/// | |
public PackageFile AddFile(string filePath, bool isTempFile) | |
{ | |
if (!File.Exists(filePath)) | |
{ | |
throw new ArgumentException("File does not exist.", "filePath"); | |
} | |
string newFileName = System.IO.Path.GetFileName(filePath); | |
if (ContainsFolder(newFileName)) | |
{ | |
PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, MessageLevel.Error); | |
return null; | |
} | |
bool showingRemovedFile = false; | |
if (ContainsFile(newFileName)) | |
{ | |
bool confirmed = PackageViewModel.UIServices.Confirm( | |
Resources.ConfirmToReplaceExsitingFile_Title, | |
String.Format(CultureInfo.CurrentCulture, Resources.ConfirmToReplaceExsitingFile, newFileName), | |
isWarning: true); | |
if (confirmed) | |
{ | |
var part = this[newFileName] as PackageFile; | |
showingRemovedFile = PackageViewModel.IsShowingFileContent(part); | |
// remove the existing file before adding the new one | |
RemoveChildByName(newFileName); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
string newTargetPath = this.Path + "\\" + newFileName; | |
var physicalFile = new PhysicalPackageFile(isTempFile, filePath, newTargetPath); | |
var newFile = new PackageFile(physicalFile, newFileName, this); | |
Children.Add(newFile); | |
newFile.IsSelected = true; | |
IsExpanded = true; | |
PackageViewModel.NotifyChanges(); | |
if (showingRemovedFile) | |
{ | |
PackageViewModel.ShowFileContent(newFile); | |
} | |
return newFile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment