Skip to content

Commit ce03e3e

Browse files
committed
tweak: require title block, fix trim build
1 parent 5c1f7ea commit ce03e3e

12 files changed

Lines changed: 104 additions & 30 deletions

File tree

src/NativeCodeGen.Cli/Program.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,23 @@ static async Task Validate(string input, bool strict)
203203
// Load shared examples
204204
var sharedExampleRegistry = new SharedExampleRegistry();
205205
var sharedExamplesDir = Path.Combine(inputDir, "code", "shared-examples");
206+
var sharedExampleErrors = new List<ParseError>();
206207
if (Directory.Exists(sharedExamplesDir))
207208
{
208209
sharedExampleRegistry.LoadExamples(sharedExamplesDir);
209210
Console.WriteLine($"Loaded {sharedExampleRegistry.Count} shared examples from {sharedExamplesDir}");
211+
212+
// Convert shared example errors to ParseErrors
213+
foreach (var error in sharedExampleRegistry.Errors)
214+
{
215+
sharedExampleErrors.Add(new ParseError
216+
{
217+
FilePath = error.Split(':')[0],
218+
Line = 1,
219+
Column = 1,
220+
Message = error.Contains(':') ? error[(error.IndexOf(':') + 2)..] : error
221+
});
222+
}
210223
}
211224
db.SharedExamples = sharedExampleRegistry.GetAllExamples();
212225

@@ -334,6 +347,10 @@ static async Task Validate(string input, bool strict)
334347

335348
db.Enums = enumRegistry.GetAllEnums();
336349

350+
// Add shared example errors
351+
foreach (var error in sharedExampleErrors)
352+
allErrors.Add(error);
353+
337354
return (db, allErrors.ToList(), allWarnings.ToList());
338355
}
339356

src/NativeCodeGen.Cli/TrimmerRoots.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@
1919
<type fullname="NativeCodeGen.Core.Export.ExportStructField" preserve="all" />
2020
<type fullname="NativeCodeGen.Core.Export.ExportNativeReference" preserve="all" />
2121
<type fullname="NativeCodeGen.Core.Export.ExportSharedExample" preserve="all" />
22+
<type fullname="NativeCodeGen.Core.Export.ExportSharedExampleCode" preserve="all" />
23+
<type fullname="NativeCodeGen.Core.Registry.SharedExampleFrontmatter" preserve="all" />
2224
</assembly>
2325
</linker>

src/NativeCodeGen.Core/Export/DatabaseConverter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ private static ExportSharedExample ConvertSharedExample(Models.SharedExample exa
166166
{
167167
Name = example.Name,
168168
Title = example.Title,
169-
Content = example.Content,
170-
Language = example.Language
169+
Examples = example.Examples.Select(e => new ExportSharedExampleCode
170+
{
171+
Content = e.Content,
172+
Language = e.Language
173+
}).ToList()
171174
};
172175
}
173176
}

src/NativeCodeGen.Core/Export/ExportModels.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,18 @@ public partial class ExportSharedExample
191191
public string Name { get; set; } = string.Empty;
192192

193193
[ProtoMember(2)]
194-
public string? Title { get; set; }
194+
public string Title { get; set; } = string.Empty;
195195

196196
[ProtoMember(3)]
197+
public List<ExportSharedExampleCode> Examples { get; set; } = new();
198+
}
199+
200+
[ProtoContract]
201+
public partial class ExportSharedExampleCode
202+
{
203+
[ProtoMember(1)]
197204
public string Content { get; set; } = string.Empty;
198205

199-
[ProtoMember(4)]
206+
[ProtoMember(2)]
200207
public string? Language { get; set; }
201208
}

src/NativeCodeGen.Core/Export/JsonContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace NativeCodeGen.Core.Export;
2222
[JsonSerializable(typeof(ExportStructField))]
2323
[JsonSerializable(typeof(ExportNativeReference))]
2424
[JsonSerializable(typeof(ExportSharedExample))]
25+
[JsonSerializable(typeof(ExportSharedExampleCode))]
26+
[JsonSerializable(typeof(List<ExportSharedExampleCode>))]
2527
[JsonSerializable(typeof(ParamFlags))]
2628
[JsonSerializable(typeof(FieldFlags))]
2729
[JsonSerializable(typeof(List<ExportNative>))]

src/NativeCodeGen.Core/Export/ProtobufExporter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ProtobufExporter : IExporter
2121
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportStructField))]
2222
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportNativeReference))]
2323
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportSharedExample))]
24+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportSharedExampleCode))]
2425
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportTypeInfo))]
2526
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportTypeEntry))]
2627
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ExportTypeCategory))]

src/NativeCodeGen.Core/Export/natives.proto

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ message NativeReference {
9191

9292
message SharedExample {
9393
string name = 1;
94-
optional string title = 2;
95-
string content = 3;
96-
optional string language = 4;
94+
string title = 2;
95+
repeated SharedExampleCode examples = 3;
96+
}
97+
98+
message SharedExampleCode {
99+
string content = 1;
100+
optional string language = 2;
97101
}

src/NativeCodeGen.Core/Models/SharedExample.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ namespace NativeCodeGen.Core.Models;
33
public class SharedExample
44
{
55
public string Name { get; set; } = string.Empty;
6-
public string? Title { get; set; }
6+
public required string Title { get; set; }
7+
public List<SharedExampleCode> Examples { get; set; } = new();
8+
public string? SourceFile { get; set; }
9+
}
10+
11+
public class SharedExampleCode
12+
{
713
public string Content { get; set; } = string.Empty;
814
public string? Language { get; set; }
9-
public string? SourceFile { get; set; }
1015
}

src/NativeCodeGen.Core/Registry/SharedExampleRegistry.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public SharedExampleRegistry()
2424
.Build();
2525
}
2626

27+
public List<string> Errors { get; } = new();
28+
2729
public void LoadExamples(string directory)
2830
{
2931
if (!Directory.Exists(directory))
@@ -36,14 +38,20 @@ public void LoadExamples(string directory)
3638

3739
// Extract frontmatter and code content
3840
var (frontmatter, body) = ParseFrontmatter(content);
39-
var (language, code) = ParseCodeBlock(body);
41+
42+
if (string.IsNullOrWhiteSpace(frontmatter?.Title))
43+
{
44+
Errors.Add($"{file}: Missing required 'title' in frontmatter");
45+
continue;
46+
}
47+
48+
var codeBlocks = ParseCodeBlocks(body);
4049

4150
_examples[name] = new SharedExample
4251
{
4352
Name = name,
44-
Title = frontmatter?.Title,
45-
Content = code,
46-
Language = language,
53+
Title = frontmatter.Title,
54+
Examples = codeBlocks,
4755
SourceFile = file
4856
};
4957
}
@@ -82,8 +90,9 @@ public void LoadExamples(string directory)
8290
}
8391
}
8492

85-
private static (string? language, string content) ParseCodeBlock(string content)
93+
private static List<SharedExampleCode> ParseCodeBlocks(string content)
8694
{
95+
var blocks = new List<SharedExampleCode>();
8796
var lines = content.Split('\n');
8897
var codeLines = new List<string>();
8998
string? language = null;
@@ -99,14 +108,19 @@ private static (string? language, string content) ParseCodeBlock(string content)
99108
{
100109
// Start of code block - extract language
101110
inCodeBlock = true;
111+
codeLines.Clear();
102112
var langPart = trimmed[3..].Trim();
103-
if (!string.IsNullOrEmpty(langPart))
104-
language = langPart;
113+
language = !string.IsNullOrEmpty(langPart) ? langPart : null;
105114
}
106115
else
107116
{
108-
// End of code block
117+
// End of code block - save it
109118
inCodeBlock = false;
119+
blocks.Add(new SharedExampleCode
120+
{
121+
Content = string.Join("\n", codeLines).Trim(),
122+
Language = language
123+
});
110124
}
111125
}
112126
else if (inCodeBlock)
@@ -115,7 +129,7 @@ private static (string? language, string content) ParseCodeBlock(string content)
115129
}
116130
}
117131

118-
return (language, string.Join("\n", codeLines).Trim());
132+
return blocks;
119133
}
120134

121135
public bool Contains(string name) => _examples.ContainsKey(name);

tests/NativeCodeGen.Tests/Export/DatabaseConverterTests.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using ModelEnumMember = NativeCodeGen.Core.Models.EnumMember;
66
using ModelStructField = NativeCodeGen.Core.Models.StructField;
77
using ModelSharedExample = NativeCodeGen.Core.Models.SharedExample;
8+
using ModelSharedExampleCode = NativeCodeGen.Core.Models.SharedExampleCode;
89

910
namespace NativeCodeGen.Tests.Export;
1011

@@ -335,8 +336,12 @@ public void Convert_WithSharedExamples_ConvertsCorrectly()
335336
["CreatePed"] = new ModelSharedExample
336337
{
337338
Name = "CreatePed",
338-
Content = "local ped = CreatePed(...)",
339-
Language = "lua"
339+
Title = "Creating a Ped",
340+
Examples = new List<ModelSharedExampleCode>
341+
{
342+
new() { Content = "local ped = CreatePed(...)", Language = "lua" },
343+
new() { Content = "const ped = CreatePed(...)", Language = "js" }
344+
}
340345
}
341346
}
342347
};
@@ -347,8 +352,12 @@ public void Convert_WithSharedExamples_ConvertsCorrectly()
347352
Assert.Single(result.SharedExamples);
348353
var example = result.SharedExamples[0];
349354
Assert.Equal("CreatePed", example.Name);
350-
Assert.Equal("local ped = CreatePed(...)", example.Content);
351-
Assert.Equal("lua", example.Language);
355+
Assert.Equal("Creating a Ped", example.Title);
356+
Assert.Equal(2, example.Examples.Count);
357+
Assert.Equal("local ped = CreatePed(...)", example.Examples[0].Content);
358+
Assert.Equal("lua", example.Examples[0].Language);
359+
Assert.Equal("const ped = CreatePed(...)", example.Examples[1].Content);
360+
Assert.Equal("js", example.Examples[1].Language);
352361
}
353362

354363
[Fact]

0 commit comments

Comments
 (0)