Skip to content

Commit

Permalink
fix expected number of subfolders
Browse files Browse the repository at this point in the history
- now correctly keeps single folder structure while allowing for correct nesting when multiple distinct root folders are present
  • Loading branch information
dnenov committed Jul 3, 2024
1 parent 9a2565c commit 3a84a5d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
41 changes: 24 additions & 17 deletions src/DynamoPackages/PackageDirectoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<stri
{
dyfFiles = new List<string>();

// Normalize roots to ensure consistent comparison
var normalizedRoots = roots.Select(r => Path.GetFullPath(r)).ToList();

// Determine if all files are under a single folder
var distinctFolders = contentFiles.SelectMany(f => f)
.Where(f => f != null)
.Select(f => f.Substring(normalizedRoots.First().Length).TrimStart(new char[] { '/', '\\' }))
.Select(rp => rp.Split(new char[] { '/', '\\' })[0])
.Distinct()
.Count();

foreach (var files in contentFiles)
{
foreach (var file in files.Where(x => x != null))
Expand All @@ -240,36 +251,32 @@ internal void CopyFilesIntoRetainedPackageDirectory(IEnumerable<IEnumerable<stri

string relativePath = "";

foreach (var root in roots)
foreach (var root in normalizedRoots)
{
if (file.StartsWith(root))
var normalizedFile = Path.GetFullPath(file);
if (normalizedFile.StartsWith(root, StringComparison.OrdinalIgnoreCase))
{
relativePath = file.Substring(root.Length);
// If we have more than 1 root, than we need to nest into a new root folder
// If we don't, and in order to preserve 1-to-1 folder structure, we remove the original root and replace with the package name
if(roots.Count() == 1 && contentFiles.Any(f => f.Count() == 1))
{
relativePath = RemoveFirstFolder(relativePath);
}
relativePath = normalizedFile.Substring(root.Length);
// Trim leading directory separators
relativePath = relativePath.TrimStart(new char[] { '/', '\\' });
}
}

// If we have more than 1 root, then we need to nest into a new root folder
// If we don't, and in order to preserve 1-to-1 folder structure, we remove the original root and replace with the package name
if (normalizedRoots.Count() == 1 && distinctFolders == 1)
{
relativePath = RemoveFirstFolder(relativePath);
}

// Ensure the relative path starts with a directory separator.
if (!string.IsNullOrEmpty(relativePath) && relativePath[0] != Path.DirectorySeparatorChar)
{
relativePath = relativePath.TrimStart(new char[] { '/', '\\' });
relativePath = Path.DirectorySeparatorChar + relativePath;
}

var destPath = Path.Combine(rootDir.FullName, relativePath.TrimStart('\\'));

// We are already creating the pkg.json file ourselves, so skip it, also skip if we are copying the file to itself.
// Should we stop creating the pkg.json on behalf of the user? If they are using this feature, they probably want to handle this as well
//if (destPath.Equals(Path.Combine(rootDir.FullName, "pkg.json")) || destPath.Equals(file))
//{
// continue;
//}

if (fileSystem.FileExists(destPath))
{
fileSystem.DeleteFile(destPath);
Expand Down
19 changes: 18 additions & 1 deletion test/DynamoCoreWpfTests/PublishPackageViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ public void FindCommonPaths_MultipleRoots()
}


[Test]
public void FindCommonPaths_MultipleRootsSingleCommonPath()
{
var vm = new PublishPackageViewModel(ViewModel);
var paths = new string[]
{
"C:\\Packages\\PackageTest\\Loc1\\Sub11\\test.docx",
"C:\\Packages\\PackageTest\\Loc2\\Sub21\\test2.docx",
"C:\\Packages\\PackageTest2\\Loc1\\test.dyn",
};

var commonPaths = vm.GetCommonPaths(paths);

Assert.AreEqual(1, commonPaths.Count);
Assert.AreEqual("C:\\Packages", commonPaths[0]);
}


[Test]
public void FindCommonPaths_BaseRoot()
{
Expand All @@ -487,7 +505,6 @@ public void FindCommonPaths_BaseRoot()
Assert.AreEqual("C:\\", commonPaths[0]);
}


#endregion

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public void BuildRetainPackageDirectory_CopiesMarkDownFiles()
var pkgsDir = @"C:\dynamopackages";
var roots = new List<string> { @"C:\pkg\" };

db.BuildRetainDirectory(pkg, pkgsDir, roots, files, Enumerable.Empty<string>());
db.BuildRetainDirectory(pkg, pkgsDir, roots, files, markdownFiles);

var mdDir = Path.Combine(pkgsDir, pkg.Name, PackageDirectoryBuilder.DocumentationDirectoryName);

Expand Down

0 comments on commit 3a84a5d

Please sign in to comment.