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

Overwrite Extraction with Original Folder Structure #212

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
37 changes: 33 additions & 4 deletions src/LessMsi.Cli/ExtractCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,45 @@ private ExtractionMode getExtractionMode(string commandArgument)
commandArgument = commandArgument.ToLowerInvariant();
ExtractionMode extractionMode = ExtractionMode.PreserveDirectoriesExtraction;

if (commandArgument[commandArgument.Length - 1] == 'o')
if (isFlatExtractionRequired(commandArgument))
{
extractionMode = ExtractionMode.OverwriteFlatExtraction;
if (commandArgument[commandArgument.Length - 1] == 'o')
{
extractionMode = ExtractionMode.OverwriteFlatExtraction;
}
else if (commandArgument[commandArgument.Length - 1] == 'r')
{
extractionMode = ExtractionMode.RenameFlatExtraction;
}
}
else if (commandArgument[commandArgument.Length - 1] == 'r')
else
{
extractionMode = ExtractionMode.RenameFlatExtraction;
if (isRegularExtracionWithOverwriteRequired(commandArgument))
{
extractionMode = ExtractionMode.OverwriteExtraction;
}
}

return extractionMode;
}

private bool isFlatExtractionRequired(string commandArgument)
{
bool flatExtractionRequiredFlag = false;

if (commandArgument.Length > 1)
{
flatExtractionRequiredFlag = commandArgument[1] == 'f';
}

return flatExtractionRequiredFlag;
}

private bool isRegularExtracionWithOverwriteRequired(string commandArgument)
{
bool regularExtracionWithOverwriteFlag = commandArgument[commandArgument.Length - 1] == 'o';

return regularExtracionWithOverwriteFlag;
}
Comment on lines +42 to +81
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see mega5800#1 for a simplification here.

}
}
7 changes: 6 additions & 1 deletion src/LessMsi.Cli/ExtractionMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public enum ExtractionMode
/// <summary>
/// Value indicating that a file extraction overwriting identical files should be performed.
/// </summary>
OverwriteFlatExtraction
OverwriteFlatExtraction,
/// <summary>
/// Value indicating that a file extraction overwriting identical files should be performed.
/// While preserving the directories structures
/// </summary>
OverwriteExtraction
}
}
17 changes: 17 additions & 0 deletions src/LessMsi.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static int Main(string[] args)
var subcommands = new Dictionary<string, LessMsiCommand> {
{"o", new OpenGuiCommand()},
{"x", extractCommand},
{"xo", extractCommand},
{"xfo", extractCommand},
{"xfr", extractCommand},
{"/x", extractCommand},
Expand Down Expand Up @@ -139,6 +140,10 @@ public static void DoExtraction(string msiFileName, string outDirName, List<stri
else
{
Wixtracts.ExtractFiles(msiFile, outDirName, filesToExtract.ToArray(), PrintProgress);
if (extractionMode == ExtractionMode.OverwriteExtraction)
{
deleteDuplicateFiles(outDirName);
}
}
}

Expand Down Expand Up @@ -209,5 +214,17 @@ private static string EnsureAbsolutePath(string filePath)

return Path.GetFullPath(filePath);
}

private static void deleteDuplicateFiles(string folderPath)
{
foreach (string file in Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories))
{
string fileName = Path.GetFileName(file);
if (fileName.Contains("duplicate"))
{
File.Delete(file);
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/LessMsi.Core/Msi/Wixtracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ public static void ExtractFiles(Path msi, string outputDir, MsiFile[] filesToExt
LessIO.Path destName = LessIO.Path.Combine(targetDirectoryForFile, entry.LongFileName);
if (FileSystem.Exists(destName))
{
Debug.Fail(string.Format("output file '{0}' already exists. We'll make it unique, but this is probably a strange msi or a bug in this program.", destName));
Debug.WriteLine(string.Format("output file '{0}' already exists. We'll make it unique, but this is probably a strange msi or a bug in this program.", destName));
mega5800 marked this conversation as resolved.
Show resolved Hide resolved

//make unique
// ReSharper disable HeuristicUnreachableCode
Trace.WriteLine(string.Concat("Duplicate file found \'", destName, "\'"));
Expand Down
22 changes: 22 additions & 0 deletions src/Lessmsi.Tests/CommandLineExtractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public void FlatRenameExtract1Arg()
TestExtraction(commandLine, GetTestName(), "NUnit-2.5.2.9222", false, flatExtractionFlag: true);
}

[Fact]
public void OverwriteExtract1Arg()
{
var commandLine = "xo TestFiles\\MsiInput\\AppleMobileDeviceSupport64.msi";
// setting "AppleMobileDeviceSupport64.msi" as actualEntriesOutputDir value, since no other output dir specified in command line text
TestExtraction(commandLine, GetTestName(), "AppleMobileDeviceSupport64", false);
}

[Fact]
public void Extract2Args()
{
Expand Down Expand Up @@ -68,6 +76,13 @@ public void FlatRenameExtract2Args()
TestExtraction(commandLine, GetTestName(), "FlatRenameExtract2Args", false, flatExtractionFlag: true);
}

[Fact]
public void OverwriteExtract2Args()
{
var commandLine = "xo TestFiles\\MsiInput\\AppleMobileDeviceSupport64.msi OverwriteExtract2Args\\";
TestExtraction(commandLine, GetTestName(), "OverwriteExtract2Args", false);
}

[Fact]
public void Extract3Args()
{
Expand Down Expand Up @@ -96,6 +111,13 @@ public void FlatRenameExtract3Args()
TestExtraction(commandLine, GetTestName(), "FlatRenameExtract3Args", false, flatExtractionFlag: true);
}

[Fact]
public void OverwriteExtract3Args()
{
var commandLine = "xo TestFiles\\MsiInput\\AppleMobileDeviceSupport64.msi OverwriteExtract3Args\\ \"api-ms-win-core-file-l1-1-0.dll\" \"api-ms-win-core-file-l1-1-0.dll.duplicate1\"";
TestExtraction(commandLine, GetTestName(), "OverwriteExtract3Args", false);
}

[Fact]
public void ExtractCompatibility1Arg()
{
Expand Down
Loading