Skip to content

Commit

Permalink
also updated CLI app
Browse files Browse the repository at this point in the history
  • Loading branch information
veler committed May 10, 2024
1 parent 146f86b commit 6d49763
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
using System.Reflection;
using System.Resources;
using DevToys.Api;
using Microsoft.Extensions.Logging;

namespace DevToys.CLI;

internal sealed class CommandToICommandLineToolMap
internal sealed partial class CommandToICommandLineToolMap
{
private readonly ILogger _logger;

internal CommandToICommandLineToolMap(ICommandLineTool commandLineTool, CommandLineToolMetadata metadata)
{
Guard.IsNotNull(commandLineTool);
Guard.IsNotNull(metadata);

_logger = this.Log();

// Get the resource manager, if possible.
ResourceManager? resourceManager = GetResourceManager(commandLineTool, metadata);

Expand Down Expand Up @@ -61,13 +66,31 @@ internal CommandToICommandLineToolMap(ICommandLineTool commandLineTool, CommandL
return resourceManager;
}

private static string GetCommandDescription(ResourceManager? resourceManager, CommandLineToolMetadata metadata)
private string GetCommandDescription(ResourceManager? resourceManager, CommandLineToolMetadata metadata)
{
string? commandDescription
= resourceManager is not null && !string.IsNullOrWhiteSpace(metadata.DescriptionResourceName)
? resourceManager.GetString(metadata.DescriptionResourceName) ?? string.Empty
: string.Empty;
if (resourceManager is null)
{
LogGetMetadataStringFailed(metadata.DescriptionResourceName, metadata.InternalComponentName);
return $"[Unable to get the text for '{metadata.DescriptionResourceName}', " +
$"likely because we couldn't find a proper '{nameof(IResourceAssemblyIdentifier)}' " +
$"for the tool '{metadata.InternalComponentName}'.]";
}

return commandDescription;
try
{
string? commandDescription
= !string.IsNullOrWhiteSpace(metadata.DescriptionResourceName)
? resourceManager.GetString(metadata.DescriptionResourceName) ?? $"[Unable to find '{metadata.DescriptionResourceName}' in '{metadata.ResourceManagerBaseName}']"
: string.Empty;
return commandDescription;
}
catch
{
LogGetMetadataStringFailed(metadata.DescriptionResourceName, metadata.InternalComponentName);
return $"[Unable to find '{metadata.DescriptionResourceName}' in '{metadata.ResourceManagerBaseName}']";
}
}

[LoggerMessage(0, LogLevel.Error, "Unable to get the string for '{metadataName}' for the tool '{toolName}'.")]
partial void LogGetMetadataStringFailed(string metadataName, string toolName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using System.Resources;
using DevToys.Api;
using DevToys.CLI.Core;
using Microsoft.Extensions.Logging;

namespace DevToys.CLI;

internal sealed class OptionToICommandLineToolMap
internal sealed partial class OptionToICommandLineToolMap
{
private readonly ILogger _logger;
private static readonly Type optionType = typeof(Option<>);

private readonly ICommandLineTool _commandLineTool;
Expand All @@ -24,6 +26,7 @@ internal sealed class OptionToICommandLineToolMap
Guard.IsNotNull(property);
Guard.IsNotNull(commandLineOptionAttribute);

_logger = this.Log();
_property = property;
_commandLineTool = commandLineTool;

Expand Down Expand Up @@ -59,7 +62,7 @@ internal void SetPropertyValue(object? value)
_property.SetValue(_commandLineTool, value);
}

private static string? GetOptionDescription(ICommandLineTool commandLineTool, CommandLineOptionAttribute commandLineOptionAttribute, ResourceManager? parentResourceManager)
private string? GetOptionDescription(ICommandLineTool commandLineTool, CommandLineOptionAttribute commandLineOptionAttribute, ResourceManager? parentResourceManager)
{
string? optionDescription = null;
if (!string.IsNullOrWhiteSpace(commandLineOptionAttribute.DescriptionResourceName))
Expand All @@ -78,6 +81,12 @@ internal void SetPropertyValue(object? value)
{
optionDescription = optionResourceManager.GetString(commandLineOptionAttribute.DescriptionResourceName);
}

if (optionDescription is null)
{
LogGetMetadataStringFailed(commandLineOptionAttribute.DescriptionResourceName);
optionDescription = $"[Unable to find '{commandLineOptionAttribute.DescriptionResourceName}' in '{commandLineOptionAttribute.ResourceManagerBaseName}']";
}
}

return optionDescription;
Expand Down Expand Up @@ -223,4 +232,7 @@ internal void SetPropertyValue(object? value)
result.ErrorMessage = result.LocalizationResources.ArgumentConversionCannotParse(result.ToString(), typeof(OneOfOption));
return null;
}

[LoggerMessage(0, LogLevel.Error, "Unable to get the string for '{metadataName}'.")]
partial void LogGetMetadataStringFailed(string metadataName);
}
5 changes: 5 additions & 0 deletions src/app/dev/platforms/desktop/DevToys.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ private static void Main(string[] args)

LogAppShuttingDown(logger);
loggerFactory.Dispose();

if (Debugger.IsAttached)
{
Console.ReadKey();
}
}

private static async Task MainAsync(string[] args)
Expand Down

0 comments on commit 6d49763

Please sign in to comment.