From a7b16d80ffdd4a610081baa7e7ff8855072c2e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 18 Feb 2020 18:31:08 +0000 Subject: [PATCH] Replace template generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Stubble.Core with mustache-sharp. - Update code accordingly. - Update templates accordingly. - Fix properties of DumpAll class. - Bump version to 2.18.0. Signed-off-by: José Simões --- .../DumpGenerator/DumpAllTable.cs | 2 + .../DumpGenerator/DumpTemplates.cs | 88 ++++++----- .../MetadataProcessor.Core.csproj | 18 +-- .../SkeletonGenerator/SkeletonTemplates.cs | 147 +++++++++--------- .../nanoDependencyGenerator.cs | 1 - .../nanoDumperGenerator.cs | 8 +- ...ework.Tools.MetaDataProcessor.Core.targets | 20 +-- .../nanoSkeletonGenerator.cs | 20 ++- source/MetadataProcessor.Core/package.nuspec | 2 +- source/MetadataProcessor.Core/packages.config | 6 +- source/version.json | 2 +- 11 files changed, 149 insertions(+), 165 deletions(-) diff --git a/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs b/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs index 086a9387..c9fbc775 100644 --- a/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs +++ b/source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs @@ -12,6 +12,8 @@ public class DumpAllTable public List AssemblyReferences = new List(); public List TypeReferences = new List(); public List TypeDefinitions = new List(); + public List MethodDefinitions = new List(); + public List InterfaceDefinitions = new List(); public List Attributes = new List(); public List UserStrings = new List(); } diff --git a/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs b/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs index 3ce4dc8f..34d44c09 100644 --- a/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs +++ b/source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs @@ -7,52 +7,58 @@ namespace nanoFramework.Tools.MetadataProcessor { internal partial class DumpTemplates { - internal static string DumpAllTemplate = -@"{{#AssemblyReferences}} -AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}' -{{/AssemblyReferences}} + internal const string DumpAllTemplate = +@"{{#each AssemblyReferences}} +AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'{{#newline}} +{{/each}} +{{#if AssemblyReferences}}{{#newline}}{{/if}} -{{#TypeReferences}} -TypeRefProps [{{ReferenceId}}]: Scope: {{Scope}} '{{Name}}' -{{#MemberReferences}} - MemberRefProps [{{ReferenceId}}]: '{{Name}}' [{{Signature}}] -{{/MemberReferences}} -{{/TypeReferences}} +{{#each TypeReferences}} +TypeRefProps [{{ReferenceId}}]: Scope: {{Scope}} '{{Name}}'{{#newline}} +{{#each MemberReferences}} + MemberRefProps [{{ReferenceId}}]: '{{Name}}' [{{Signature}}]{{#newline}} +{{/each}} +{{#newline}} +{{/each}} +{{#if TypeReferences}}{{#newline}}{{/if}} -{{#TypeDefinitions}} -TypeDefProps [{{ReferenceId}}]: Flags: {{Flags}} Extends: {{ExtendsType}} Enclosed: {{EnclosedType}} '{{Name}}' -{{#FieldDefinitions}} - FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}] -{{/FieldDefinitions}} -{{#MethodDefinitions}} - MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}] - {{#Locals}} - Locals {{Locals}} - {{/Locals}} - {{#ExceptionHandlers}} - EH: {{ExceptionHandler}} - {{/ExceptionHandlers}} - {{#ILCodeInstructionsCount}} - IL count: {{ILCodeInstructionsCount}} - {{/ILCodeInstructionsCount}} - {{#ILCode}} - {{IL}} - {{/ILCode}} -{{/MethodDefinitions}} -{{#InterfaceDefinitions}} - InterfaceImplProps [{{ReferenceId}}]: Itf: {{Interface}} -{{/InterfaceDefinitions}} +{{#each TypeDefinitions}} +TypeDefProps [{{ReferenceId}}]: Flags: {{Flags}} Extends: {{ExtendsType}} Enclosed: {{EnclosedType}} '{{Name}}'{{#newline}} +{{#each FieldDefinitions}} + FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}]{{#newline}} +{{/each}} -{{/TypeDefinitions}} +{{#each MethodDefinitions}} + MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]{{#newline}} +{{#if Locals}} + Locals {{Locals}}{{#newline}} +{{/if}} +{{#each ExceptionHandlers}} + EH: {{Handler}}{{#newline}} +{{/each}} +{{#if ILCodeInstructionsCount}} + IL count: {{ILCodeInstructionsCount}}{{#newline}} +{{/if}} +{{#each ILCode}} + {{IL}}{{#newline}} +{{/each}} +{{/each}} -{{#Attributes}} -Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}] -{{/Attributes}} +{{#each InterfaceDefinitions}} + InterfaceImplProps [{{ReferenceId}}]: Itf: {{Interface}}{{#newline}} +{{/each}} +{{#if InterfaceDefinitions}}{{#newline}}{{/if}} +{{#newline}} +{{/each}} -{{#UserStrings}} -UserString [{{ReferenceId}}]: '{{Content}}' -{{/UserStrings}} -"; +{{#each Attributes}} +Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]{{#newline}} +{{/each}} +{{#if Attributes}}{{#newline}}{{/if}} +{{#each UserStrings}} +UserString [{{ReferenceId}}]: '{{Content}}'{{#newline}} +{{/each}} +"; } } diff --git a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj index 4a1ef00a..76960fd9 100644 --- a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj +++ b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj @@ -52,24 +52,12 @@ ..\packages\Mono.Cecil.0.11.1\lib\net40\Mono.Cecil.Rocks.dll - - ..\packages\Stubble.Core.1.6.3\lib\net45\Stubble.Core.dll + + ..\packages\mustache-sharp.1.0.0\lib\net45\mustache-sharp.dll - - ..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll - False - - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll - False - - - ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll - False - @@ -147,7 +135,5 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - \ No newline at end of file diff --git a/source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs b/source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs index 4035d2da..d78dc667 100644 --- a/source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs +++ b/source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs @@ -7,77 +7,84 @@ namespace nanoFramework.Tools.MetadataProcessor { internal partial class SkeletonTemplates { - internal static string AssemblyHeaderTemplate = -@"//----------------------------------------------------------------------------- -// -// ** WARNING! ** -// This file was generated automatically by a tool. -// Re-running the tool will overwrite this file. -// You should copy this file to a custom location -// before adding any customization in the copy to -// prevent loss of your changes when the tool is -// re-run. -// -//----------------------------------------------------------------------------- - -#ifndef _{{ShortNameUpper}}_H_ -#define _{{ShortNameUpper}}_H_ - -#include -#include -#include - -{{#Classes}} -struct Library_{{AssemblyName}}_{{Name}} -{ - {{#StaticFields}} - static const int FIELD_STATIC__{{Name}} = {{ReferenceIndex}}; - {{/StaticFields}} - - {{#InstanceFields}} - {{#FieldWarning}} - {{FieldWarning}} - {{/FieldWarning}} - static const int FIELD__{{Name}} = {{ReferenceIndex}}; - {{/InstanceFields}} - - {{#Methods}} - NANOCLR_NATIVE_DECLARE({{Declaration}}); - {{/Methods}} - - //--// - -}; - -{{/Classes}} -extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{Name}}; - -#endif //_{{ShortNameUpper}}_H_ + internal const string AssemblyHeaderTemplate = +@"//-----------------------------------------------------------------------------{{#newline}} +//{{#newline}} +// ** WARNING! ** {{#newline}} +// This file was generated automatically by a tool.{{#newline}} +// Re-running the tool will overwrite this file.{{#newline}} +// You should copy this file to a custom location{{#newline}} +// before adding any customization in the copy to{{#newline}} +// prevent loss of your changes when the tool is{{#newline}} +// re-run.{{#newline}} +//{{#newline}} +//-----------------------------------------------------------------------------{{#newline}} +{{#newline}} + +#ifndef _{{ShortNameUpper}}_H_{{#newline}} +#define _{{ShortNameUpper}}_H_{{#newline}} +{{#newline}} + +#include {{#newline}} +#include {{#newline}} +#include {{#newline}} +{{#newline}} + +{{#each Classes}} +struct Library_{{AssemblyName}}_{{Name}}{{#newline}} +{{{#newline}} + +{{#each StaticFields}} + static const int FIELD_STATIC__{{Name}} = {{ReferenceIndex}};{{#newline}} +{{/each}} +{{#if StaticFields}}{{#newline}}{{/if}} + +{{#each InstanceFields}} +{{#if FieldWarning}}{{FieldWarning}}{{/if}} + static const int FIELD__{{Name}} = {{ReferenceIndex}};{{#newline}} +{{/each}} +{{#if InstanceFields}}{{#newline}}{{/if}} + +{{#each Methods}} + NANOCLR_NATIVE_DECLARE({{Declaration}});{{#newline}} +{{/each}} +{{#if Methods}}{{#newline}}{{/if}} + + //--//{{#newline}} +{{#newline}} +};{{#newline}} +{{#newline}} +{{/each}} +extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{Name}};{{#newline}} +{{#newline}} +#endif //_{{ShortNameUpper}}_H_{{#newline}} "; - internal static string AssemblyLookupTemplate = -@"#include ""{{HeaderFileName}}.h"" - -static const CLR_RT_MethodHandler method_lookup[] = -{ -{{#LookupTable}} - {{Declaration}}, -{{/LookupTable}} -}; - -const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{AssemblyName}} = -{ - ""{{Name}}"", - {{NativeCRC32}}, - method_lookup, - //////////////////////////////////////////////////////////////////////////////////// - // check if the version bellow matches the one in AssemblyNativeVersion attribute // - //////////////////////////////////////////////////////////////////////////////////// - { {{NativeVersion.Major}}, {{NativeVersion.Minor}}, {{NativeVersion.Build}}, {{NativeVersion.Revision}} } -}; + internal const string AssemblyLookupTemplate = +@"#include ""{{HeaderFileName}}.h""{{#newline}} +{{#newline}} + +static const CLR_RT_MethodHandler method_lookup[] ={{#newline}} +{{{#newline}} +{{#each LookupTable}} + {{Declaration}},{{#newline}} +{{/each}} +};{{#newline}} +{{#newline}} + +const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{AssemblyName}} ={{#newline}} +{{{#newline}} + ""{{Name}}"",{{#newline}} + {{NativeCRC32}},{{#newline}} + method_lookup,{{#newline}} + ////////////////////////////////////////////////////////////////////////////////////{{#newline}} + // check if the version bellow matches the one in AssemblyNativeVersion attribute //{{#newline}} + ////////////////////////////////////////////////////////////////////////////////////{{#newline}} + { {{NativeVersion.Major}}, {{NativeVersion.Minor}}, {{NativeVersion.Build}}, {{NativeVersion.Revision}} }{{#newline}} +};{{#newline}} "; - internal static string ClassStubTemplate = + internal const string ClassStubTemplate = @"//----------------------------------------------------------------------------- // // ** WARNING! ** @@ -92,7 +99,7 @@ struct Library_{{AssemblyName}}_{{Name}} #include ""{{HeaderFileName}}.h"" -{{#Functions}} +{{#each Functions}} HRESULT {{Declaration}}( CLR_RT_StackFrame& stack ) { NANOCLR_HEADER(); @@ -101,8 +108,6 @@ struct Library_{{AssemblyName}}_{{Name}} NANOCLR_NOCLEANUP(); } - -{{/Functions}} -"; +{{/each}}"; } } diff --git a/source/MetadataProcessor.Core/nanoDependencyGenerator.cs b/source/MetadataProcessor.Core/nanoDependencyGenerator.cs index 01a84211..60e75d8a 100644 --- a/source/MetadataProcessor.Core/nanoDependencyGenerator.cs +++ b/source/MetadataProcessor.Core/nanoDependencyGenerator.cs @@ -5,7 +5,6 @@ using Mono.Cecil; using nanoFramework.Tools.MetadataProcessor.Core.Extensions; -using Stubble.Core.Builders; using System; using System.IO; using System.Linq; diff --git a/source/MetadataProcessor.Core/nanoDumperGenerator.cs b/source/MetadataProcessor.Core/nanoDumperGenerator.cs index 29e1ac00..52e807e6 100644 --- a/source/MetadataProcessor.Core/nanoDumperGenerator.cs +++ b/source/MetadataProcessor.Core/nanoDumperGenerator.cs @@ -6,8 +6,8 @@ using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Collections.Generic; +using Mustache; using nanoFramework.Tools.MetadataProcessor.Core.Extensions; -using Stubble.Core.Builders; using System; using System.Collections.Generic; using System.IO; @@ -45,11 +45,13 @@ public void DumpAll() DumpCustomAttributes(dumpTable); DumpUserStrings(dumpTable); - var stubble = new StubbleBuilder().Build(); + + FormatCompiler compiler = new FormatCompiler(); + Generator generator = compiler.Compile(DumpTemplates.DumpAllTemplate); using (var dumpFile = File.CreateText(_path)) { - var output = stubble.Render(DumpTemplates.DumpAllTemplate, dumpTable); + var output = generator.Render(dumpTable); dumpFile.Write(output); } } diff --git a/source/MetadataProcessor.Core/nanoFramework.Tools.MetaDataProcessor.Core.targets b/source/MetadataProcessor.Core/nanoFramework.Tools.MetaDataProcessor.Core.targets index 3657bf9b..b6e00e04 100644 --- a/source/MetadataProcessor.Core/nanoFramework.Tools.MetaDataProcessor.Core.targets +++ b/source/MetadataProcessor.Core/nanoFramework.Tools.MetaDataProcessor.Core.targets @@ -12,25 +12,7 @@ Always true - - MSBuild - nanoFramework\v1.0\ - Always - true - - - MSBuild - nanoFramework\v1.0\ - Always - true - - - MSBuild - nanoFramework\v1.0\ - Always - true - - + MSBuild nanoFramework\v1.0\ Always diff --git a/source/MetadataProcessor.Core/nanoSkeletonGenerator.cs b/source/MetadataProcessor.Core/nanoSkeletonGenerator.cs index 88d6bea2..94b8325b 100644 --- a/source/MetadataProcessor.Core/nanoSkeletonGenerator.cs +++ b/source/MetadataProcessor.Core/nanoSkeletonGenerator.cs @@ -4,8 +4,8 @@ // using Mono.Cecil; +using Mustache; using nanoFramework.Tools.MetadataProcessor.Core.Extensions; -using Stubble.Core.Builders; using System; using System.IO; using System.Linq; @@ -86,11 +86,15 @@ private void GenerateStubs() // anything to add to the header? if (classStubs.Functions.Count > 0) { - var stubble = new StubbleBuilder().Build(); + FormatCompiler compiler = new FormatCompiler + { + RemoveNewLines = false + }; + Generator generator = compiler.Compile(SkeletonTemplates.ClassStubTemplate); using (var headerFile = File.CreateText(Path.Combine(_path, $"{_project}_{className}.cpp"))) { - var output = stubble.Render(SkeletonTemplates.ClassStubTemplate, classStubs); + var output = generator.Render(classStubs); headerFile.Write(output); } } @@ -179,11 +183,12 @@ private void GenerateAssemblyLookup() } } - var stubble = new StubbleBuilder().Build(); + FormatCompiler compiler = new FormatCompiler(); + Generator generator = compiler.Compile(SkeletonTemplates.AssemblyLookupTemplate); using (var headerFile = File.CreateText(Path.Combine(_path, $"{_project}.cpp"))) { - var output = stubble.Render(SkeletonTemplates.AssemblyLookupTemplate, assemblyLookup); + var output = generator.Render(assemblyLookup); headerFile.Write(output); } } @@ -287,13 +292,14 @@ private void GenerateAssemblyHeader() } } - var stubble = new StubbleBuilder().Build(); + FormatCompiler compiler = new FormatCompiler(); + Generator generator = compiler.Compile(SkeletonTemplates.AssemblyHeaderTemplate); Directory.CreateDirectory(_path); using (var headerFile = File.CreateText(Path.Combine(_path, $"{_project}.h"))) { - var output = stubble.Render(SkeletonTemplates.AssemblyHeaderTemplate, assemblyData); + var output = generator.Render(assemblyData); headerFile.Write(output); } } diff --git a/source/MetadataProcessor.Core/package.nuspec b/source/MetadataProcessor.Core/package.nuspec index b0749f44..268ce5c5 100644 --- a/source/MetadataProcessor.Core/package.nuspec +++ b/source/MetadataProcessor.Core/package.nuspec @@ -23,7 +23,7 @@ nanoFramework, nano Framework, NETNF, NETMF, Micro Framework, .net - + diff --git a/source/MetadataProcessor.Core/packages.config b/source/MetadataProcessor.Core/packages.config index 1b691009..5af88ffd 100644 --- a/source/MetadataProcessor.Core/packages.config +++ b/source/MetadataProcessor.Core/packages.config @@ -1,10 +1,6 @@  + - - - - - \ No newline at end of file diff --git a/source/version.json b/source/version.json index 6c417da6..9ec4264a 100644 --- a/source/version.json +++ b/source/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.16.0", + "version": "2.18.0", "release": { "branchName" : "release-v{version}", "versionIncrement" : "minor",