Skip to content

Commit

Permalink
Show command for displaying manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh committed Aug 16, 2023
1 parent 44a102a commit 98cbed9
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 15 deletions.
32 changes: 32 additions & 0 deletions doc/show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# show command (Winget-Create)

The **show** command of the [Winget-Create](../README.md) tool is designed to show an existing manifest from the [Windows Package Manager repo](https://docs.microsoft.com/windows/package-manager/). This command is particularly useful when you need to update an existing package and wish to examine an older manifest without leaving the CLI. This helps you determine the count of existing installer nodes and decide if architecture or scope overrides are necessary for the update.

## Usage

Show the latest manifest of an existing package from the Windows Package Manager repo:

`wingetcreate.exe show <PackageIdentifier> --token <GitHubPersonalAccessToken>`

Show a specified version of a package manifest:

`wingetcreate.exe show <PackageIdentifier> --version <PackageVersion> --token <GitHubPersonalAccessToken>`

Show only the installer and default locale manifest:

`wingetcreate.exe show <PackageIdentifier> --installer-manifest --defaultlocale-manifest --token <GitHubPersonalAccessToken>`

## Arguments

The following arguments are available:

| Argument | Description |
|--------------|-------------|
| **-v, --version** | The version of the existing package manifest from the Windows Package Manager Repo to retrieve the manifest for. Default is the latest version.
| **--installer-manifest** | Switch to display the installer manifest.
| **--defaultlocale-manifest** | Switch to display the default locale manifest.
| **--locale-manifests** | Switch to display all locale manifests.
| **--version-manifests** | Switch to display the version manifest.
| **-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).

Instructions on setting up GitHub Token for Winget-Create can be found [here](../README.md#github-personal-access-token-classic-permissions).
3 changes: 3 additions & 0 deletions doc/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The **update** command of the [Winget-Create](../README.md) tool is designed to

The **update** command can be called with the installer URL(s) that you wish to update the manifest with. **Please make sure that the number of installer URL(s) included matches the number of existing installer nodes in the manifest you are updating. Otherwise, the command will fail.** This is to ensure that we can deterministically update each installer node with the correct matching installer url provided.

> **Note**\
> The [show](show.md) command can be used to quickly view an existing manifest from the packages repository.
### *How does Winget-Create know which installer(s) to match when executing an update?*

[Winget-Create](../README.md) will attempt to match installers based on the installer architecture and installer type. The installer type will always be derived from downloading and analyzing the installer package.
Expand Down
212 changes: 212 additions & 0 deletions src/WingetCreateCLI/Commands/ShowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.

namespace Microsoft.WingetCreateCLI.Commands
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommandLine;
using CommandLine.Text;
using Microsoft.WingetCreateCLI.Logging;
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;

/// <summary>
/// Show command to display manifest from the packages repository.
/// </summary>
[Verb("show", HelpText = "ShowCommand_HelpText", ResourceType = typeof(Resources))]
public class ShowCommand : BaseCommand
{
/// <summary>
/// Gets the usage examples for the update command.
/// </summary>
[Usage(ApplicationAlias = ProgramApplicationAlias)]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example(Resources.Example_ShowCommand_DisplayLatestManifest, new ShowCommand { Id = "<PackageIdentifier>" });
yield return new Example(Resources.Example_ShowCommand_DisplaySpecifiedVersion, new ShowCommand { Id = "<PackageIdentifier>", Version = "<Version>", GitHubToken = "<GitHubPersonalAccessToken>" });
yield return new Example(Resources.Example_ShowCommand_ShowInstallerAndDefaultLocale, new ShowCommand { Id = "<PackageIdentifier>", ShowInstallerManifest = true, ShowDefaultLocaleManifest = true, GitHubToken = "<GitHubPersonalAccessToken>" });
}
}

/// <summary>
/// Gets or sets the id used for looking up an existing manifest in the Windows Package Manager repository.
/// </summary>
[Value(0, MetaName = "PackageIdentifier", Required = true, HelpText = "PackageIdentifier_HelpText", ResourceType = typeof(Resources))]
public string Id { get; set; }

/// <summary>
/// Gets or sets the version of the package from the Windows Package Manager repository to display the manifest for.
/// </summary>
[Option('v', "version", Required = false, Default = null, HelpText = "ShowCommand_Version_HelpText", ResourceType = typeof(Resources))]
public string Version { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show the installer manifest.
/// </summary>
[Option("installer-manifest", Required = false, HelpText = "InstallerManifest_HelpText", ResourceType = typeof(Resources))]
public bool ShowInstallerManifest { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show the default locale manifest.
/// </summary>
[Option("defaultlocale-manifest", Required = false, HelpText = "DefaultLocaleManifest_HelpText", ResourceType = typeof(Resources))]
public bool ShowDefaultLocaleManifest { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show the locale manifests.
/// </summary>
[Option("locale-manifests", Required = false, HelpText = "LocaleManifests_HelpText", ResourceType = typeof(Resources))]
public bool ShowLocaleManifests { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show the version manifest.
/// </summary>
[Option("version-manifest", Required = false, HelpText = "VersionManifest_HelpText", ResourceType = typeof(Resources))]
public bool ShowVersionManifest { get; set; }

/// <summary>
/// Gets or sets the GitHub token for authenticated access to GitHub API.
/// </summary>
[Option('t', "token", Required = false, HelpText = "ShowCommand_GitHubToken_HelpText", ResourceType = typeof(Resources))]
public override string GitHubToken { get => base.GitHubToken; set => base.GitHubToken = value; }

/// <summary>
/// Executes the show command flow.
/// </summary>
/// <returns>Boolean representing success or fail of the command.</returns>
public override async Task<bool> Execute()
{
CommandExecutedEvent commandEvent = new CommandExecutedEvent
{
Id = this.Id,
Version = this.Version,
HasGitHubToken = !string.IsNullOrEmpty(this.GitHubToken),
};

Console.OutputEncoding = System.Text.Encoding.UTF8;
string exactId;
try
{
exactId = await this.GitHubClient.FindPackageId(this.Id);
}
catch (Octokit.RateLimitExceededException)
{
Logger.ErrorLocalized(nameof(Resources.RateLimitExceeded_Message));
return false;
}

if (!string.IsNullOrEmpty(exactId))
{
this.Id = exactId;
}

List<string> latestManifestContent;

try
{
latestManifestContent = await this.GitHubClient.GetManifestContentAsync(this.Id, this.Version);
Manifests originalManifests = Serialization.DeserializeManifestContents(latestManifestContent);
this.ParseArgumentsAndShowManifest(originalManifests);
return await Task.FromResult(commandEvent.IsSuccessful = true);
}
catch (Octokit.NotFoundException e)
{
Logger.ErrorLocalized(nameof(Resources.Error_Prefix), e.Message);
Logger.ErrorLocalized(nameof(Resources.OctokitNotFound_Error));
return false;
}
finally
{
TelemetryManager.Log.WriteEvent(commandEvent);
}
}

private static void ShowAllManifests(Manifests manifests)
{
DisplayInstallerManifest(manifests.InstallerManifest);
DisplayDefaultLocaleManifest(manifests.DefaultLocaleManifest);
DisplayLocaleManifests(manifests.LocaleManifests);
DisplayVersionManifest(manifests.VersionManifest);
}

private static void DisplayInstallerManifest(InstallerManifest installerManifest)
{
Logger.InfoLocalized(nameof(Resources.InstallerManifest_Message));
Console.WriteLine(installerManifest.ToYaml(true));
}

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

private static void DisplayDefaultLocaleManifest(DefaultLocaleManifest defaultLocaleManifest)
{
Logger.InfoLocalized(nameof(Resources.DefaultLocaleManifest_Message), defaultLocaleManifest.PackageLocale);
Console.WriteLine(defaultLocaleManifest.ToYaml(true));
}

private static void DisplayLocaleManifests(List<LocaleManifest> localeManifests)
{
foreach (var localeManifest in localeManifests)
{
Logger.InfoLocalized(nameof(Resources.LocaleManifest_Message), localeManifest.PackageLocale);
Console.WriteLine(localeManifest.ToYaml(true));
}
}

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

private void ParseArgumentsAndShowManifest(Manifests manifests)
{
if (manifests.SingletonManifest != null)
{
DisplaySingletonManifest(manifests.SingletonManifest);
return;
}

bool showAll = !this.ShowInstallerManifest && !this.ShowDefaultLocaleManifest && !this.ShowLocaleManifests && !this.ShowVersionManifest;
if (this.ShowInstallerManifest)
{
DisplayInstallerManifest(manifests.InstallerManifest);
}

if (this.ShowDefaultLocaleManifest || this.ShowLocaleManifests)
{
DisplayDefaultLocaleManifest(manifests.DefaultLocaleManifest);
}

if (this.ShowLocaleManifests)
{
DisplayLocaleManifests(manifests.LocaleManifests);
}

if (this.ShowVersionManifest)
{
DisplayVersionManifest(manifests.VersionManifest);
}

if (showAll)
{
ShowAllManifests(manifests);
}
}
}
}
2 changes: 1 addition & 1 deletion src/WingetCreateCLI/Commands/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public override async Task<bool> Execute()

try
{
latestManifestContent = await this.GitHubClient.GetLatestManifestContentAsync(this.Id);
latestManifestContent = await this.GitHubClient.GetManifestContentAsync(this.Id);
}
catch (Octokit.NotFoundException e)
{
Expand Down
7 changes: 6 additions & 1 deletion src/WingetCreateCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ private static async Task<int> Main(string[] args)
config.CaseSensitive = false;
});

var types = new Type[] { typeof(NewCommand), typeof(UpdateCommand), typeof(SubmitCommand), typeof(SettingsCommand), typeof(TokenCommand), typeof(CacheCommand) };
var types = new Type[]
{
typeof(NewCommand), typeof(UpdateCommand), typeof(SubmitCommand),
typeof(SettingsCommand), typeof(TokenCommand), typeof(CacheCommand),
typeof(ShowCommand),
};
var parserResult = myParser.ParseArguments(args, types);

BaseCommand command = parserResult.MapResult(c => c as BaseCommand, err => null);
Expand Down
Loading

0 comments on commit 98cbed9

Please sign in to comment.