Skip to content

Commit 94c5d21

Browse files
authored
Remove empty list fields from the manifest (#563)
1 parent 786625f commit 94c5d21

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

src/WingetCreateCLI/Commands/BaseCommand.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,19 +424,19 @@ protected static void DisplayManifestPreview(Manifests manifests)
424424
/// Removes fields with empty string values from all manifests.
425425
/// </summary>
426426
/// <param name="manifests">Wrapper object containing the manifest object models.</param>
427-
protected static void RemoveEmptyStringFieldsInManifests(Manifests manifests)
427+
protected static void RemoveEmptyStringAndListFieldsInManifests(Manifests manifests)
428428
{
429-
RemoveEmptyStringFields(manifests.InstallerManifest);
430-
RemoveEmptyStringFields(manifests.DefaultLocaleManifest);
429+
RemoveEmptyStringAndListFields(manifests.InstallerManifest);
430+
RemoveEmptyStringAndListFields(manifests.DefaultLocaleManifest);
431431

432432
foreach (var localeManifest in manifests.LocaleManifests)
433433
{
434-
RemoveEmptyStringFields(localeManifest);
434+
RemoveEmptyStringAndListFields(localeManifest);
435435
}
436436

437437
foreach (var installer in manifests.InstallerManifest.Installers)
438438
{
439-
RemoveEmptyStringFields(installer);
439+
RemoveEmptyStringAndListFields(installer);
440440
}
441441
}
442442

@@ -824,13 +824,16 @@ protected string GetPRTitle(Manifests currentManifest, Manifests repositoryManif
824824
}
825825

826826
/// <summary>
827-
/// Removes fields with empty string values from a given object.
827+
/// Removes fields with empty string and list values from a given object.
828828
/// </summary>
829829
/// <param name="obj">Object to remove empty string fields from.</param>
830-
private static void RemoveEmptyStringFields(object obj)
830+
private static void RemoveEmptyStringAndListFields(object obj)
831831
{
832832
var stringProperties = obj.GetType().GetProperties()
833833
.Where(p => p.PropertyType == typeof(string));
834+
var listProperties = obj.GetType().GetProperties()
835+
.Where(p => p.PropertyType.IsGenericType &&
836+
p.PropertyType.GetGenericTypeDefinition() == typeof(List<>));
834837

835838
foreach (var prop in stringProperties)
836839
{
@@ -839,6 +842,14 @@ private static void RemoveEmptyStringFields(object obj)
839842
prop.SetValue(obj, null);
840843
}
841844
}
845+
846+
foreach (var prop in listProperties)
847+
{
848+
if (prop.GetValue(obj) is IList list && list.Count == 0)
849+
{
850+
prop.SetValue(obj, null);
851+
}
852+
}
842853
}
843854
}
844855
}

src/WingetCreateCLI/Commands/NewCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public override async Task<bool> Execute()
255255
PromptManifestProperties(manifests);
256256
MergeNestedInstallerFilesIfApplicable(manifests.InstallerManifest);
257257
ShiftInstallerFieldsToRootLevel(manifests.InstallerManifest);
258-
RemoveEmptyStringFieldsInManifests(manifests);
258+
RemoveEmptyStringAndListFieldsInManifests(manifests);
259259
DisplayManifestPreview(manifests);
260260
isManifestValid = ValidateManifestsInTempDir(manifests);
261261
}

src/WingetCreateCLI/Commands/UpdateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ await this.UpdateManifestsInteractively(initialManifests) :
263263
return false;
264264
}
265265

266-
RemoveEmptyStringFieldsInManifests(updatedManifests);
266+
RemoveEmptyStringAndListFieldsInManifests(updatedManifests);
267267
ShiftInstallerFieldsToRootLevel(updatedManifests.InstallerManifest);
268268
DisplayManifestPreview(updatedManifests);
269269

src/WingetCreateTests/WingetCreateTests/Resources/TestPublisher.EmptyFields.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ Installers:
1414
PackageFamilyName: ''
1515
PrivacyUrl: ''
1616
Author: ''
17+
Platform: []
18+
InstallModes: []
19+
FileExtensions: []
20+
Commands: []
21+
AppsAndFeaturesEntries: []
22+
Protocols: []
23+
Capabilities: []
24+
UnsupportedArguments: []
25+
UnsupportedOSArchitectures: []
26+
RestrictedCapabilities: []
27+
NestedInstallerFiles: []
28+
ExpectedReturnCodes: []
29+
Tags: []
30+
Agreements: []
31+
Documentations: []
32+
Icons: []
1733
PackageLocale: en-US
1834
ManifestType: singleton
19-
ManifestVersion: 1.0.0
35+
ManifestVersion: 1.6.0

src/WingetCreateTests/WingetCreateTests/UnitTests/UpdateCommandTests.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public async Task UpdateMultipleUrlManifests()
164164
}
165165

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

183183
Manifests updatedManifests = Serialization.DeserializeManifestContents(updatedManifestContents);
184+
185+
// Empty string fields are removed
184186
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.PrivacyUrl, "PrivacyUrl should be null.");
185187
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Author, "Author should be null.");
186-
187188
var firstInstaller = updatedManifests.InstallerManifest.Installers.First();
188189
ClassicAssert.IsNull(firstInstaller.ProductCode, "ProductCode should be null.");
189190
ClassicAssert.IsNull(firstInstaller.PackageFamilyName, "ProductCode should be null.");
191+
192+
// Empty list fields are removed
193+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Platform, "Platform should be null.");
194+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.InstallModes, "InstallModes should be null.");
195+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.FileExtensions, "FileExtensions should be null.");
196+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Commands, "Commands should be null.");
197+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.AppsAndFeaturesEntries, "AppsAndFeaturesEntries should be null.");
198+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Protocols, "Protocols should be null.");
199+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.Capabilities, "Capabilities should be null.");
200+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.UnsupportedArguments, "UnsupportedArguments should be null.");
201+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.UnsupportedOSArchitectures, "UnsupportedOSArchitectures should be null.");
202+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.RestrictedCapabilities, "RestrictedCapabilities should be null.");
203+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.NestedInstallerFiles, "NestedInstallerFiles should be null.");
204+
ClassicAssert.IsNull(updatedManifests.InstallerManifest.ExpectedReturnCodes, "ExpectedReturnCodes should be null.");
205+
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Tags, "Tags should be null.");
206+
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Agreements, "Agreements should be null.");
207+
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Documentations, "Documentations should be null.");
208+
ClassicAssert.IsNull(updatedManifests.DefaultLocaleManifest.Icons, "Icons should be null.");
190209
}
191210

192211
/// <summary>

0 commit comments

Comments
 (0)