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

Prompt for RelativeFilePaths in interactive update #431

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 63 additions & 46 deletions src/WingetCreateCLI/Commands/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public async Task<Manifests> UpdateManifestsAutonomously(Manifests manifests)
// If the previous RelativeFilePath is not an exact match, check if there is a new suitable match in the archive.
if (!File.Exists(Path.Combine(extractDirectory, nestedInstallerFile.RelativeFilePath)))
{
string relativeFilePath = ObtainMatchingRelativeFilePath(nestedInstallerFile.RelativeFilePath, extractDirectory, packageFile);
string relativeFilePath = this.ObtainMatchingRelativeFilePath(nestedInstallerFile.RelativeFilePath, extractDirectory, packageFile);
if (string.IsNullOrEmpty(relativeFilePath))
{
return null;
Expand Down Expand Up @@ -440,29 +440,6 @@ private static string ExtractArchiveAndRetrieveDirectoryPath(string packageFileP
}
}

private static string ObtainMatchingRelativeFilePath(string oldRelativeFilePath, string directory, string archiveName)
{
string fileName = Path.GetFileName(oldRelativeFilePath);
string[] matchingFiles = Directory.GetFiles(directory, fileName, SearchOption.AllDirectories);

// If there is only one match in the archive, use that as the new relative file path.
if (matchingFiles.Length == 1)
{
string relativePath = matchingFiles.First().Replace(directory, string.Empty).TrimStart(Path.DirectorySeparatorChar);
return relativePath;
}
else if (matchingFiles.Length > 1)
{
Logger.ErrorLocalized(nameof(Resources.MultipleMatchingNestedInstallersFound_Error), fileName, Path.GetFileName(archiveName));
return null;
}
else
{
Logger.ErrorLocalized(nameof(Resources.NestedInstallerFileNotFound_Error), oldRelativeFilePath);
return null;
}
}

private static Manifests ConvertSingletonToMultifileManifest(WingetCreateCore.Models.Singleton.SingletonManifest singletonManifest)
{
// Create automapping configuration
Expand Down Expand Up @@ -622,6 +599,54 @@ private static void DisplayManifestsAsMenuSelection(Manifests manifests)
}
}

private string ObtainMatchingRelativeFilePath(string oldRelativeFilePath, string directory, string archiveName)
{
string fileName = Path.GetFileName(oldRelativeFilePath);
List<string> matchingFiles = Directory.GetFiles(directory, fileName, SearchOption.AllDirectories)
.Select(filePath => filePath = Path.GetRelativePath(directory, filePath))
.ToList();

// If there is only one match in the archive, use that as the new relative file path.
if (matchingFiles.Count == 1)
{
return matchingFiles.First();
}
else if (matchingFiles.Count > 1)
{
if (this.Interactive)
{
Console.WriteLine();
Logger.WarnLocalized(nameof(Resources.MultipleMatchingNestedInstallersFound_Warning), fileName, Path.GetFileName(archiveName));
return Prompt.Select(Resources.SelectRelativeFilePath_Message, matchingFiles);
}

Logger.ErrorLocalized(nameof(Resources.MultipleMatchingNestedInstallersFound_Error), fileName, Path.GetFileName(archiveName));
return null;
}
else
{
if (this.Interactive)
{
List<string> sameExtensionFiles = Directory.GetFiles(directory, $"*{Path.GetExtension(oldRelativeFilePath)}", SearchOption.AllDirectories)
.Select(filePath => filePath = Path.GetRelativePath(directory, filePath))
.ToList();
if (sameExtensionFiles.Count > 0)
{
Console.WriteLine();
Logger.WarnLocalized(nameof(Resources.NestedInstallerFileNotFound_Warning), oldRelativeFilePath);
return Prompt.Select(Resources.SelectRelativeFilePath_Message, sameExtensionFiles);
}
else
{
return null;
}
mdanish-kh marked this conversation as resolved.
Show resolved Hide resolved
}

Logger.ErrorLocalized(nameof(Resources.NestedInstallerFileNotFound_Error), oldRelativeFilePath);
return null;
}
}

/// <summary>
/// Parses the installer urls for any architecture or scope overrides.
/// </summary>
Expand Down Expand Up @@ -781,33 +806,25 @@ private async Task UpdateSingleInstallerInteractively(Installer installer)
if (packageFile.IsZipFile())
{
string extractDirectory = ExtractArchiveAndRetrieveDirectoryPath(packageFile);
string relativeFilePath = installer.NestedInstallerFiles.First().RelativeFilePath;
string pathToNestedInstaller = Path.Combine(extractDirectory, relativeFilePath);
if (File.Exists(pathToNestedInstaller))
{
packageFile = pathToNestedInstaller;
}
else
bool isRelativePathNull = false;

foreach (NestedInstallerFile nestedInstaller in installer.NestedInstallerFiles)
{
relativeFilePath = ObtainMatchingRelativeFilePath(relativeFilePath, extractDirectory, packageFile);
if (relativeFilePath == null)
nestedInstaller.RelativeFilePath = this.ObtainMatchingRelativeFilePath(nestedInstaller.RelativeFilePath, extractDirectory, packageFile);
if (string.IsNullOrEmpty(nestedInstaller.RelativeFilePath))
{
continue;
isRelativePathNull = true;
break;
}
}

pathToNestedInstaller = Path.Combine(extractDirectory, relativeFilePath);
packageFile = pathToNestedInstaller;

// Update the relative file path for all nested installers.
foreach (NestedInstallerFile nestedInstaller in installer.NestedInstallerFiles)
{
nestedInstaller.RelativeFilePath = ObtainMatchingRelativeFilePath(nestedInstaller.RelativeFilePath, extractDirectory, packageFile);
if (string.IsNullOrEmpty(nestedInstaller.RelativeFilePath))
{
continue;
}
}
if (isRelativePathNull)
{
Logger.ErrorLocalized(nameof(Resources.NestedInstallerTypeNotFound_Error));
continue;
}

packageFile = Path.Combine(extractDirectory, installer.NestedInstallerFiles.First().RelativeFilePath);
}

if (!PackageParser.ParsePackageAndUpdateInstallerNode(installer, packageFile, url))
Expand Down
40 changes: 38 additions & 2 deletions src/WingetCreateCLI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions src/WingetCreateCLI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@
{1} - will be replaced by the InstallerUrl</comment>
</data>
<data name="NestedInstallerFileNotFound_Error" xml:space="preserve">
<value>Nested installer not found in zip archive: {0}</value>
<value>Nested installer not found in zip archive: {0}. Please use the interactive mode to update this package.</value>
<comment>{0} - represents the relative file path of the nested installer file.</comment>
</data>
<data name="NestedInstallerFiles_KeywordDescription" xml:space="preserve">
Expand Down Expand Up @@ -1031,7 +1031,26 @@
<value>The url of the hosted icon file</value>
</data>
<data name="MultipleMatchingNestedInstallersFound_Error" xml:space="preserve">
<value>Multiple matches found for "{0}" from {1}</value>
<value>Multiple matches found for "{0}" from {1}. Please use the interactive mode to update this package.</value>
mdanish-kh marked this conversation as resolved.
Show resolved Hide resolved
<comment>{0} - will be replaced by the name of the nested installer file

{1} - will be replaced by the name of the zip archive.</comment>
</data>
<data name="MultipleMatchingNestedInstallersFound_Warning" xml:space="preserve">
<value>Multiple matches found for "{0}" from {1}.</value>
<comment>{0} - will be replaced by the name of the nested installer file

{1} - will be replaced by the name of the zip archive.</comment>
</data>
<data name="NestedInstallerFileNotFound_Warning" xml:space="preserve">
<value>Nested installer not found in zip archive: {0}</value>
<comment>{0} - represents the relative file path of the nested installer file.</comment>
</data>
<data name="NestedInstallerTypeNotFound_Error" xml:space="preserve">
<value>The zip archive does not contain a matching installer type with the old relative path.</value>
</data>
<data name="SelectRelativeFilePath_Message" xml:space="preserve">
<value>Select the new relative file path</value>
</data>
<data name="ShowCommand_HelpText" xml:space="preserve">
<value>Display a manifest from the Windows Package Manager repository</value>
Expand Down