Skip to content

Commit

Permalink
Handle rooted file harvesting.
Browse files Browse the repository at this point in the history
Also don't fail-fast when directory doesn't exist.

Fixes wixtoolset/issues#8740.
  • Loading branch information
barnson committed Sep 22, 2024
1 parent dd2fe20 commit 372d892
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
92 changes: 44 additions & 48 deletions src/wix/WixToolset.Core/HarvestFilesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,19 @@ private void HarvestFiles(HarvestFilesSymbol harvestFile, IntermediateSection se

var resolvedFiles = Enumerable.Empty<WildcardFile>();

try
{
var included = this.GetWildcardFiles(harvestFile, inclusions);
var excluded = this.GetWildcardFiles(harvestFile, exclusions);
var included = this.GetWildcardFiles(harvestFile, inclusions);
var excluded = this.GetWildcardFiles(harvestFile, exclusions);

foreach (var excludedFile in excluded)
{
this.Messaging.Write(OptimizerVerboses.ExcludedFile(harvestFile.SourceLineNumbers, excludedFile.Path));
}
foreach (var excludedFile in excluded)
{
this.Messaging.Write(OptimizerVerboses.ExcludedFile(harvestFile.SourceLineNumbers, excludedFile.Path));
}

resolvedFiles = included.Except(excluded, comparer).ToList();
resolvedFiles = included.Except(excluded, comparer).ToList();

if (!resolvedFiles.Any())
{
this.Messaging.Write(OptimizerWarnings.ZeroFilesHarvested(harvestFile.SourceLineNumbers));
}
}
catch (DirectoryNotFoundException e)
if (!resolvedFiles.Any())
{
this.Messaging.Write(OptimizerWarnings.ExpectedDirectory(harvestFile.SourceLineNumbers, e.Message));

return;
this.Messaging.Write(OptimizerWarnings.ZeroFilesHarvested(harvestFile.SourceLineNumbers));
}

foreach (var fileByRecursiveDir in resolvedFiles.GroupBy(resolvedFile => resolvedFile.RecursiveDir, resolvedFile => resolvedFile.Path))
Expand Down Expand Up @@ -139,48 +130,53 @@ private IEnumerable<WildcardFile> GetWildcardFiles(HarvestFilesSymbol harvestFil

var files = new List<WildcardFile>();

foreach (var pattern in patterns)
try
{
// Resolve bind paths, if any, which might result in multiple directories.
foreach (var path in this.ResolveBindPaths(sourceLineNumbers, pattern))
foreach (var pattern in patterns)
{
var sourceDirectory = String.IsNullOrEmpty(sourcePath) ? Path.GetDirectoryName(sourceLineNumbers.FileName) : sourcePath;
var recursive = path.IndexOf("**") >= 0;
var filePortion = Path.GetFileName(path);
var directoryPortion = Path.GetDirectoryName(path);

if (directoryPortion?.EndsWith(@"\**") == true)
// Resolve bind paths, if any, which might result in multiple directories.
foreach (var path in this.ResolveBindPaths(sourceLineNumbers, pattern))
{
directoryPortion = directoryPortion.Substring(0, directoryPortion.Length - 3);
}
var sourceDirectory = String.IsNullOrEmpty(sourcePath) ? Path.GetDirectoryName(sourceLineNumbers.FileName) : sourcePath;
var recursive = path.IndexOf("**") >= 0;
var filePortion = Path.GetFileName(path);
var directoryPortion = Path.GetDirectoryName(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

var recursiveDirOffset = directoryPortion.Length + 1;
if (directoryPortion?.EndsWith(@"\**") == true)
{
directoryPortion = directoryPortion.Substring(0, directoryPortion.Length - 3);
}

if (directoryPortion is null || directoryPortion.Length == 0 || directoryPortion == "**")
{
directoryPortion = sourceDirectory;
recursiveDirOffset = sourceDirectory.Length + 1;
if (directoryPortion is null || directoryPortion.Length == 0 || directoryPortion == "**")
{
directoryPortion = sourceDirectory;

}
else if (!Path.IsPathRooted(directoryPortion))
{
directoryPortion = Path.Combine(sourceDirectory, directoryPortion);
recursiveDirOffset = directoryPortion.Length + 1;
}
}
else if (!Path.IsPathRooted(directoryPortion))
{
directoryPortion = Path.Combine(sourceDirectory, directoryPortion);
}

var foundFiles = Directory.EnumerateFiles(directoryPortion, filePortion, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
var recursiveDirOffset = directoryPortion.Length + 1;

foreach (var foundFile in foundFiles)
{
var recursiveDir = Path.GetDirectoryName(foundFile.Substring(recursiveDirOffset));
files.Add(new WildcardFile()
var foundFiles = Directory.EnumerateFiles(directoryPortion, filePortion, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

foreach (var foundFile in foundFiles)
{
RecursiveDir = recursiveDir,
Path = foundFile,
});
var recursiveDir = Path.GetDirectoryName(foundFile.Substring(recursiveDirOffset));
files.Add(new WildcardFile()
{
RecursiveDir = recursiveDir,
Path = foundFile,
});
}
}
}
}
catch (DirectoryNotFoundException e)
{
this.Messaging.Write(OptimizerWarnings.ExpectedDirectory(harvestFile.SourceLineNumbers, e.Message));
}

return files;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void MissingHarvestDirectoryIsAWarning()
Assert.Equal(new[]
{
8601,
8600,
8601,
8600,
}, messages);
});
}
Expand Down Expand Up @@ -272,7 +274,11 @@ public void CanHarvestFilesInStandardDirectory()
@"flsCgt3Noa1VJHlHG5HOVjD5vdJm5Q=PFiles\MsiPackage\files2_sub3\FileName.Extension",
};

Build("StandardDirectory.wxs", (msiPath, _) => AssertFileIdsAndTargetPaths(msiPath, expected));
Build("StandardDirectory.wxs", (msiPath, result) =>
{
result.AssertSuccess();
AssertFileIdsAndTargetPaths(msiPath, expected);
}, warningsAsErrors: false);
}

[Fact]
Expand Down Expand Up @@ -319,7 +325,7 @@ private static void AssertFileIdsAndTargetPaths(string msiPath, string[] expecte
Assert.Equal(sortedExpected, actual);
}

private static void Build(string file, Action<string, WixRunnerResult> tester, bool isPackage = true, params string[] additionalCommandLineArguments)
private static void Build(string file, Action<string, WixRunnerResult> tester, bool isPackage = true, bool warningsAsErrors = true, params string[] additionalCommandLineArguments)
{
var folder = TestData.Get("TestData", "HarvestFiles");

Expand All @@ -346,7 +352,7 @@ private static void Build(string file, Action<string, WixRunnerResult> tester, b
arguments.AddRange(additionalCommandLineArguments);
}

var result = WixRunner.Execute(arguments.ToArray());
var result = WixRunner.Execute(warningsAsErrors, arguments.ToArray());

tester(msiPath, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<StandardDirectory Id="ProgramFiles6432Folder">
<!-- Relies on default-feature feature to include naked files in package. -->
<Files Subdirectory="MsiPackage" Include="files1\**" />
<Files Subdirectory="MsiPackage" Include="files2\**" />
<Files Subdirectory="MsiPackage" Include="files2\**">
<Exclude Files="notfound\**" />
</Files>
</StandardDirectory>
</Package>
</Wix>

0 comments on commit 372d892

Please sign in to comment.