Skip to content

Commit

Permalink
Adds R/WinRT C# Autogen Gen 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Jan 21, 2023
1 parent b361aa2 commit 92894b5
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ app.manifest text encoding=utf-8 eol=lf
*.resw text encoding=utf-8-bom eol=crlf
*.xaml text encoding=utf-8 eol=crlf
*.sln text encoding=utf-8-bom eol=crlf
*.csproj text encoding=utf-8-bom eol=lf
*.csproj text encoding=utf-8 eol=lf
*.props text encoding=utf-8 eol=lf
*.targets text encoding=utf-8 eol=lf
*.vcxproj text encoding=utf-8 eol=crlf
Expand Down
1 change: 1 addition & 0 deletions nuget/Mntone.RWinRT.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<!-- C# sources -->
<file target="embedded\any" src="ResourceManager.cs" />
<file target="embedded\any" src="ResourceManager3.cs" />
<file target="tools\net6" src="..\bin\Release\RWinRT\net6\*" exclude="..\bin\Release\RWinRT\**\*.pdb" />

<!-- C++ includes -->
Expand Down
3 changes: 2 additions & 1 deletion nuget/Mntone.RWinRT.targets
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ $(_MntoneResourcePublicParams)
DependsOnTargets="_MntoneResourceGenerate"
Condition="'$(MntoneResourceGenerateEnabled)'=='true'">
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)..\embedded\any\ResourceManager.cs" Exclude="@(Compile)" />
<Compile Include="$(MSBuildThisFileDirectory)..\embedded\any\ResourceManager.cs" Exclude="@(Compile)" Condition="'$(MntoneResourceGenerateMode)'!='3'" />
<Compile Include="$(MSBuildThisFileDirectory)..\embedded\any\ResourceManager3.cs" Exclude="@(Compile)" Condition="'$(MntoneResourceGenerateMode)'=='3'" />
<Compile Include="$(MntoneResourceGeneratedFilesDir)$(MntoneResourceGenerateFileName)" Exclude="@(Compile)" />
</ItemGroup>
</Target>
Expand Down
107 changes: 107 additions & 0 deletions nuget/ResourceManager3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// R/WinRT (C#)
//
// Copyright (C) mntone.
// Licensed under the MIT License.

#nullable enable

using Microsoft.Windows.ApplicationModel.Resources;

namespace RWinRT
{
internal abstract class ResourceManager
{
private ResourceMap Resources { get; }

protected ResourceManager(string resourceName)
{
Resources = Native.MainResourceMap.GetSubtree(resourceName);
}

public string GetValueCore(string key)
{
return Resources.GetValue(key, Context).ValueAsString;
}

public string GetValueCore(string key, ResourceContext context)
{
return Resources.GetValue(key, context).ValueAsString;
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public string GetFormatValueCore(string key, params object?[] args)
#else
public string GetFormatValueCore(string key, params object[] args)
#endif
{
var format = Resources.GetValue(key, Context).ValueAsString;
return string.Format(format, args);
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public string GetFormatValueCore(string key, ResourceContext context, params object?[] args)
#else
public string GetFormatValueCore(string key, ResourceContext context, params object[] args)
#endif
{
var format = Resources.GetValue(key, context).ValueAsString;
return string.Format(format, args);
}

protected static Microsoft.Windows.ApplicationModel.Resources.ResourceManager Native { get; }

protected static ResourceContext Context { get; }

static ResourceManager()
{
Native = new Microsoft.Windows.ApplicationModel.Resources.ResourceManager();
Context = Native.CreateResourceContext();
}

public static void ChangeLanguage(string language)
{
Context.QualifierValues["Language"] = language;
}
}

internal struct ResourceObject
{
public ResourceManager Manager { get; }

public string Key { get; }

internal ResourceObject(ResourceManager manager, string key)
{
Manager = manager;
Key = key;
}

public string Value
{
get { return Manager.GetValueCore(Key); }
}

public string ValueIn(ResourceContext context)
{
return Manager.GetValueCore(Key, context);
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public string Format(params object?[] args)
#else
public string Format(params object[] args)
#endif
{
return Manager.GetFormatValueCore(Key, args);
}

#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public string Format(ResourceContext context, params object?[] args)
#else
public string Format(ResourceContext context, params object[] args)
#endif
{
return Manager.GetFormatValueCore(Key, context, args);
}
}
}
2 changes: 1 addition & 1 deletion samples/cs/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<Expander Grid.Row="2"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
Header="Document"
Header="Document (CSharpAutogenV2)"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Left"
IsExpanded="True"
Expand Down
22 changes: 22 additions & 0 deletions samples/cs/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ public MainWindow()
private void OnMessageButtonClick(object sender, RoutedEventArgs e)
{
string message;
#if CSHARP_AUTOGEN_IS_V3
// vvv Experimental version (CSharpAutogen V3) vvv
// Require mode=3 (<MntoneResourceGenerateMode>3</MntoneResourceGenerateMode> in csproj)
switch (i++ % 4)
{
case 0:
message = R.MainWindow_MessageButton_Message.Value;
break;
case 1:
message = R.MainWindow_MessageButton_Message2.Value;
break;
case 2:
message = R.MainWindow_MessageButton_Message3.Format(i);
break;
case 3:
default:
message = R.MainWindow_MessageButton_Message4.Value;
break;
}
#else
// vvv Initial release version (CSharpAutogen V2) vvv
switch (i++ % 4)
{
case 0:
Expand All @@ -33,6 +54,7 @@ private void OnMessageButtonClick(object sender, RoutedEventArgs e)
message = R.MainWindow_MessageButton_Message4.GetValue();
break;
}
#endif

var dialog = new ContentDialog()
{
Expand Down
10 changes: 8 additions & 2 deletions samples/cs/RWinRT.CSharpApp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
Expand All @@ -24,10 +24,16 @@
<MntoneResourceExcludeResourceNames>Resources</MntoneResourceExcludeResourceNames>
</PropertyGroup>

<!-- R/WinRT options (experimental) -->
<PropertyGroup>
<MntoneResourceGenerateMode>3</MntoneResourceGenerateMode>
<DefineConstants>$(DefineConstants);CSHARP_AUTOGEN_IS_V3</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.2.221209.1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
<PackageReference Include="Mntone.RWinRT" Version="0.0.1-alpha" />
<PackageReference Include="Mntone.RWinRT" Version="0.0.2-alpha" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

Expand Down
76 changes: 76 additions & 0 deletions src/Generators/CSharp/CSharpAutogen3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Mntone.RWinRT.Generators.CSharp.BlockWriters;
using Mntone.RWinRT.Generators.UnitWriters;
using System.Linq;

namespace Mntone.RWinRT.Generators.CSharp
{
public static class CSharpAutogen3

{
public static string[] Headers { get; } =
{
"//------------------------------------------------------------------------------",
"// <auto-generated>",
$"// This file was generated by R/WinRT v{typeof(Program).Assembly.GetName().Version}",
"//",
"// Changes to this file may cause incorrect behavior and will be lost if",
"// the code is regenerated.",
"// </auto-generated>",
"//------------------------------------------------------------------------------",
"#nullable enable",
"",
"// R/WinRT (C#) / CSharpAutogen V3",
"//",
$"// Copyright (C) {System.DateTime.UtcNow.Year} {System.Diagnostics.FileVersionInfo.GetVersionInfo( typeof(Program).Assembly.Location).CompanyName}.",
"// Licensed under the MIT License.",
"",
};

public static string[] Body { get; } =
{
"private sealed class __<0>_ResourceManager : global::RWinRT.ResourceManager",
"{",
"<indent>public static __<0>_ResourceManager Instance { get; } = new __<0>_ResourceManager();",
"<indent>public __<0>_ResourceManager() : base(\"<0>\") { }",
"}",
"",
};

private static void WriteSection(CSharpWriterContext ctx, string type, ResourcesData data)
{
using (StaticClass.Block(ctx, "R"))
{
Raw.Write(ctx, Body.Select(v => v.Replace("<0>", type)).ToArray());

foreach (var resource in data.Resources)
{
var name = resource.Name;
var preferredName = ctx.PreferredNameConverter(name);
Raw.Write(ctx,
$"/// <summary>\"{resource.Value}\"</summary>",
$"public static global::RWinRT.ResourceObject {preferredName} {{ get; }} = new global::RWinRT.ResourceObject(__{type}_ResourceManager.Instance, \"{name}\");");
}
}
}

public static string Build(CSharpWriterContext ctx, ResourcesData defaultResource, ResourcesData[] resources)
{
// Write headers
Raw.Write(ctx, Headers);

// Write root namespace
using (Namespace.Block(ctx, ctx.RootNamespace))
{
WriteSection(ctx, defaultResource.ResourceType, defaultResource);
foreach (var resource in resources)
{
using (Namespace.Block(ctx, resource.ResourceType))
{
WriteSection(ctx, resource.ResourceType, resource);
}
}
}
return ctx.Builder.ToString();
}
}
}
2 changes: 1 addition & 1 deletion src/Generators/CSharp/UnitWriters/ConstVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void WriteCore(ICodeWriterContext ctx, params string[] vals)
var name = vals[0];
var value = vals[1];
var preferredName = ctx.PreferredNameConverter(name);
ctx.Builder.Append($"{ctx.CurrentIndent()}// \"{value}\"{ctx.LineBreak}");
ctx.Builder.Append($"{ctx.CurrentIndent()}/// <summary>\"{value}\"</summary>{ctx.LineBreak}");
ctx.Builder.Append($"{ctx.CurrentIndent()}{accessor} const string {preferredName} = \"{name}\";{ctx.LineBreak}");
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ static int Main(string[] args)
case 1:
output = CSharpAutogen1.Build(ctx, defaultResource, otherResources);
break;
case 3:
output = CSharpAutogen3.Build(ctx, defaultResource, otherResources);
break;
case 2:
default:
output = CSharpAutogen2.Build(ctx, defaultResource, otherResources);
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"R/WinRT (C#)": {
"commandName": "Project",
"commandLineArgs": "--input \"$(ProjectDir)..\\samples\\cs\\Strings\\ProgramResources.lang-en-US.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\ProgramResources.lang-ja-JP.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\Resources.lang-en-US.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\Resources.lang-ja-JP.resw\"\r\n--output \"$(ProjectDir)..\\obj\\Debug\\RWinRT.CSharpApp\\x86\\Generated Files\"\r\n--exclude Resources\r\n--filename Resources.g.cs\r\n--namespace RWinRT.CSharpApp\r\n--impl-namespace __Impl\r\n--default ProgramResources\r\n--indent Space4\r\n--language en-US\r\n--langver CSharp10\r\n--linebreak LF"
"commandLineArgs": "--input \"$(ProjectDir)..\\samples\\cs\\Strings\\ProgramResources.lang-en-US.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\ProgramResources.lang-ja-JP.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\Resources.lang-en-US.resw\",\"$(ProjectDir)..\\samples\\cs\\Strings\\Resources.lang-ja-JP.resw\"\r\n--output \"$(ProjectDir)..\\obj\\Debug\\RWinRT.CSharpApp\\x86\\Generated Files\"\r\n--exclude Resources\r\n--filename Resources.g.cs\r\n--namespace RWinRT.CSharpApp\r\n--impl-namespace __Impl\r\n--default ProgramResources\r\n--indent Space4\r\n--language en-US\r\n--langver CSharp10\r\n--linebreak LF\r\n--mode 3"
},
"R/WinRT (C++)": {
"commandName": "Project",
Expand Down
40 changes: 6 additions & 34 deletions src/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,14 @@
<RWinRTCopyright>Copyright (c) 2023 $(RWinRTAuthor). All rights reserved.</RWinRTCopyright>
<RWinRTVersionMajor>0</RWinRTVersionMajor>
<RWinRTVersionMinor>0</RWinRTVersionMinor>
<RWinRTVersionPatch>1</RWinRTVersionPatch>
<RWinRTVersionPatch>2</RWinRTVersionPatch>
<RWinRTVersionBuild Condition="$(AngelVersionBuild)==''">$([System.DateTime]::Now.ToString(`yyyy`))</RWinRTVersionBuild>
<RWinRTVersionRevision Condition="$(AngelVersionRevision)==''">$([System.DateTime]::Now.ToString(`MMdd`))</RWinRTVersionRevision>
<RWinRTVersionPreRelease>alpha</RWinRTVersionPreRelease>
<RWinRTVersionPreReleaseId Condition="'$(AngelVersionPreRelease.StartsWith(`alpha`))'">$(AngelVersionPreRelease.Substring(5))</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(AngelVersionPreRelease.StartsWith(`beta`))'">$([MSBuild]::Add(1000, $([System.Convert]::ToUInt16($(AngelVersionPreRelease.Substring(4)), 10))))</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="$(AngelVersionPreReleaseId)==''">10000</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(RWinRTVersionPreRelease)'=='alpha'">0</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(RWinRTVersionPreReleaseId)'=='' And '$(RWinRTVersionPreRelease.StartsWith(`alpha`))'">$(RWinRTVersionPreRelease.Substring(5))</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(RWinRTVersionPreReleaseId)'=='' And '$(RWinRTVersionPreRelease)'=='beta'">1000</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(RWinRTVersionPreReleaseId)'=='' And '$(RWinRTVersionPreRelease.StartsWith(`beta`))'">$([MSBuild]::Add(1000, $([System.Convert]::ToUInt16($(RWinRTVersionPreRelease.Substring(4)), 10))))</RWinRTVersionPreReleaseId>
<RWinRTVersionPreReleaseId Condition="'$(RWinRTVersionPreRelease)'==''">10000</RWinRTVersionPreReleaseId>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>
%(PreprocessorDefinitions);
RWINRT_AUTHOR=$(AngelAuthor);
RWINRT_COPYRIGHT=$(AngelCopyright);
RWINRT_VERSION_MAJOR=$(AngelVersionMajor);
RWINRT_VERSION_MINOR=$(AngelVersionMinor);
RWINRT_VERSION_PATCH=$(AngelVersionPatch);
RWINRT_VERSION_BUILD=$(AngelVersionBuild);
RWINRT_VERSION_REVISION=$(AngelVersionRevision);
RWINRT_VERSION_PRERELEASE=$(AngelVersionPreRelease);
RWINRT_VERSION_PREID=$(AngelVersionPreReleaseId);
</PreprocessorDefinitions>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>
%(PreprocessorDefinitions);
RWINRT_AUTHOR=$(AngelAuthor);
RWINRT_COPYRIGHT=$(AngelCopyright);
RWINRT_VERSION_MAJOR=$(AngelVersionMajor);
RWINRT_VERSION_MINOR=$(AngelVersionMinor);
RWINRT_VERSION_PATCH=$(AngelVersionPatch);
RWINRT_VERSION_BUILD=$(AngelVersionBuild);
RWINRT_VERSION_REVISION=$(AngelVersionRevision);
RWINRT_VERSION_PRERELEASE=$(AngelVersionPreRelease);
RWINRT_VERSION_PREID=$(AngelVersionPreReleaseId);
</PreprocessorDefinitions>
</ResourceCompile>
</ItemDefinitionGroup>
</Project>

0 comments on commit 92894b5

Please sign in to comment.