Skip to content

Commit 895399a

Browse files
Validate Document Registry Generation
Fix issue with Author missing Fix issue with old path setup for markdown file path
1 parent f3f17ba commit 895399a

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

CommunityToolkit.Tooling.SampleGen.Tests/Helpers/TestHelpers.Compilation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal static GeneratorDriver WithMarkdown(this GeneratorDriver driver, params
5050
{
5151
if (!string.IsNullOrWhiteSpace(markdown))
5252
{
53-
var text = new InMemoryAdditionalText(@"C:\pathtorepo\components\experiment\samples\experiment.Samples\documentation.md", markdown);
53+
var text = new InMemoryAdditionalText(@"C:\pathtorepo\components\experiment\samples\documentation.md", markdown);
5454
driver = driver.AddAdditionalTexts(ImmutableArray.Create<AdditionalText>(text));
5555
}
5656
}

CommunityToolkit.Tooling.SampleGen.Tests/ToolkitSampleMetadataTests.Documentation.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void DocumentationMissingSample()
141141
}
142142

143143
[TestMethod]
144-
public void DocumentationValid()
144+
public void DocumentationValidNoDiagnostics()
145145
{
146146
string markdown = @"---
147147
title: Canvas Layout
@@ -216,4 +216,51 @@ public void DocumentationInvalidIssueId()
216216
result.AssertNoCompilationErrors();
217217
result.AssertDiagnosticsAre(DiagnosticDescriptors.MarkdownYAMLFrontMatterException, DiagnosticDescriptors.DocumentationHasNoSamples);
218218
}
219+
220+
[TestMethod]
221+
public void DocumentationValidWithRegistry()
222+
{
223+
string markdown = @"---
224+
title: Canvas Layout
225+
author: mhawker
226+
description: A canvas-like VirtualizingLayout for use in an ItemsRepeater
227+
keywords: CanvasLayout, ItemsRepeater, VirtualizingLayout, Canvas, Layout, Panel, Arrange
228+
dev_langs:
229+
- csharp
230+
category: Controls
231+
subcategory: Layout
232+
discussion-id: 0
233+
issue-id: 0
234+
icon: assets/icon.png
235+
---
236+
# This is some test documentation...
237+
Which is valid.
238+
> [!SAMPLE Sample]";
239+
240+
var sampleProjectAssembly = SimpleSource.ToSyntaxTree()
241+
.CreateCompilation("MyApp.Samples")
242+
.ToMetadataReference();
243+
244+
var headCompilation = string.Empty
245+
.ToSyntaxTree()
246+
.CreateCompilation("MyApp.Head")
247+
.AddReferences(sampleProjectAssembly);
248+
249+
var result = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>(markdown);
250+
251+
result.AssertNoCompilationErrors();
252+
253+
Assert.AreEqual(result.Compilation.GetFileContentsByName("ToolkitDocumentRegistry.g.cs"), """
254+
#nullable enable
255+
namespace CommunityToolkit.Tooling.SampleGen;
256+
257+
public static class ToolkitDocumentRegistry
258+
{
259+
public static System.Collections.Generic.IEnumerable<CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitFrontMatter> Execute()
260+
{
261+
yield return new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitFrontMatter() { Title = "Canvas Layout", Author = "mhawker", Description = "A canvas-like VirtualizingLayout for use in an ItemsRepeater", Keywords = "CanvasLayout, ItemsRepeater, VirtualizingLayout, Canvas, Layout, Panel, Arrange", Category = ToolkitSampleCategory.Controls, Subcategory = ToolkitSampleSubcategory.Layout, DiscussionId = 0, IssueId = 0, Icon = @"experiment/samples/assets/icon.png", FilePath = @"experiment\samples\documentation.md", SampleIdReferences = new string[] { "Sample" } };
262+
}
263+
}
264+
""", "Unexpected code generated");
265+
}
219266
}

CommunityToolkit.Tooling.SampleGen/ToolkitSampleMetadataGenerator.Documentation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public partial class ToolkitSampleMetadataGenerator
1919
private const string FrontMatterRegexTitleExpression = @"^title:\s*(?<title>.*)$";
2020
private static readonly Regex FrontMatterRegexTitle = new Regex(FrontMatterRegexTitleExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
2121

22+
private const string FrontMatterRegexAuthorExpression = @"^author:\s*(?<author>.*)$";
23+
private static readonly Regex FrontMatterRegexAuthor = new Regex(FrontMatterRegexAuthorExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
24+
2225
private const string FrontMatterRegexDescriptionExpression = @"^description:\s*(?<description>.*)$";
2326
private static readonly Regex FrontMatterRegexDescription = new Regex(FrontMatterRegexDescriptionExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
2427

@@ -110,6 +113,7 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
110113

111114
// Grab all front matter fields using RegEx expressions.
112115
var title = ParseYamlField(ref ctx, file.Path, ref frontmatter, FrontMatterRegexTitle, "title");
116+
var author = ParseYamlField(ref ctx, file.Path, ref frontmatter, FrontMatterRegexAuthor, "author");
113117
var description = ParseYamlField(ref ctx, file.Path, ref frontmatter, FrontMatterRegexDescription, "description");
114118
var keywords = ParseYamlField(ref ctx, file.Path, ref frontmatter, FrontMatterRegexKeywords, "keywords");
115119

@@ -204,6 +208,7 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
204208
return new ToolkitFrontMatter()
205209
{
206210
Title = title!,
211+
Author = author!,
207212
Description = description!,
208213
Keywords = keywords!,
209214
Category = categoryValue,

CommunityToolkit.Tooling.SampleGen/ToolkitSampleMetadataGenerator.Sample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3838
var assemblyName = context.CompilationProvider.Select((x, _) => x.Assembly.Name);
3939

4040
// Only generate diagnostics (sample projects)
41-
// Skip creating the registry for symbols in the executing assembly. This would place an incomplete registry in each sample project and cause compiler erorrs.
41+
// Skip creating the registry for symbols in the executing assembly. This would place an incomplete registry in each sample project and cause compiler errors.
4242
Execute(symbolsInExecutingAssembly, skipRegistry: true);
4343

4444
// Only generate the registry (project head)

0 commit comments

Comments
 (0)