From 6955f90ba0df7efc616b872dbe14813035c5c9c2 Mon Sep 17 00:00:00 2001 From: Muhammad Danish <88161975+mdanish-kh@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:52:31 +0500 Subject: [PATCH] Remove empty list fields from the manifest --- src/WingetCreateCLI/Commands/BaseCommand.cs | 25 +++++++++++++------ src/WingetCreateCLI/Commands/NewCommand.cs | 2 +- src/WingetCreateCLI/Commands/UpdateCommand.cs | 2 +- .../Resources/TestPublisher.EmptyFields.yaml | 18 ++++++++++++- .../UnitTests/UpdateCommandTests.cs | 23 +++++++++++++++-- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/WingetCreateCLI/Commands/BaseCommand.cs b/src/WingetCreateCLI/Commands/BaseCommand.cs index c6548542..c7e6ac17 100644 --- a/src/WingetCreateCLI/Commands/BaseCommand.cs +++ b/src/WingetCreateCLI/Commands/BaseCommand.cs @@ -424,19 +424,19 @@ protected static void DisplayManifestPreview(Manifests manifests) /// Removes fields with empty string values from all manifests. /// /// Wrapper object containing the manifest object models. - 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); } } @@ -824,13 +824,16 @@ protected string GetPRTitle(Manifests currentManifest, Manifests repositoryManif } /// - /// Removes fields with empty string values from a given object. + /// Removes fields with empty string and list values from a given object. /// /// Object to remove empty string fields from. - 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) { @@ -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); + } + } } } } diff --git a/src/WingetCreateCLI/Commands/NewCommand.cs b/src/WingetCreateCLI/Commands/NewCommand.cs index 169ff5e7..93670c72 100644 --- a/src/WingetCreateCLI/Commands/NewCommand.cs +++ b/src/WingetCreateCLI/Commands/NewCommand.cs @@ -255,7 +255,7 @@ public override async Task Execute() PromptManifestProperties(manifests); MergeNestedInstallerFilesIfApplicable(manifests.InstallerManifest); ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest); - RemoveEmptyStringFieldsInManifests(manifests); + RemoveEmptyStringAndListFieldsInManifests(manifests); DisplayManifestPreview(manifests); isManifestValid = ValidateManifestsInTempDir(manifests); } diff --git a/src/WingetCreateCLI/Commands/UpdateCommand.cs b/src/WingetCreateCLI/Commands/UpdateCommand.cs index d7f5bf0c..1c4926dd 100644 --- a/src/WingetCreateCLI/Commands/UpdateCommand.cs +++ b/src/WingetCreateCLI/Commands/UpdateCommand.cs @@ -263,7 +263,7 @@ await this.UpdateManifestsInteractively(initialManifests) : return false; } - RemoveEmptyStringFieldsInManifests(updatedManifests); + RemoveEmptyStringAndListFieldsInManifests(updatedManifests); ShiftInstallerFieldsToRootLevel(updatedManifests.InstallerManifest); DisplayManifestPreview(updatedManifests); diff --git a/src/WingetCreateTests/WingetCreateTests/Resources/TestPublisher.EmptyFields.yaml b/src/WingetCreateTests/WingetCreateTests/Resources/TestPublisher.EmptyFields.yaml index 30082d1c..17c22718 100644 --- a/src/WingetCreateTests/WingetCreateTests/Resources/TestPublisher.EmptyFields.yaml +++ b/src/WingetCreateTests/WingetCreateTests/Resources/TestPublisher.EmptyFields.yaml @@ -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 \ No newline at end of file +ManifestVersion: 1.6.0 \ No newline at end of file diff --git a/src/WingetCreateTests/WingetCreateTests/UnitTests/UpdateCommandTests.cs b/src/WingetCreateTests/WingetCreateTests/UnitTests/UpdateCommandTests.cs index 2e7d1924..9f516cf2 100644 --- a/src/WingetCreateTests/WingetCreateTests/UnitTests/UpdateCommandTests.cs +++ b/src/WingetCreateTests/WingetCreateTests/UnitTests/UpdateCommandTests.cs @@ -164,7 +164,7 @@ public async Task UpdateMultipleUrlManifests() } /// - /// 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. /// /// A representing the asynchronous unit test. [Test] @@ -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."); } ///