Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retain package structure flow where nested directory at root level were lost #14918

Merged
merged 12 commits into from
Feb 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ public bool RetainFolderStructureOverride
}
}
}
private string CurrentPackageDirectory { get; set; }
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
private static MetadataLoadContext sharedMetaDataLoadContext = null;
/// <summary>
/// A shared MetaDataLoadContext that is used for assembly inspection during package publishing.
Expand Down Expand Up @@ -995,7 +996,10 @@ private void RefreshPackageContents()
.ToObservableCollection();

var items = new Dictionary<string, PackageItemRootViewModel>();

if (!String.IsNullOrEmpty(this.CurrentPackageDirectory))
{
var v = 1;
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
}
if(!String.IsNullOrEmpty(RootFolder))
{
var root = new PackageItemRootViewModel(RootFolder);
Expand All @@ -1022,7 +1026,7 @@ private void RefreshPackageContents()
}
}

var updatedItems = BindParentToChild(items);
var updatedItems = BindParentToChild(items);

updatedItems.AddRange(itemsToAdd.Where(pa => pa.DependencyType.Equals(DependencyType.CustomNode)));

Expand Down Expand Up @@ -1050,8 +1054,6 @@ private bool IsDuplicateFile(PackageItemRootViewModel item1, PackageItemRootView

private List<PackageItemRootViewModel> BindParentToChild(Dictionary<string, PackageItemRootViewModel> items)
{
var updatedItems = new List<PackageItemRootViewModel>();

foreach (var parent in items)
{
foreach(var child in items)
Expand All @@ -1067,10 +1069,33 @@ private List<PackageItemRootViewModel> BindParentToChild(Dictionary<string, Pack
}

// Only add the folder items, they contain the files
updatedItems = items.Values.Where(x => !x.isChild).ToList();
var updatedItems = GetRootItems(items);
return updatedItems;
}

private List<PackageItemRootViewModel> GetRootItems(Dictionary<string, PackageItemRootViewModel> items)
{
var rootItems = items.Values.Where(x => !x.isChild).ToList();
if (!rootItems.Any()) return rootItems;

var root = new PackageItemRootViewModel(CurrentPackageDirectory);
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))
{
root.AddChild(item);
zeusongit marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
root.ChildItems.Add(item);
}
}
return root.ChildItems.ToList();
}

/// <summary>
/// Test if path2 is subpath of path1
/// If it is, make sure all the intermediate file paths are created as separte PackageItemRootViewModel
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -1089,7 +1114,11 @@ private bool IsSubPathOfDeep(PackageItemRootViewModel path1, PackageItemRootView
{
return true;
}
else di2 = di2.Parent;
else
{
if (di2.Parent.FullName.Length < di1.FullName.Length) return false;
zeusongit marked this conversation as resolved.
Show resolved Hide resolved
di2 = di2.Parent;
}
}

return false;
Expand Down Expand Up @@ -1403,6 +1432,7 @@ internal static PublishPackageViewModel FromLocalPackage(DynamoViewModel dynamoV
CopyrightHolder = pkg.CopyrightHolder,
CopyrightYear = pkg.CopyrightYear,
IsPublishFromLocalPackage = true,
CurrentPackageDirectory = pkg.RootDirectory,
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
//default retain folder structure to true when publishing a new version from local.
RetainFolderStructureOverride = retainFolderStructure
};
Expand Down
37 changes: 4 additions & 33 deletions src/DynamoPackages/PackageDirectoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +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;
package.RootDirectory = rootDir.FullName;

var dyfFiles = new List<string>();

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

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

internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<string>> contentFiles, IEnumerable<string> markdownFiles, IDirectoryInfo rootDir, out List<string> dyfFiles)
internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<string>> contentFiles, IEnumerable<string> markdownFiles, string oldDir, IDirectoryInfo rootDir, out List<string> dyfFiles)
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
{
dyfFiles = new List<string>();

foreach (var files in contentFiles)
{
// We expect that files are bundled in root folders
// For single files, just get its folder
var commonPath = files.Count() > 1 ? GetLongestCommonPrefix(files.ToArray()) : Path.GetDirectoryName(files.FirstOrDefault());
commonPath = commonPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var commonRootPath = Path.GetDirectoryName(commonPath);
if (commonRootPath == null) commonRootPath = commonPath; // already at the root

foreach (var file in files.Where(x => x != null))
{
// If the file doesn't actually exist, don't copy it
Expand All @@ -233,7 +227,7 @@ internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<stri
continue;
}

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

// Ensure the relative path starts with a directory separator.
if (!string.IsNullOrEmpty(relativePath) && relativePath[0] != Path.DirectorySeparatorChar)
Expand Down Expand Up @@ -387,29 +381,6 @@ public static string NormalizePath(string path)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.ToUpperInvariant();
}


/// <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>
public static 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));
}

#endregion

}
Expand Down
Loading