Skip to content

Commit

Permalink
Remove empty fields from manifest in new and update command (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft authored Dec 6, 2021
1 parent 073d4f8 commit 7742e43
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ protected static void DisplayManifestPreview(Manifests manifests)
Console.WriteLine(manifests.DefaultLocaleManifest.ToYaml());
}

/// <summary>
/// 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)
{
RemoveEmptyStringFields(manifests.InstallerManifest);
RemoveEmptyStringFields(manifests.DefaultLocaleManifest);

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

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

/// <summary>
/// Launches the GitHub OAuth flow and obtains a GitHub token.
/// </summary>
Expand Down Expand Up @@ -494,5 +514,23 @@ protected async Task<bool> GitHubSubmitManifests(Manifests manifests)

return true;
}

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

foreach (var prop in stringProperties)
{
if ((string)prop.GetValue(obj) == string.Empty)
{
prop.SetValue(obj, null);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/WingetCreateCLI/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public override async Task<bool> Execute()
}

PromptPropertiesAndDisplayManifests(manifests);
RemoveEmptyStringFieldsInManifests(manifests);
isManifestValid = ValidateManifestsInTempDir(manifests);
}
while (Prompt.Confirm(Resources.ConfirmManifestCreation_Message));
Expand Down
1 change: 1 addition & 0 deletions src/WingetCreateCLI/Commands/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ await this.UpdateManifestsInteractively(initialManifests) :
return false;
}

RemoveEmptyStringFieldsInManifests(updatedManifests);
DisplayManifestPreview(updatedManifests);

if (string.IsNullOrEmpty(this.OutputDir))
Expand Down
8 changes: 6 additions & 2 deletions src/WingetCreateCore/Models/InstallerManifestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti
public partial class Installer
{
[Newtonsoft.Json.JsonProperty("InstallerLocale", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(20, MinimumLength = 1)]
[System.ComponentModel.DataAnnotations.StringLength(20)]
[System.ComponentModel.DataAnnotations.RegularExpression(@"^([a-zA-Z]{2,3}|[iI]-[a-zA-Z]+|[xX]-[a-zA-Z]{1,8})(-[a-zA-Z]{1,8})*$")]
public string InstallerLocale { get; set; }

[Newtonsoft.Json.JsonProperty("Platform", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
Expand Down Expand Up @@ -237,6 +238,7 @@ public partial class Installer

[Newtonsoft.Json.JsonProperty("ProductCode", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(255, MinimumLength = 1)]
[System.ComponentModel.DataAnnotations.RegularExpression(@"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$")]
public string ProductCode { get; set; }

[Newtonsoft.Json.JsonProperty("Capabilities", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
Expand Down Expand Up @@ -280,7 +282,8 @@ public partial class InstallerManifest
public string Channel { get; set; }

[Newtonsoft.Json.JsonProperty("InstallerLocale", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(20, MinimumLength = 1)]
[System.ComponentModel.DataAnnotations.StringLength(20)]
[System.ComponentModel.DataAnnotations.RegularExpression(@"^([a-zA-Z]{2,3}|[iI]-[a-zA-Z]+|[xX]-[a-zA-Z]{1,8})(-[a-zA-Z]{1,8})*$")]
public string InstallerLocale { get; set; }

[Newtonsoft.Json.JsonProperty("Platform", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
Expand Down Expand Up @@ -336,6 +339,7 @@ public partial class InstallerManifest

[Newtonsoft.Json.JsonProperty("ProductCode", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.StringLength(255, MinimumLength = 1)]
[System.ComponentModel.DataAnnotations.RegularExpression(@"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$")]
public string ProductCode { get; set; }

[Newtonsoft.Json.JsonProperty("Capabilities", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PackageIdentifier: TestPublisher.EmptyFields
PackageVersion: 0.1.2
PackageName: Empty fields test
Publisher: Test publisher
License: MIT
ShortDescription: A manifest used to test if empty fields are removed from the manifest.
InstallerLocale: en-US
Installers:
- Architecture: x64
InstallerUrl: https://fakedomain.com/WingetCreateTestExeInstaller.exe
InstallerType: exe
InstallerSha256: A7803233EEDB6A4B59B3024CCF9292A6FFFB94507DC998AA67C5B745D197A5DC
ProductCode: ''
PackageFamilyName: ''
PrivacyUrl: ''
Author: ''
PackageLocale: en-US
ManifestType: singleton
ManifestVersion: 1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ 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.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Test]
public async Task UpdateRemovesEmptyFields()
{
string packageId = "TestPublisher.EmptyFields";
string version = "1.2.3.4";
TestUtils.InitializeMockDownloads(TestConstants.TestExeInstaller);
(UpdateCommand command, var initialManifestContent) = GetUpdateCommandAndManifestData(packageId, version, this.tempPath, null);
bool updateExecuted = await command.ExecuteManifestUpdate(initialManifestContent, this.testCommandEvent);
Assert.IsTrue(updateExecuted, "Command should have succeeded");
string manifestDir = Utils.GetAppManifestDirPath(packageId, version);
var updatedManifestContents = Directory.GetFiles(Path.Combine(this.tempPath, manifestDir)).Select(f => File.ReadAllText(f));
Assert.IsTrue(updatedManifestContents.Any(), "Updated manifests were not created successfully");

Manifests updatedManifests = Serialization.DeserializeManifestContents(updatedManifestContents);
Assert.IsNull(updatedManifests.DefaultLocaleManifest.PrivacyUrl, "PrivacyUrl should be null.");
Assert.IsNull(updatedManifests.DefaultLocaleManifest.Author, "Author should be null.");

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

/// <summary>
/// Verify that update command fails if there is a discrepency in the URL count.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<None Update="Resources\Multifile.MsixTest\Multifile.MsixTest.locale.en-US.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\TestPublisher.EmptyFields.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\TestPublisher.MultipleArchitectureOverride.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 7742e43

Please sign in to comment.