Skip to content

Commit

Permalink
Add support for JSON serialization (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh authored Feb 16, 2024
1 parent b0d7e4c commit e741605
Show file tree
Hide file tree
Showing 55 changed files with 1,720 additions and 376 deletions.
1 change: 1 addition & 0 deletions doc/new-locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following arguments are available:
| **-l, --locale** | The package locale to create a new manifest for. If not provided, the tool will prompt you for this value.
| **-r, --reference-locale** | Existing locale manifest to be used as reference for default values. If not provided, the default locale manifest will be used.
| **-o, --out** | The output directory where the newly created manifests will be saved locally.
| **-f,--format** | Output format of the manifest. Default is "yaml". |
| **-t,--token** | GitHub personal access token used for direct submission to the Windows Package Manager repo |
| **-?, --help** | Gets additional help on this command |

Expand Down
1 change: 1 addition & 0 deletions doc/new.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following arguments are available:
| Argument | Description |
|--------------|-------------|
| **-o,--out** | The output directory where the newly created manifests will be saved locally |
| **-f,--format** | Output format of the manifest. Default is "yaml". |
| **-t,--token** | GitHub personal access token used for direct submission to the Windows Package Manager repo |
| **-?, --help** | Gets additional help on this command |

Expand Down
1 change: 1 addition & 0 deletions doc/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The following arguments are available:
| **-d, --defaultlocale-manifest** | Switch to display the default locale manifest.
| **-l, --locale-manifests** | Switch to display all locale manifests.
| **--version-manifest** | Switch to display the version manifest.
| **-f,--format** | Output format of the manifest. Default is "yaml". |
| **-t, --token** | GitHub personal access token used for authenticated access to the GitHub API. It is recommended to provide a token to get a higher [API rate limit](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting).
| **-?, --help** | Gets additional help on this command. |

Expand Down
1 change: 1 addition & 0 deletions doc/update-locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following arguments are available:
| **-v, --version** | The version of the package to update the locale for. Default is the latest version.
| **-l, --locale** | The package locale to update the manifest for. If not provided, the tool will prompt you a list of existing locales to choose from.
| **-o, --out** | The output directory where the newly created manifests will be saved locally.
| **-f,--format** | Output format of the manifest. Default is "yaml". |
| **-t,--token** | GitHub personal access token used for direct submission to the Windows Package Manager repo |
| **-?, --help** | Gets additional help on this command |

Expand Down
1 change: 1 addition & 0 deletions doc/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The following arguments are available:
| **-s, --submit** | Boolean value for submitting to the Windows Package Manager repo. If true, updated manifest will be submitted directly using the provided GitHub Token
| **-r, --replace** | Boolean value for replacing an existing manifest from the Windows Package Manager repo. Optionally provide a version or else the latest version will be replaced. Default is false.
| **-p, --prtitle** | The title of the pull request submitted to GitHub.
| **-f,--format** | Output format of the manifest. Default is "yaml". |
| **-t, --token** | GitHub personal access token used for direct submission to the Windows Package Manager repo. If no token is provided, tool will prompt for GitHub login credentials.
| **-?, --help** | Gets additional help on this command. |

Expand Down
63 changes: 41 additions & 22 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.WingetCreateCLI.Commands
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.WingetCreateCLI.Logging;
using Microsoft.WingetCreateCLI.Models.Settings;
using Microsoft.WingetCreateCLI.Properties;
using Microsoft.WingetCreateCLI.Telemetry;
using Microsoft.WingetCreateCLI.Telemetry.Events;
Expand Down Expand Up @@ -55,11 +56,21 @@ public abstract class BaseCommand
/// </summary>
private static readonly Dictionary<string, string> DownloadedInstallers = new();

/// <summary>
/// Gets the extension of the output manifest files.
/// </summary>
public static string Extension => Serialization.ManifestSerializer.AssociatedFileExtension;

/// <summary>
/// Gets or sets the GitHub token used to submit a pull request on behalf of the user.
/// </summary>
public virtual string GitHubToken { get; set; }

/// <summary>
/// Gets or sets the format of the output manifest files and preview.
/// </summary>
public virtual ManifestFormat Format { get; set; } = UserSettings.ManifestFormat;

/// <summary>
/// Gets or sets the winget repo owner to use.
/// </summary>
Expand Down Expand Up @@ -116,7 +127,6 @@ public async Task<bool> LoadGitHubClient(bool requireToken = false)
{
Logger.Trace("No token parameter, reading cached token");
this.GitHubToken = GitHubOAuth.ReadTokenCache();

if (string.IsNullOrEmpty(this.GitHubToken))
{
if (requireToken)
Expand Down Expand Up @@ -184,18 +194,18 @@ protected static string SaveManifestDirToLocalPath(
fullDirPath = Path.Combine(outputDir, manifestDir);
}

string versionManifestFileName = Manifests.GetFileName(manifests.VersionManifest);
string installerManifestFileName = Manifests.GetFileName(manifests.InstallerManifest);
string defaultLocaleManifestFileName = Manifests.GetFileName(manifests.DefaultLocaleManifest);
string versionManifestFileName = Manifests.GetFileName(manifests.VersionManifest, Extension);
string installerManifestFileName = Manifests.GetFileName(manifests.InstallerManifest, Extension);
string defaultLocaleManifestFileName = Manifests.GetFileName(manifests.DefaultLocaleManifest, Extension);

File.WriteAllText(Path.Combine(fullDirPath, versionManifestFileName), versionManifest.ToYaml());
File.WriteAllText(Path.Combine(fullDirPath, installerManifestFileName), installerManifest.ToYaml());
File.WriteAllText(Path.Combine(fullDirPath, defaultLocaleManifestFileName), defaultLocaleManifest.ToYaml());
File.WriteAllText(Path.Combine(fullDirPath, versionManifestFileName), versionManifest.ToManifestString());
File.WriteAllText(Path.Combine(fullDirPath, installerManifestFileName), installerManifest.ToManifestString());
File.WriteAllText(Path.Combine(fullDirPath, defaultLocaleManifestFileName), defaultLocaleManifest.ToManifestString());

foreach (LocaleManifest localeManifest in localeManifests)
{
string localeManifestFileName = Manifests.GetFileName(localeManifest);
File.WriteAllText(Path.Combine(fullDirPath, localeManifestFileName), localeManifest.ToYaml());
string localeManifestFileName = Manifests.GetFileName(localeManifest, Extension);
File.WriteAllText(Path.Combine(fullDirPath, localeManifestFileName), localeManifest.ToManifestString());
}

Console.WriteLine();
Expand All @@ -212,21 +222,21 @@ protected static string SaveManifestDirToLocalPath(
/// <returns>A boolean value indicating whether validation of the manifests was successful.</returns>
protected static bool ValidateManifestsInTempDir(Manifests manifests)
{
string versionManifestFileName = Manifests.GetFileName(manifests.VersionManifest);
string installerManifestFileName = Manifests.GetFileName(manifests.InstallerManifest);
string defaultLocaleManifestFileName = Manifests.GetFileName(manifests.DefaultLocaleManifest);
string versionManifestFileName = Manifests.GetFileName(manifests.VersionManifest, Extension);
string installerManifestFileName = Manifests.GetFileName(manifests.InstallerManifest, Extension);
string defaultLocaleManifestFileName = Manifests.GetFileName(manifests.DefaultLocaleManifest, Extension);

string randomDirPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(randomDirPath);

File.WriteAllText(Path.Combine(randomDirPath, versionManifestFileName), manifests.VersionManifest.ToYaml());
File.WriteAllText(Path.Combine(randomDirPath, installerManifestFileName), manifests.InstallerManifest.ToYaml());
File.WriteAllText(Path.Combine(randomDirPath, defaultLocaleManifestFileName), manifests.DefaultLocaleManifest.ToYaml());
File.WriteAllText(Path.Combine(randomDirPath, versionManifestFileName), manifests.VersionManifest.ToManifestString());
File.WriteAllText(Path.Combine(randomDirPath, installerManifestFileName), manifests.InstallerManifest.ToManifestString());
File.WriteAllText(Path.Combine(randomDirPath, defaultLocaleManifestFileName), manifests.DefaultLocaleManifest.ToManifestString());

foreach (LocaleManifest localeManifest in manifests.LocaleManifests)
{
string localeManifestFileName = Manifests.GetFileName(localeManifest);
File.WriteAllText(Path.Combine(randomDirPath, localeManifestFileName), localeManifest.ToYaml());
string localeManifestFileName = Manifests.GetFileName(localeManifest, Extension);
File.WriteAllText(Path.Combine(randomDirPath, localeManifestFileName), localeManifest.ToManifestString());
}

bool result = ValidateManifest(randomDirPath);
Expand Down Expand Up @@ -403,11 +413,11 @@ protected static void DisplayManifestPreview(Manifests manifests)
{
Logger.Debug(Resources.GenerateManifestPreview_Message);
Logger.Info(Resources.VersionManifestPreview_Message);
Console.WriteLine(manifests.VersionManifest.ToYaml());
Console.WriteLine(manifests.VersionManifest.ToManifestString());
Logger.Info(Resources.InstallerManifestPreview_Message);
Console.WriteLine(manifests.InstallerManifest.ToYaml());
Console.WriteLine(manifests.InstallerManifest.ToManifestString());
Logger.Info(Resources.DefaultLocaleManifestPreview_Message);
Console.WriteLine(manifests.DefaultLocaleManifest.ToYaml());
Console.WriteLine(manifests.DefaultLocaleManifest.ToManifestString());
}

/// <summary>
Expand Down Expand Up @@ -569,7 +579,7 @@ protected static void PromptOptionalProperties<T>(T manifest, List<string> optio
protected static void DisplayDefaultLocaleManifest(DefaultLocaleManifest defaultLocaleManifest)
{
Logger.InfoLocalized(nameof(Resources.DefaultLocaleManifest_Message), defaultLocaleManifest.PackageLocale);
Console.WriteLine(defaultLocaleManifest.ToYaml(true));
Console.WriteLine(defaultLocaleManifest.ToManifestString(true));
}

/// <summary>
Expand All @@ -581,7 +591,7 @@ protected static void DisplayLocaleManifests(List<LocaleManifest> localeManifest
foreach (var localeManifest in localeManifests)
{
Logger.InfoLocalized(nameof(Resources.LocaleManifest_Message), localeManifest.PackageLocale);
Console.WriteLine(localeManifest.ToYaml(true));
Console.WriteLine(localeManifest.ToManifestString(true));
}
}

Expand Down Expand Up @@ -710,6 +720,15 @@ protected async Task<bool> CheckGitHubTokenAndSetClient()
/// <returns>A <see cref="Task"/> representing the success of the asynchronous operation.</returns>
protected async Task<bool> GitHubSubmitManifests(Manifests manifests, string prTitle = null, bool shouldReplace = false, string replaceVersion = null)
{
// Community repo only supports yaml submissions.
if (this.WingetRepo == DefaultWingetRepo &&
this.WingetRepoOwner == DefaultWingetRepoOwner &&
this.Format != ManifestFormat.Yaml)
{
Logger.ErrorLocalized(nameof(Resources.FormatNotSupportedForDefaultRepo_Error));
return false;
}

if (string.IsNullOrEmpty(this.GitHubToken))
{
Logger.WarnLocalized(nameof(Resources.NoTokenProvided_Message));
Expand Down
8 changes: 7 additions & 1 deletion src/WingetCreateCLI/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.WingetCreateCLI.Commands
using CommandLine;
using CommandLine.Text;
using Microsoft.WingetCreateCLI.Logging;
using Microsoft.WingetCreateCLI.Models.Settings;
using Microsoft.WingetCreateCLI.Properties;
using Microsoft.WingetCreateCLI.Telemetry;
using Microsoft.WingetCreateCLI.Telemetry.Events;
Expand All @@ -24,7 +25,6 @@ namespace Microsoft.WingetCreateCLI.Commands
using Microsoft.WingetCreateCore.Models.DefaultLocale;
using Microsoft.WingetCreateCore.Models.Installer;
using Microsoft.WingetCreateCore.Models.Version;
using Newtonsoft.Json;
using Sharprompt;

/// <summary>
Expand Down Expand Up @@ -74,6 +74,12 @@ public static IEnumerable<Example> Examples
[Option('o', "out", Required = false, HelpText = "OutputDirectory_HelpText", ResourceType = typeof(Resources))]
public string OutputDir { get; set; }

/// <summary>
/// Gets or sets the format of the output manifest files.
/// </summary>
[Option('f', "format", Required = false, HelpText = "ManifestFormat_HelpText", ResourceType = typeof(Resources))]
public override ManifestFormat Format { get => base.Format; set => base.Format = value; }

/// <summary>
/// Gets or sets the GitHub token used to submit a pull request on behalf of the user.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/WingetCreateCLI/Commands/NewLocaleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.WingetCreateCLI.Commands
using CommandLine;
using CommandLine.Text;
using Microsoft.WingetCreateCLI.Logging;
using Microsoft.WingetCreateCLI.Models.Settings;
using Microsoft.WingetCreateCLI.Properties;
using Microsoft.WingetCreateCLI.Telemetry;
using Microsoft.WingetCreateCLI.Telemetry.Events;
Expand Down Expand Up @@ -85,6 +86,12 @@ public static IEnumerable<Example> Examples
[Option('o', "out", Required = false, HelpText = "OutputDirectory_HelpText", ResourceType = typeof(Resources))]
public string OutputDir { get; set; }

/// <summary>
/// Gets or sets the format of the output manifest files.
/// </summary>
[Option('f', "format", Required = false, HelpText = "ManifestFormat_HelpText", ResourceType = typeof(Resources))]
public override ManifestFormat Format { get => base.Format; set => base.Format = value; }

/// <summary>
/// Gets or sets the GitHub token used to submit a pull request on behalf of the user.
/// </summary>
Expand Down Expand Up @@ -329,7 +336,7 @@ private void DisplayGeneratedLocales(List<LocaleManifest> newLocales)
foreach (var localeManifest in newLocales)
{
Logger.InfoLocalized(nameof(Resources.LocaleManifest_Message), localeManifest.PackageLocale);
Console.WriteLine(localeManifest.ToYaml(true));
Console.WriteLine(localeManifest.ToManifestString(true));
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/WingetCreateCLI/Commands/ShowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ namespace Microsoft.WingetCreateCLI.Commands
using CommandLine;
using CommandLine.Text;
using Microsoft.WingetCreateCLI.Logging;
using Microsoft.WingetCreateCLI.Models.Settings;
using Microsoft.WingetCreateCLI.Properties;
using Microsoft.WingetCreateCLI.Telemetry;
using Microsoft.WingetCreateCLI.Telemetry.Events;
using Microsoft.WingetCreateCore;
using Microsoft.WingetCreateCore.Models;
using Microsoft.WingetCreateCore.Models.DefaultLocale;
using Microsoft.WingetCreateCore.Models.Installer;
using Microsoft.WingetCreateCore.Models.Locale;
using Microsoft.WingetCreateCore.Models.Singleton;
using Microsoft.WingetCreateCore.Models.Version;

Expand Down Expand Up @@ -76,6 +75,12 @@ public static IEnumerable<Example> Examples
[Option("version-manifest", Required = false, HelpText = "VersionManifest_HelpText", ResourceType = typeof(Resources))]
public bool ShowVersionManifest { get; set; }

/// <summary>
/// Gets or sets the format of the output manifest preview.
/// </summary>
[Option('f', "format", Required = false, HelpText = "ManifestFormat_HelpText", ResourceType = typeof(Resources))]
public override ManifestFormat Format { get => base.Format; set => base.Format = value; }

/// <summary>
/// Gets or sets the GitHub token for authenticated access to GitHub API.
/// </summary>
Expand Down Expand Up @@ -143,19 +148,19 @@ private static void ShowAllManifests(Manifests manifests)
private static void DisplayInstallerManifest(InstallerManifest installerManifest)
{
Logger.InfoLocalized(nameof(Resources.InstallerManifest_Message));
Console.WriteLine(installerManifest.ToYaml(true));
Console.WriteLine(installerManifest.ToManifestString(true));
}

private static void DisplayVersionManifest(VersionManifest versionManifest)
{
Logger.InfoLocalized(nameof(Resources.VersionManifest_Message));
Console.WriteLine(versionManifest.ToYaml(true));
Console.WriteLine(versionManifest.ToManifestString(true));
}

private static void DisplaySingletonManifest(SingletonManifest singletonManifest)
{
Logger.InfoLocalized(nameof(Resources.SingletonManifest_Message));
Console.WriteLine(singletonManifest.ToYaml(true));
Console.WriteLine(singletonManifest.ToManifestString(true));
}

private void ParseArgumentsAndShowManifest(Manifests manifests)
Expand Down
Loading

0 comments on commit e741605

Please sign in to comment.