Skip to content

Commit 9998ad4

Browse files
authored
Replace template generator (#54)
1 parent d542e2d commit 9998ad4

11 files changed

+149
-165
lines changed

source/MetadataProcessor.Core/DumpGenerator/DumpAllTable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class DumpAllTable
1212
public List<AssemblyRef> AssemblyReferences = new List<AssemblyRef>();
1313
public List<TypeRef> TypeReferences = new List<TypeRef>();
1414
public List<TypeDef> TypeDefinitions = new List<TypeDef>();
15+
public List<MethodDef> MethodDefinitions = new List<MethodDef>();
16+
public List<InterfaceDef> InterfaceDefinitions = new List<InterfaceDef>();
1517
public List<AttributeCustom> Attributes = new List<AttributeCustom>();
1618
public List<UserString> UserStrings = new List<UserString>();
1719
}

source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,58 @@ namespace nanoFramework.Tools.MetadataProcessor
77
{
88
internal partial class DumpTemplates
99
{
10-
internal static string DumpAllTemplate =
11-
@"{{#AssemblyReferences}}
12-
AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'
13-
{{/AssemblyReferences}}
10+
internal const string DumpAllTemplate =
11+
@"{{#each AssemblyReferences}}
12+
AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'{{#newline}}
13+
{{/each}}
14+
{{#if AssemblyReferences}}{{#newline}}{{/if}}
1415
15-
{{#TypeReferences}}
16-
TypeRefProps [{{ReferenceId}}]: Scope: {{Scope}} '{{Name}}'
17-
{{#MemberReferences}}
18-
MemberRefProps [{{ReferenceId}}]: '{{Name}}' [{{Signature}}]
19-
{{/MemberReferences}}
20-
{{/TypeReferences}}
16+
{{#each TypeReferences}}
17+
TypeRefProps [{{ReferenceId}}]: Scope: {{Scope}} '{{Name}}'{{#newline}}
18+
{{#each MemberReferences}}
19+
MemberRefProps [{{ReferenceId}}]: '{{Name}}' [{{Signature}}]{{#newline}}
20+
{{/each}}
21+
{{#newline}}
22+
{{/each}}
23+
{{#if TypeReferences}}{{#newline}}{{/if}}
2124
22-
{{#TypeDefinitions}}
23-
TypeDefProps [{{ReferenceId}}]: Flags: {{Flags}} Extends: {{ExtendsType}} Enclosed: {{EnclosedType}} '{{Name}}'
24-
{{#FieldDefinitions}}
25-
FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}]
26-
{{/FieldDefinitions}}
27-
{{#MethodDefinitions}}
28-
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]
29-
{{#Locals}}
30-
Locals {{Locals}}
31-
{{/Locals}}
32-
{{#ExceptionHandlers}}
33-
EH: {{ExceptionHandler}}
34-
{{/ExceptionHandlers}}
35-
{{#ILCodeInstructionsCount}}
36-
IL count: {{ILCodeInstructionsCount}}
37-
{{/ILCodeInstructionsCount}}
38-
{{#ILCode}}
39-
{{IL}}
40-
{{/ILCode}}
41-
{{/MethodDefinitions}}
42-
{{#InterfaceDefinitions}}
43-
InterfaceImplProps [{{ReferenceId}}]: Itf: {{Interface}}
44-
{{/InterfaceDefinitions}}
25+
{{#each TypeDefinitions}}
26+
TypeDefProps [{{ReferenceId}}]: Flags: {{Flags}} Extends: {{ExtendsType}} Enclosed: {{EnclosedType}} '{{Name}}'{{#newline}}
27+
{{#each FieldDefinitions}}
28+
FieldDefProps [{{ReferenceId}}]: Attr: {{Attributes}} Flags: {{Flags}} '{{Name}}' [{{Signature}}]{{#newline}}
29+
{{/each}}
4530
46-
{{/TypeDefinitions}}
31+
{{#each MethodDefinitions}}
32+
MethodDefProps [{{ReferenceId}}]: Flags: {{Flags}} Impl: {{Implementation}} RVA: {{RVA}} '{{Name}}' [{{Signature}}]{{#newline}}
33+
{{#if Locals}}
34+
Locals {{Locals}}{{#newline}}
35+
{{/if}}
36+
{{#each ExceptionHandlers}}
37+
EH: {{Handler}}{{#newline}}
38+
{{/each}}
39+
{{#if ILCodeInstructionsCount}}
40+
IL count: {{ILCodeInstructionsCount}}{{#newline}}
41+
{{/if}}
42+
{{#each ILCode}}
43+
{{IL}}{{#newline}}
44+
{{/each}}
45+
{{/each}}
4746
48-
{{#Attributes}}
49-
Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]
50-
{{/Attributes}}
47+
{{#each InterfaceDefinitions}}
48+
InterfaceImplProps [{{ReferenceId}}]: Itf: {{Interface}}{{#newline}}
49+
{{/each}}
50+
{{#if InterfaceDefinitions}}{{#newline}}{{/if}}
51+
{{#newline}}
52+
{{/each}}
5153
52-
{{#UserStrings}}
53-
UserString [{{ReferenceId}}]: '{{Content}}'
54-
{{/UserStrings}}
55-
";
54+
{{#each Attributes}}
55+
Attribute: {{Name}}::[{{ReferenceId}} {{TypeToken}}]{{#newline}}
56+
{{/each}}
57+
{{#if Attributes}}{{#newline}}{{/if}}
5658
59+
{{#each UserStrings}}
60+
UserString [{{ReferenceId}}]: '{{Content}}'{{#newline}}
61+
{{/each}}
62+
";
5763
}
5864
}

source/MetadataProcessor.Core/MetadataProcessor.Core.csproj

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,12 @@
5252
<Reference Include="Mono.Cecil.Rocks, Version=0.11.1.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
5353
<HintPath>..\packages\Mono.Cecil.0.11.1\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
5454
</Reference>
55-
<Reference Include="Stubble.Core, Version=1.6.0.0, Culture=neutral, processorArchitecture=MSIL">
56-
<HintPath>..\packages\Stubble.Core.1.6.3\lib\net45\Stubble.Core.dll</HintPath>
55+
<Reference Include="mustache-sharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5885df51f4df0041, processorArchitecture=MSIL">
56+
<HintPath>..\packages\mustache-sharp.1.0.0\lib\net45\mustache-sharp.dll</HintPath>
5757
</Reference>
5858
<Reference Include="System" />
59-
<Reference Include="System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
60-
<HintPath>..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
61-
<SpecificVersion>False</SpecificVersion>
62-
</Reference>
6359
<Reference Include="System.Drawing" />
6460
<Reference Include="System.Numerics" />
65-
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
66-
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
67-
<SpecificVersion>False</SpecificVersion>
68-
</Reference>
69-
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
70-
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
71-
<SpecificVersion>False</SpecificVersion>
72-
</Reference>
7361
<Reference Include="System.Windows.Forms" />
7462
<Reference Include="System.Xml" />
7563
</ItemGroup>
@@ -147,7 +135,5 @@
147135
<ErrorText>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}.</ErrorText>
148136
</PropertyGroup>
149137
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.0.50\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.0.50\build\Nerdbank.GitVersioning.targets'))" />
150-
<Error Condition="!Exists('..\packages\StrongNamer.0.2.5\build\StrongNamer.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\StrongNamer.0.2.5\build\StrongNamer.targets'))" />
151138
</Target>
152-
<Import Project="..\packages\StrongNamer.0.2.5\build\StrongNamer.targets" Condition="Exists('..\packages\StrongNamer.0.2.5\build\StrongNamer.targets')" />
153139
</Project>

source/MetadataProcessor.Core/SkeletonGenerator/SkeletonTemplates.cs

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,84 @@ namespace nanoFramework.Tools.MetadataProcessor
77
{
88
internal partial class SkeletonTemplates
99
{
10-
internal static string AssemblyHeaderTemplate =
11-
@"//-----------------------------------------------------------------------------
12-
//
13-
// ** WARNING! **
14-
// This file was generated automatically by a tool.
15-
// Re-running the tool will overwrite this file.
16-
// You should copy this file to a custom location
17-
// before adding any customization in the copy to
18-
// prevent loss of your changes when the tool is
19-
// re-run.
20-
//
21-
//-----------------------------------------------------------------------------
22-
23-
#ifndef _{{ShortNameUpper}}_H_
24-
#define _{{ShortNameUpper}}_H_
25-
26-
#include <nanoCLR_Interop.h>
27-
#include <nanoCLR_Runtime.h>
28-
#include <corlib_native.h>
29-
30-
{{#Classes}}
31-
struct Library_{{AssemblyName}}_{{Name}}
32-
{
33-
{{#StaticFields}}
34-
static const int FIELD_STATIC__{{Name}} = {{ReferenceIndex}};
35-
{{/StaticFields}}
36-
37-
{{#InstanceFields}}
38-
{{#FieldWarning}}
39-
{{FieldWarning}}
40-
{{/FieldWarning}}
41-
static const int FIELD__{{Name}} = {{ReferenceIndex}};
42-
{{/InstanceFields}}
43-
44-
{{#Methods}}
45-
NANOCLR_NATIVE_DECLARE({{Declaration}});
46-
{{/Methods}}
47-
48-
//--//
49-
50-
};
51-
52-
{{/Classes}}
53-
extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{Name}};
54-
55-
#endif //_{{ShortNameUpper}}_H_
10+
internal const string AssemblyHeaderTemplate =
11+
@"//-----------------------------------------------------------------------------{{#newline}}
12+
//{{#newline}}
13+
// ** WARNING! ** {{#newline}}
14+
// This file was generated automatically by a tool.{{#newline}}
15+
// Re-running the tool will overwrite this file.{{#newline}}
16+
// You should copy this file to a custom location{{#newline}}
17+
// before adding any customization in the copy to{{#newline}}
18+
// prevent loss of your changes when the tool is{{#newline}}
19+
// re-run.{{#newline}}
20+
//{{#newline}}
21+
//-----------------------------------------------------------------------------{{#newline}}
22+
{{#newline}}
23+
24+
#ifndef _{{ShortNameUpper}}_H_{{#newline}}
25+
#define _{{ShortNameUpper}}_H_{{#newline}}
26+
{{#newline}}
27+
28+
#include <nanoCLR_Interop.h>{{#newline}}
29+
#include <nanoCLR_Runtime.h>{{#newline}}
30+
#include <corlib_native.h>{{#newline}}
31+
{{#newline}}
32+
33+
{{#each Classes}}
34+
struct Library_{{AssemblyName}}_{{Name}}{{#newline}}
35+
{{{#newline}}
36+
37+
{{#each StaticFields}}
38+
static const int FIELD_STATIC__{{Name}} = {{ReferenceIndex}};{{#newline}}
39+
{{/each}}
40+
{{#if StaticFields}}{{#newline}}{{/if}}
41+
42+
{{#each InstanceFields}}
43+
{{#if FieldWarning}}{{FieldWarning}}{{/if}}
44+
static const int FIELD__{{Name}} = {{ReferenceIndex}};{{#newline}}
45+
{{/each}}
46+
{{#if InstanceFields}}{{#newline}}{{/if}}
47+
48+
{{#each Methods}}
49+
NANOCLR_NATIVE_DECLARE({{Declaration}});{{#newline}}
50+
{{/each}}
51+
{{#if Methods}}{{#newline}}{{/if}}
52+
53+
//--//{{#newline}}
54+
{{#newline}}
55+
};{{#newline}}
56+
{{#newline}}
57+
{{/each}}
58+
extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{Name}};{{#newline}}
59+
{{#newline}}
60+
#endif //_{{ShortNameUpper}}_H_{{#newline}}
5661
";
5762

58-
internal static string AssemblyLookupTemplate =
59-
@"#include ""{{HeaderFileName}}.h""
60-
61-
static const CLR_RT_MethodHandler method_lookup[] =
62-
{
63-
{{#LookupTable}}
64-
{{Declaration}},
65-
{{/LookupTable}}
66-
};
67-
68-
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{AssemblyName}} =
69-
{
70-
""{{Name}}"",
71-
{{NativeCRC32}},
72-
method_lookup,
73-
////////////////////////////////////////////////////////////////////////////////////
74-
// check if the version bellow matches the one in AssemblyNativeVersion attribute //
75-
////////////////////////////////////////////////////////////////////////////////////
76-
{ {{NativeVersion.Major}}, {{NativeVersion.Minor}}, {{NativeVersion.Build}}, {{NativeVersion.Revision}} }
77-
};
63+
internal const string AssemblyLookupTemplate =
64+
@"#include ""{{HeaderFileName}}.h""{{#newline}}
65+
{{#newline}}
66+
67+
static const CLR_RT_MethodHandler method_lookup[] ={{#newline}}
68+
{{{#newline}}
69+
{{#each LookupTable}}
70+
{{Declaration}},{{#newline}}
71+
{{/each}}
72+
};{{#newline}}
73+
{{#newline}}
74+
75+
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_{{AssemblyName}} ={{#newline}}
76+
{{{#newline}}
77+
""{{Name}}"",{{#newline}}
78+
{{NativeCRC32}},{{#newline}}
79+
method_lookup,{{#newline}}
80+
////////////////////////////////////////////////////////////////////////////////////{{#newline}}
81+
// check if the version bellow matches the one in AssemblyNativeVersion attribute //{{#newline}}
82+
////////////////////////////////////////////////////////////////////////////////////{{#newline}}
83+
{ {{NativeVersion.Major}}, {{NativeVersion.Minor}}, {{NativeVersion.Build}}, {{NativeVersion.Revision}} }{{#newline}}
84+
};{{#newline}}
7885
";
7986

80-
internal static string ClassStubTemplate =
87+
internal const string ClassStubTemplate =
8188
@"//-----------------------------------------------------------------------------
8289
//
8390
// ** WARNING! **
@@ -92,7 +99,7 @@ struct Library_{{AssemblyName}}_{{Name}}
9299
93100
#include ""{{HeaderFileName}}.h""
94101
95-
{{#Functions}}
102+
{{#each Functions}}
96103
HRESULT {{Declaration}}( CLR_RT_StackFrame& stack )
97104
{
98105
NANOCLR_HEADER();
@@ -101,8 +108,6 @@ struct Library_{{AssemblyName}}_{{Name}}
101108
102109
NANOCLR_NOCLEANUP();
103110
}
104-
105-
{{/Functions}}
106-
";
111+
{{/each}}";
107112
}
108113
}

source/MetadataProcessor.Core/nanoDependencyGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using Mono.Cecil;
77
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
8-
using Stubble.Core.Builders;
98
using System;
109
using System.IO;
1110
using System.Linq;

source/MetadataProcessor.Core/nanoDumperGenerator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using Mono.Cecil;
77
using Mono.Cecil.Cil;
88
using Mono.Collections.Generic;
9+
using Mustache;
910
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
10-
using Stubble.Core.Builders;
1111
using System;
1212
using System.Collections.Generic;
1313
using System.IO;
@@ -45,11 +45,13 @@ public void DumpAll()
4545
DumpCustomAttributes(dumpTable);
4646
DumpUserStrings(dumpTable);
4747

48-
var stubble = new StubbleBuilder().Build();
48+
49+
FormatCompiler compiler = new FormatCompiler();
50+
Generator generator = compiler.Compile(DumpTemplates.DumpAllTemplate);
4951

5052
using (var dumpFile = File.CreateText(_path))
5153
{
52-
var output = stubble.Render(DumpTemplates.DumpAllTemplate, dumpTable);
54+
var output = generator.Render(dumpTable);
5355
dumpFile.Write(output);
5456
}
5557
}

source/MetadataProcessor.Core/nanoFramework.Tools.MetaDataProcessor.Core.targets

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,7 @@
1212
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1313
<IncludeInVSIX>true</IncludeInVSIX>
1414
</Content>
15-
<Content Include="$(OutDir)Stubble.Core.dll">
16-
<InstallRoot>MSBuild</InstallRoot>
17-
<VSIXSubPath>nanoFramework\v1.0\</VSIXSubPath>
18-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19-
<IncludeInVSIX>true</IncludeInVSIX>
20-
</Content>
21-
<Content Include="$(OutDir)System.Collections.Immutable.dll">
22-
<InstallRoot>MSBuild</InstallRoot>
23-
<VSIXSubPath>nanoFramework\v1.0\</VSIXSubPath>
24-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25-
<IncludeInVSIX>true</IncludeInVSIX>
26-
</Content>
27-
<Content Include="$(OutDir)System.Runtime.CompilerServices.Unsafe.dll">
28-
<InstallRoot>MSBuild</InstallRoot>
29-
<VSIXSubPath>nanoFramework\v1.0\</VSIXSubPath>
30-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31-
<IncludeInVSIX>true</IncludeInVSIX>
32-
</Content>
33-
<Content Include="$(OutDir)System.Threading.Tasks.Extensions.dll">
15+
<Content Include="$(OutDir)mustache-sharp.dll">
3416
<InstallRoot>MSBuild</InstallRoot>
3517
<VSIXSubPath>nanoFramework\v1.0\</VSIXSubPath>
3618
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

0 commit comments

Comments
 (0)