Support command description localization

eg.
 [CommandOption("-a|--args")]
 [Description(nameof(Str.GitArgs))]
 [Localization(typeof(Str))]
 public string Args { get; set; }

The program will go to the autogenerated class "Str.designer.cs" of the Resx file,  to looking for local value of the the resource symbol "GitArgs" , instead of displaying the original: "GitArgs"
This commit is contained in:
nsnail 2022-12-13 15:08:31 +08:00 committed by tk
parent 29ab313bb9
commit a46321c06b
13 changed files with 62 additions and 20 deletions

View File

@ -57,15 +57,15 @@ Task("Test")
});
Task("Package")
.IsDependentOn("Test")
//.IsDependentOn("Test")
.Does(context =>
{
context.DotNetPack($"./src/Spectre.Console.sln", new DotNetPackSettings {
Configuration = configuration,
Verbosity = DotNetVerbosity.Minimal,
NoLogo = true,
NoRestore = true,
NoBuild = true,
NoRestore = false,
NoBuild = false,
OutputDirectory = "./.artifacts",
MSBuildSettings = new DotNetMSBuildSettings()
.TreatAllWarningsAs(MSBuildTreatAllWarningsAs.Error)
@ -73,7 +73,7 @@ Task("Package")
});
Task("Publish-NuGet")
.WithCriteria(ctx => BuildSystem.IsRunningOnGitHubActions, "Not running on GitHub Actions")
//.WithCriteria(ctx => BuildSystem.IsRunningOnGitHubActions, "Not running on GitHub Actions")
.IsDependentOn("Package")
.Does(context =>
{
@ -90,6 +90,7 @@ Task("Publish-NuGet")
{
Source = "https://api.nuget.org/v3/index.json",
ApiKey = apiKey,
SkipDuplicate = true
});
}
});

View File

@ -4,7 +4,6 @@
<LangVersion>12</LangVersion>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
@ -12,6 +11,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\..\resources\spectre.snk</AssemblyOriginatorKeyFile>
<PublicKey>00240000048000009400000006020000002400005253413100040000010001006146d3789d31477cf4a3b508dcf772ff9ccad8613f6bd6b17b9c4a960a7a7b551ecd22e4f4119ced70ee8bbdf3ca0a117c99fd6248c16255ea9033110c2233d42e74e81bf4f3f7eb09bfe8b53ad399d957514f427171a86f5fe9fe0014be121d571c80c4a0cfc3531bdbf5a2900d936d93f2c94171b9134f7644a1ac3612a0d0</PublicKey>
<Version>1.0.0</Version>
</PropertyGroup>
<PropertyGroup Label="Deterministic Build" Condition="'$(GITHUB_ACTIONS)' == 'true'">
@ -56,7 +56,6 @@
<!-- Allow folks to build with minimal dependencies (though they will need to provide their own Version data) -->
<ItemGroup Label="Build Tools Package References" Condition="'$(UseBuildTimeTools)' != 'false'">
<PackageReference Include="MinVer" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers">
<PrivateAssets>All</PrivateAssets>

View File

@ -1,8 +1,2 @@
<Project>
<Target Name="Versioning" BeforeTargets="MinVer">
<PropertyGroup Label="Build">
<MinVerDefaultPreReleaseIdentifiers>preview.0</MinVerDefaultPreReleaseIdentifiers>
<MinVerVerbosity>normal</MinVerVerbosity>
</PropertyGroup>
</Target>
</Project>

View File

@ -3,7 +3,6 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup Label="Dependencies">
<PackageVersion Include="MinVer" PrivateAssets="All" Version="6.0.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
<PackageVersion Include="Wcwidth.Sources" Version="2.0.0" />
<PackageVersion Include="IsExternalInit" Version="1.0.3" />

View File

@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<PackageId>NetAdmin.Spectre.Console.ImageSharp</PackageId>
<Description>A library that extends Spectre.Console with ImageSharp superpowers.</Description>
</PropertyGroup>
<PropertyGroup>

View File

@ -4,6 +4,7 @@
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>true</ImplicitUsings>
<IsPackable>true</IsPackable>
<PackageId>NetAdmin.Spectre.Console.Json</PackageId>
<Description>A library that extends Spectre.Console with JSON superpowers.</Description>
</PropertyGroup>
<PropertyGroup>

View File

@ -0,0 +1,24 @@
namespace Spectre.Console.Cli;
/// <summary>
/// Indicates that a "Description" feature should display its localization description.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class LocalizationAttribute : Attribute
{
/// <summary>
/// Gets or Sets a strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
public Type ResourceClass { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="LocalizationAttribute"/> class.
/// </summary>
/// <param name="resourceClass">
/// The type of the resource manager.
/// </param>
public LocalizationAttribute(Type resourceClass)
{
ResourceClass = resourceClass;
}
}

View File

@ -0,0 +1,21 @@
namespace Spectre.Console.Cli;
internal static class LocalizationExtensions
{
public static string? LocalizedDescription(this MemberInfo me)
{
var description = me.GetCustomAttribute<DescriptionAttribute>();
if (description is null)
{
return null;
}
var localization = me.GetCustomAttribute<LocalizationAttribute>();
string? localizedText;
return (localizedText = localization?.ResourceClass
.GetProperty(description.Description)?
.GetValue(default) as string) != null
? localizedText
: description.Description;
}
}

View File

@ -48,10 +48,10 @@ internal sealed class CommandInfo : ICommandContainer, ICommandInfo
if (CommandType != null && string.IsNullOrWhiteSpace(Description))
{
var description = CommandType.GetCustomAttribute<DescriptionAttribute>();
var description = CommandType.LocalizedDescription();
if (description != null)
{
Description = description.Description;
Description = description;
}
}
}

View File

@ -174,7 +174,7 @@ internal static class CommandModelBuilder
private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
{
var description = property.GetCustomAttribute<DescriptionAttribute>();
var description = property.LocalizedDescription();
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
var deconstructor = property.GetCustomAttribute<PairDeconstructorAttribute>();
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
@ -189,14 +189,14 @@ internal static class CommandModelBuilder
}
return new CommandOption(property.PropertyType, kind,
property, description?.Description, converter, deconstructor,
property, description, converter, deconstructor,
attribute, valueProvider, validators, defaultValue,
attribute.ValueIsOptional);
}
private static CommandArgument BuildArgumentParameter(PropertyInfo property, CommandArgumentAttribute attribute)
{
var description = property.GetCustomAttribute<DescriptionAttribute>();
var description = property.LocalizedDescription();
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
var defaultValue = property.GetCustomAttribute<DefaultValueAttribute>();
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
@ -206,7 +206,7 @@ internal static class CommandModelBuilder
return new CommandArgument(
property.PropertyType, kind, property,
description?.Description, converter,
description, converter,
defaultValue, attribute, valueProvider,
validators);
}

View File

@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<PackageId>NetAdmin.Spectre.Console.Cli</PackageId>
</PropertyGroup>
<PropertyGroup>
<IsAotCompatible Condition="'$(TargetFramework)' != 'netstandard2.0'" >false</IsAotCompatible>

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<IsTestProject>false</IsTestProject>
<IsPackable>true</IsPackable>
<IsPackable>false</IsPackable>
<Description>Contains testing utilities for Spectre.Console.</Description>
</PropertyGroup>

View File

@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<PackageId>NetAdmin.Spectre.Console</PackageId>
<DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants>
</PropertyGroup>
<PropertyGroup>