Skip to content

Commit

Permalink
Remove empty list fields from the manifest (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh authored Dec 10, 2024
1 parent 786625f commit 94c5d21
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
25 changes: 18 additions & 7 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,19 @@ protected static void DisplayManifestPreview(Manifests manifests)
/// Removes fields with empty string values from all manifests.
/// </summary>
/// <param name="manifests">Wrapper object containing the manifest object models.</param>
protected static void RemoveEmptyStringFieldsInManifests(Manifests manifests)
protected static void RemoveEmptyStringAndListFieldsInManifests(Manifests manifests)
{
RemoveEmptyStringFields(manifests.InstallerManifest);
RemoveEmptyStringFields(manifests.DefaultLocaleManifest);
RemoveEmptyStringAndListFields(manifests.InstallerManifest);
RemoveEmptyStringAndListFields(manifests.DefaultLocaleManifest);

foreach (var localeManifest in manifests.LocaleManifests)
{
RemoveEmptyStringFields(localeManifest);
RemoveEmptyStringAndListFields(localeManifest);
}

foreach (var installer in manifests.InstallerManifest.Installers)
{
RemoveEmptyStringFields(installer);
RemoveEmptyStringAndListFields(installer);
}
}

Expand Down Expand Up @@ -824,13 +824,16 @@ protected string GetPRTitle(Manifests currentManifest, Manifests repositoryManif
}

/// <summary>
/// Removes fields with empty string values from a given object.
/// Removes fields with empty string and list values from a given object.
/// </summary>
/// <param name="obj">Object to remove empty string fields from.</param>
private static void RemoveEmptyStringFields(object obj)
private static void RemoveEmptyStringAndListFields(object obj)
{
var stringProperties = obj.GetType().GetProperties()
.Where(p => p.PropertyType == typeof(string));
var listProperties = obj.GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType &&
p.PropertyType.GetGenericTypeDefinition() == typeof(List<>));

foreach (var prop in stringProperties)
{
Expand All @@ -839,6 +842,14 @@ private static void RemoveEmptyStringFields(object obj)
prop.SetValue(obj, null);
}
}

foreach (var prop in listProperties)
{
if (prop.GetValue(obj) is IList list && list.Count == 0)
{
prop.SetValue(obj, null);
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/WingetCreateCLI/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public override async Task<bool> Execute()
PromptManifestProperties(manifests);
MergeNestedInstallerFilesIfApplicable(manifests.InstallerManifest);
ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest);
RemoveEmptyStringFieldsInManifests(manifests);
RemoveEmptyStringAndListFieldsInManifests(manifests);
DisplayManifestPreview(manifests);
isManifestValid = ValidateManifestsInTempDir(manifests);
}
Expand Down
2 changes: 1 addition & 1 deletion src/WingetCreateCLI/Commands/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ await this.UpdateManifestsInteractively(initialManifests) :
return false;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ Installers:
PackageFamilyName: ''
PrivacyUrl: ''
Author: ''
Platform: []
InstallModes: []
FileExtensions: []
Commands: []
AppsAndFeaturesEntries: []
Protocols: []
Capabilities: []
UnsupportedArguments: []
UnsupportedOSArchitectures: []
RestrictedCapabilities: []
NestedInstallerFiles: []
ExpectedReturnCodes: []
Tags: []
Agreements: []
Documentations: []
Icons: []
PackageLocale: en-US
ManifestType: singleton
ManifestVersion: 1.0.0
ManifestVersion: 1.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public async Task UpdateMultipleUrlManifests()
}

/// <summary>
/// Verifies that any fields with empty string values are replaced with null so that they do not appear in the manifest output.
/// Verifies that any fields with empty string and list values are replaced with null so that they do not appear in the manifest output.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Test]
Expand All @@ -181,12 +181,31 @@ public async Task UpdateRemovesEmptyFields()
ClassicAssert.IsTrue(updatedManifestContents.Any(), "Updated manifests were not created successfully");

Manifests updatedManifests = Serialization.DeserializeManifestContents(updatedManifestContents);

// Empty string fields are removed
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.PrivacyUrl, "PrivacyUrl should be null.");
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Author, "Author should be null.");

var firstInstaller = updatedManifests.InstallerManifest.Installers.First();
ClassicAssert.IsNull(firstInstaller.ProductCode, "ProductCode should be null.");
ClassicAssert.IsNull(firstInstaller.PackageFamilyName, "ProductCode should be null.");

// Empty list fields are removed
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Platform, "Platform should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.InstallModes, "InstallModes should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.FileExtensions, "FileExtensions should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Commands, "Commands should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.AppsAndFeaturesEntries, "AppsAndFeaturesEntries should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Protocols, "Protocols should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Capabilities, "Capabilities should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.UnsupportedArguments, "UnsupportedArguments should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.UnsupportedOSArchitectures, "UnsupportedOSArchitectures should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.RestrictedCapabilities, "RestrictedCapabilities should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.NestedInstallerFiles, "NestedInstallerFiles should be null.");
ClassicAssert.IsNull(updatedManifests.InstallerManifest.ExpectedReturnCodes, "ExpectedReturnCodes should be null.");
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Tags, "Tags should be null.");
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Agreements, "Agreements should be null.");
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Documentations, "Documentations should be null.");
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Icons, "Icons should be null.");
}

/// <summary>
Expand Down

0 comments on commit 94c5d21

Please sign in to comment.