Skip to content

Commit

Permalink
comments and a test
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusongit committed Feb 1, 2024
1 parent f227a10 commit 157fff7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ internal void AddChildren(PackageItemRootViewModel item)
if (this.ChildItems.Contains(item)) return;
this.ChildItems.Add(item);
}

/// <summary>
/// The methods is used for adding a child item to all the encountered parent folders in a nested path
/// and make sure all the intermediate file paths are created as separte PackageItemRootViewModel.
/// For example if we have a path like "\dir1\dir2\dir3" and we want to add a child item to "dir1", the method will
/// add "dir 3" to "dir2" and then "dir2" to "dir1".
/// </summary>
/// <param name="elem">Child item to be added.</param>
internal void AddChild(PackageItemRootViewModel elem)
{
if (elem.DependencyType.Equals(DependencyType.CustomNode)) return;
Expand All @@ -149,7 +155,7 @@ internal void AddChild(PackageItemRootViewModel elem)

while (di.Parent != null)
{
// if we already have a subfodler item with that name,
// if we already have a subfolder item with that name,
// add this element's children to its children instead of creating a new subfolder branch
if(existingSubFolders.Keys.Contains(elem.DirectoryName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,9 @@ public bool RetainFolderStructureOverride
}
}
}
/// <summary>
/// The root directory of the package
/// </summary>
private string CurrentPackageDirectory { get; set; }
private static MetadataLoadContext sharedMetaDataLoadContext = null;
/// <summary>
Expand Down Expand Up @@ -990,17 +993,14 @@ private void RefreshPackageContents()
var itemsToAdd = CustomNodeDefinitions
.Select(def => new PackageItemRootViewModel(def))
.Concat(Assemblies.Select((pa) => new PackageItemRootViewModel(pa)))
.Concat(AdditionalFiles.Select((s) => new PackageItemRootViewModel(new FileInfo(s))))
.Concat(AdditionalFiles.Where(x => !x.ToLower().EndsWith("pkg.json")).Select((s) => new PackageItemRootViewModel(new FileInfo(s))))
.Concat(CustomDyfFilepaths.Select((s) => new PackageItemRootViewModel((string)s.Key, (string)s.Value)))
.ToList()
.ToObservableCollection();

var items = new Dictionary<string, PackageItemRootViewModel>();
if (!String.IsNullOrEmpty(this.CurrentPackageDirectory))
{
var v = 1;
}
if(!String.IsNullOrEmpty(RootFolder))

if (!String.IsNullOrEmpty(RootFolder))
{
var root = new PackageItemRootViewModel(RootFolder);
items[RootFolder] = root;
Expand Down Expand Up @@ -1073,18 +1073,24 @@ private List<PackageItemRootViewModel> BindParentToChild(Dictionary<string, Pack
return updatedItems;
}

/// <summary>
/// Gets the list PackageItemRootViewModel items which will be at the root directory of the package with all the child items.
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
private List<PackageItemRootViewModel> GetRootItems(Dictionary<string, PackageItemRootViewModel> items)
{
var rootItems = items.Values.Where(x => !x.isChild).ToList();
if (!rootItems.Any()) return rootItems;
var packageSourceDir = CurrentPackageDirectory ??= GetLongestCommonPrefix(items.Keys.ToArray());

var root = new PackageItemRootViewModel(CurrentPackageDirectory);
var root = new PackageItemRootViewModel(packageSourceDir);
var updatedItems = new List<PackageItemRootViewModel>();
//check each root item and create any missing connections
foreach (var item in rootItems)
{
var itemDir = new DirectoryInfo(item.DirectoryName);
if (!itemDir.Parent.FullName.Equals(CurrentPackageDirectory))
if (!itemDir.Parent.FullName.Equals(packageSourceDir))
{
root.AddChild(item);
}
Expand All @@ -1098,7 +1104,6 @@ private List<PackageItemRootViewModel> GetRootItems(Dictionary<string, PackageIt

/// <summary>
/// Test if path2 is subpath of path1
/// If it is, make sure all the intermediate file paths are created as separte PackageItemRootViewModel
/// </summary>
/// <param name="path1"></param>
/// <param name="path2"></param>
Expand All @@ -1124,6 +1129,27 @@ private bool IsSubPathOfDeep(PackageItemRootViewModel path1, PackageItemRootView
return false;
}

/// <summary>
/// Utility method to get the common file path, this may fail for files with the same partial name.
/// </summary>
/// <param name="s">A collection of filepaths</param>
/// <returns></returns>
internal string GetLongestCommonPrefix(string[] s)
{
int k = s[0].Length;
for (int i = 1; i < s.Length; i++)
{
k = Math.Min(k, s[i].Length);
for (int j = 0; j < k; j++)
if (s[i][j] != s[0][j])
{
k = j;
break;
}
}
return Path.GetDirectoryName(s[0].Substring(0, k));
}

/// <summary>
/// Return a list of HostComboboxEntry describing known hosts from PM.
/// Return an empty list if PM is down.
Expand Down Expand Up @@ -2262,6 +2288,10 @@ private void PublishLocally()
var remapper = new CustomNodePathRemapper(DynamoViewModel.Model.CustomNodeManager,
DynamoModel.IsTestMode);
var builder = new PackageDirectoryBuilder(new MutatingFileSystem(), remapper);
if (string.IsNullOrEmpty(Package.RootDirectory))
{
Package.RootDirectory = CurrentPackageDirectory;
}
builder.BuildRetainDirectory(Package, publishPath, updatedFiles, MarkdownFiles);
UploadState = PackageUploadHandle.State.Uploaded;
}
Expand Down
8 changes: 4 additions & 4 deletions src/DynamoPackages/PackageDirectoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public IDirectoryInfo BuildRetainDirectory(Package package, string packagesDirec

var rootPath = Path.Combine(packagesDirectory, package.Name);
var rootDir = fileSystem.TryCreateDirectory(rootPath);
var oldDir = package.RootDirectory;
var sourcePackageDir = package.RootDirectory;
package.RootDirectory = rootDir.FullName;

var dyfFiles = new List<string>();

RemoveUnselectedFiles(contentFiles.SelectMany(files => files).ToList(), rootDir);
CopyFilesIntoRetainedPackageDirectory(contentFiles, markdownFiles, oldDir, rootDir, out dyfFiles);
CopyFilesIntoRetainedPackageDirectory(contentFiles, markdownFiles, sourcePackageDir, rootDir, out dyfFiles);
RemoveRetainDyfFiles(contentFiles.SelectMany(files => files).ToList(), dyfFiles);

RemapRetainCustomNodeFilePaths(contentFiles.SelectMany(files => files).ToList(), dyfFiles);
Expand Down Expand Up @@ -213,7 +213,7 @@ private void WritePackageHeader(Package package, IDirectoryInfo rootDir)
fileSystem.WriteAllText(headerPath, pkgHeaderStr);
}

internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<string>> contentFiles, IEnumerable<string> markdownFiles, string oldDir, IDirectoryInfo rootDir, out List<string> dyfFiles)
internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<string>> contentFiles, IEnumerable<string> markdownFiles, string sourcePackageDir, IDirectoryInfo rootDir, out List<string> dyfFiles)
{
dyfFiles = new List<string>();

Expand All @@ -227,7 +227,7 @@ internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<stri
continue;
}

var relativePath = file.Substring(oldDir.Length);
var relativePath = file.Substring(sourcePackageDir.Length);

// Ensure the relative path starts with a directory separator.
if (!string.IsNullOrEmpty(relativePath) && relativePath[0] != Path.DirectorySeparatorChar)
Expand Down
40 changes: 40 additions & 0 deletions test/DynamoCoreWpfTests/PackageManager/PackageManagerUITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,46 @@ public void AssertPreviewPackageRetainFolderStructureEqualsPublishLocalPackageRe
Directory.Delete(publishPath, true);
}

[Test]
public void AssertPreviewPackageRetainFolderStructureEqualsPublishLocalPackageResultsForNestedFolders()
{
var packageName = "NestedPackage";
var pathManager = this.ViewModel.Model.PathManager as PathManager;
var publishPath = Path.Combine(pathManager.DefaultPackagesDirectory, packageName);

string nodePath = Path.Combine(TestDirectory, "pkgs", packageName);
var allFiles = Directory.GetFiles(nodePath, "*", SearchOption.AllDirectories).ToList();
var allFolders = Directory.GetDirectories(nodePath, "*", SearchOption.AllDirectories).ToList();

//now lets publish this package.
var newPkgVm = new PublishPackageViewModel(this.ViewModel);
newPkgVm.RetainFolderStructureOverride = true;
newPkgVm.AddAllFilesAfterSelection(allFiles);

var previewFilesAndFolders = PackageItemRootViewModel.GetFiles(newPkgVm.PreviewPackageContents.ToList());
var previewFiles = previewFilesAndFolders.Where(x => !x.DependencyType.Equals(DependencyType.Folder));
var previewFolders = previewFilesAndFolders.Where(x => x.DependencyType.Equals(DependencyType.Folder));

newPkgVm.Name = packageName;
newPkgVm.MajorVersion = "0";
newPkgVm.MinorVersion = "0";
newPkgVm.BuildVersion = "1";
newPkgVm.PublishLocallyCommand.Execute();

Assert.IsTrue(Directory.Exists(publishPath));

// Arrange
var createdFiles = Directory.GetFiles(publishPath, "*", SearchOption.AllDirectories).ToList();
var createdFolders = Directory.GetDirectories(publishPath, "*", SearchOption.AllDirectories).ToList();

// Assert
Assert.AreEqual(createdFiles.Count(), previewFiles.Count() + 1);
Assert.AreEqual(1, createdFolders.Count(), previewFolders.Count()); // One subfolder was created

// Clean up
Directory.Delete(publishPath, true);
}

[Test]
public void AssertPublishLocalHandleType()
{
Expand Down
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/pkgs/NestedPackage/pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"license":"","file_hash":null,"name":"NestedPackage","version":"1.0.0","description":"original package","group":"","keywords":null,"dependencies":[],"contents":"","engine_version":"2.1.0.7840","engine":"dynamo","engine_metadata":"","site_url":"","repository_url":"","contains_binaries":true,"node_libraries":["Package, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]}

0 comments on commit 157fff7

Please sign in to comment.