Skip to content

Commit

Permalink
Shift common installer fields to manifest root level (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh committed Jul 21, 2023
1 parent 4364e03 commit 4f8406f
Show file tree
Hide file tree
Showing 8 changed files with 891 additions and 40 deletions.
75 changes: 68 additions & 7 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Microsoft.WingetCreateCLI.Commands
{
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -23,8 +24,8 @@ namespace Microsoft.WingetCreateCLI.Commands
using Microsoft.WingetCreateCore.Models.Locale;
using Microsoft.WingetCreateCore.Models.Version;
using Octokit;
using RestSharp;

using RestSharp;

/// <summary>
/// Abstract base command class that all commands inherit from.
/// </summary>
Expand Down Expand Up @@ -419,16 +420,16 @@ protected static void RemoveEmptyStringFieldsInManifests(Manifests manifests)
}

/// <summary>
/// Shifts common installer fields from root to installer level.
/// Shifts common installer fields from manifest root to installer level.
/// </summary>
/// <param name="installerManifest">Wrapper object containing the installer manifest object models.</param>
protected static void ShiftFieldsFromRootToInstallerLevel(InstallerManifest installerManifest)
protected static void ShiftRootFieldsToInstallerLevel(InstallerManifest installerManifest)
{
var rootProperties = installerManifest.GetType().GetProperties();
var installerProperties = installerManifest.Installers.First().GetType().GetProperties();

// Get common properties between root and installer level
var commonProperties = rootProperties.Where(p => installerProperties.Any(ip => ip.Name == p.Name));
var commonProperties = rootProperties.Where(rp => installerProperties.Any(ip => ip.Name == rp.Name));

foreach (var property in commonProperties)
{
Expand All @@ -445,7 +446,67 @@ protected static void ShiftFieldsFromRootToInstallerLevel(InstallerManifest inst
property.SetValue(installerManifest, null);
}
}
}
}

/// <summary>
/// Shifts common installer fields from installer level to manifest root.
/// </summary>
/// <param name="installerManifest">Wrapper object containing the installer manifest object models.</param>
protected static void ShiftInstallerFieldsToRootLevel(InstallerManifest installerManifest)
{
var rootProperties = installerManifest.GetType().GetProperties();
var installerProperties = installerManifest.Installers.First().GetType().GetProperties();

// Get common properties between root and installer level
var commonProperties = rootProperties.Where(rp => installerProperties.Any(ip => ip.Name == rp.Name));

foreach (var property in commonProperties)
{
var firstInstallerValue = installerManifest.Installers.First().GetType().GetProperty(property.Name).GetValue(installerManifest.Installers.First());
if (firstInstallerValue != null)
{
// Check if all installers have the same value
bool allInstallersHaveSameValue = installerManifest.Installers.All(i =>
{
var propertyValue = i.GetType().GetProperty(property.Name).GetValue(i);
return propertyValue != null &&
propertyValue.Equals(firstInstallerValue);
});

// If value is false, it can be because we don't have .Equals() override implemented for the type of the property.
// For that, we check further if the property is of type list and check if the lists are equal
if (!allInstallersHaveSameValue)
{
if (firstInstallerValue is IList installerValueList)
{
allInstallersHaveSameValue = installerManifest.Installers.All(i =>
{
var propertyValue = i.GetType().GetProperty(property.Name).GetValue(i);
if (propertyValue is IList otherList)
{
return installerValueList.Cast<object>().SequenceEqual(otherList.Cast<object>());
}
else
{
return false;
}
});
}
}

if (allInstallersHaveSameValue)
{
// Copy the value to root level
property.SetValue(installerManifest, firstInstallerValue);
foreach (var installer in installerManifest.Installers)
{
// Set installer value to null
installer.GetType().GetProperty(property.Name).SetValue(installer, null);
}
}
}
}
}

/// <summary>
/// Launches the GitHub OAuth flow and obtains a GitHub token.
Expand Down
9 changes: 5 additions & 4 deletions src/WingetCreateCLI/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,11 @@ public override async Task<bool> Execute()
}
}

PromptPropertiesAndDisplayManifests(manifests);
PromptManifestProperties(manifests);
MergeNestedInstallerFilesIfApplicable(manifests.InstallerManifest);
ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest);
RemoveEmptyStringFieldsInManifests(manifests);
DisplayManifestPreview(manifests);
isManifestValid = ValidateManifestsInTempDir(manifests);
}
while (Prompt.Confirm(Resources.ConfirmManifestCreation_Message));
Expand Down Expand Up @@ -270,12 +273,11 @@ public override async Task<bool> Execute()
}
}

private static void PromptPropertiesAndDisplayManifests(Manifests manifests)
private static void PromptManifestProperties(Manifests manifests)
{
PromptRequiredProperties(manifests.VersionManifest);
PromptRequiredProperties(manifests.InstallerManifest, manifests.VersionManifest);
PromptRequiredProperties(manifests.DefaultLocaleManifest, manifests.VersionManifest);
MergeNestedInstallerFilesIfApplicable(manifests.InstallerManifest);

Console.WriteLine();
if (Prompt.Confirm(Resources.ModifyOptionalDefaultLocaleFields_Message))
Expand All @@ -289,7 +291,6 @@ private static void PromptPropertiesAndDisplayManifests(Manifests manifests)
}

Console.WriteLine();
DisplayManifestPreview(manifests);
}

private static void PromptRequiredProperties<T>(T manifest, VersionManifest versionManifest = null)
Expand Down
6 changes: 5 additions & 1 deletion src/WingetCreateCLI/Commands/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ await this.UpdateManifestsInteractively(initialManifests) :
}

RemoveEmptyStringFieldsInManifests(updatedManifests);
ShiftInstallerFieldsToRootLevel(updatedManifests.InstallerManifest);
DisplayManifestPreview(updatedManifests);

if (string.IsNullOrEmpty(this.OutputDir))
Expand Down Expand Up @@ -227,6 +228,7 @@ await this.UpdateManifestsInteractively(initialManifests) :
/// <returns>Manifests object representing the updates manifest content, or null if the update failed.</returns>
public async Task<Manifests> UpdateManifestsAutonomously(Manifests manifests)
{
ShiftRootFieldsToInstallerLevel(manifests.InstallerManifest);
InstallerManifest installerManifest = manifests.InstallerManifest;

if (!this.InstallerUrls.Any())
Expand Down Expand Up @@ -306,6 +308,7 @@ public async Task<Manifests> UpdateManifestsAutonomously(Manifests manifests)
PackageParser.UpdateInstallerNodesAsync(installerMetadataList, installerManifest);
DisplayArchitectureWarnings(installerMetadataList);
ResetVersionSpecificFields(manifests);
ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest);
}
catch (InvalidOperationException)
{
Expand Down Expand Up @@ -661,19 +664,20 @@ private List<InstallerMetadata> ParseInstallerUrlsForOverrides(List<string> inst
/// <returns>The updated manifest.</returns>
private async Task<Manifests> UpdateManifestsInteractively(Manifests manifests)
{
ShiftRootFieldsToInstallerLevel(manifests.InstallerManifest);
Prompt.Symbols.Done = new Symbol(string.Empty, string.Empty);
Prompt.Symbols.Prompt = new Symbol(string.Empty, string.Empty);

// Clone the list of installers in order to preserve initial values.
Manifests originalManifest = new Manifests { InstallerManifest = new InstallerManifest() };
ShiftFieldsFromRootToInstallerLevel(manifests.InstallerManifest);
originalManifest.InstallerManifest.Installers = manifests.CloneInstallers();

do
{
Console.Clear();
manifests.InstallerManifest.Installers = originalManifest.CloneInstallers();
await this.UpdateInstallersInteractively(manifests.InstallerManifest.Installers);
ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest);
DisplayManifestPreview(manifests);
ValidateManifestsInTempDir(manifests);
}
Expand Down
Loading

0 comments on commit 4f8406f

Please sign in to comment.