@@ -49,6 +49,9 @@ public partial class ToolkitSampleMetadataGenerator
4949 private const string FrontMatterRegexIsExperimentalExpression = @"^experimental:\s*(?<experimental>.*)$" ;
5050 private static readonly Regex FrontMatterRegexIsExperimental = new Regex ( FrontMatterRegexIsExperimentalExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
5151
52+ private const string CsProjRegexComponentNameExpression = @"^\s*<ToolkitComponentName>(?<ToolkitComponentName>.*)<\/ToolkitComponentName>\s*$" ;
53+ private static readonly Regex CsProjRegexComponentName = new Regex ( CsProjRegexComponentNameExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
54+
5255 private static void ReportDocumentDiagnostics ( SourceProductionContext ctx , Dictionary < string , ToolkitSampleRecord > sampleMetadata , IEnumerable < AdditionalText > markdownFileData , IEnumerable < ( ToolkitSampleAttribute Attribute , string AttachedQualifiedTypeName , ISymbol Symbol ) > toolkitSampleAttributeData , ImmutableArray < ToolkitFrontMatter > docFrontMatter )
5356 {
5457 // Keep track of all sample ids and remove them as we reference them so we know if we have any unreferenced samples.
@@ -90,14 +93,16 @@ private static void ReportDocumentDiagnostics(SourceProductionContext ctx, Dicti
9093 }
9194 }
9295
93- private ImmutableArray < ToolkitFrontMatter > GatherDocumentFrontMatter ( SourceProductionContext ctx , IEnumerable < AdditionalText > data )
96+ private static ImmutableArray < ToolkitFrontMatter > GatherDocumentFrontMatter ( SourceProductionContext ctx , IEnumerable < ( AdditionalText Document , AdditionalText ? CsProj ) > data )
9497 {
95- return data . Select ( file =>
98+ return data . Select ( info =>
9699 {
100+ var file = info . Document ;
101+
97102 // We have to manually parse the YAML here for now because of
98103 // https://github.com/dotnet/roslyn/issues/43903
99104
100- var content = file . GetText ( ) ! . ToString ( ) ;
105+ var content = info . Document . GetText ( ) ! . ToString ( ) ;
101106 var matter = content . Split ( new [ ] { "---" } , StringSplitOptions . RemoveEmptyEntries ) ;
102107
103108 if ( matter . Length <= 1 )
@@ -221,6 +226,28 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
221226 return null ;
222227 }
223228
229+ string ? componentName = null ;
230+ string ? csprojName = null ;
231+
232+ // Get component name from csproj file (if we have one) and its filename, otherwise, use path data of doc file
233+ if ( info . CsProj != null )
234+ {
235+ var text = info . CsProj . GetText ( ) ! . ToString ( ) ;
236+
237+ var match = CsProjRegexComponentName . Match ( text ) ;
238+
239+ if ( match . Success )
240+ {
241+ componentName = match . Groups [ "ToolkitComponentName" ] . Value . Trim ( ) ;
242+ }
243+
244+ csprojName = info . CsProj . Path . Split ( new char [ ] { '/' , '\\ ' } ) . LastOrDefault ( ) ;
245+ }
246+ else
247+ {
248+ componentName = filepath . Split ( new char [ ] { '/' , '\\ ' } ) . FirstOrDefault ( ) ;
249+ }
250+
224251 // Finally, construct the complete object.
225252 return new ToolkitFrontMatter ( )
226253 {
@@ -236,12 +263,14 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
236263 IssueId = issueId ,
237264 Icon = iconpath ,
238265 IsExperimental = isExperimental ,
266+ ComponentName = componentName ,
267+ CsProjName = csprojName ,
239268 } ;
240269 }
241270 } ) . OfType < ToolkitFrontMatter > ( ) . ToImmutableArray ( ) ;
242271 }
243272
244- private string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string captureGroupName , bool optional = false )
273+ private static string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string captureGroupName , bool optional = false )
245274 {
246275 var match = pattern . Match ( content ) ;
247276
@@ -263,7 +292,7 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
263292 return match . Groups [ captureGroupName ] . Value . Trim ( ) ;
264293 }
265294
266- private void CreateDocumentRegistry ( SourceProductionContext ctx , ImmutableArray < ToolkitFrontMatter > matter )
295+ private static void CreateDocumentRegistry ( SourceProductionContext ctx , ImmutableArray < ToolkitFrontMatter > matter )
267296 {
268297 // TODO: Emit a better error that no documentation is here?
269298 if ( matter . Length == 0 )
@@ -292,6 +321,6 @@ private static string FrontMatterToRegistryCall(ToolkitFrontMatter metadata)
292321 var categoryParam = $ "{ nameof ( ToolkitSampleCategory ) } .{ metadata . Category } ";
293322 var subcategoryParam = $ "{ nameof ( ToolkitSampleSubcategory ) } .{ metadata . Subcategory } ";
294323
295- return @$ "yield return new { typeof ( ToolkitFrontMatter ) . FullName } () {{ Title = ""{ metadata . Title } "", Author = ""{ metadata . Author } "", Description = ""{ metadata . Description } "", Keywords = ""{ metadata . Keywords } "", Category = { categoryParam } , Subcategory = { subcategoryParam } , DiscussionId = { metadata . DiscussionId } , IssueId = { metadata . IssueId } , Icon = @""{ metadata . Icon } "", FilePath = @""{ metadata . FilePath } "", SampleIdReferences = new string[] {{ ""{ string . Join ( "\" ,\" " , metadata . SampleIdReferences ) } "" }}, IsExperimental = { metadata . IsExperimental . ToString ( ) . ToLowerInvariant ( ) } }};"; // TODO: Add list of sample ids in document
324+ return @$ "yield return new { typeof ( ToolkitFrontMatter ) . FullName } () {{ ComponentName = "" { metadata . ComponentName } "", Title = ""{ metadata . Title } "", Author = ""{ metadata . Author } "", Description = ""{ metadata . Description } "", Keywords = ""{ metadata . Keywords } "", Category = { categoryParam } , Subcategory = { subcategoryParam } , DiscussionId = { metadata . DiscussionId } , IssueId = { metadata . IssueId } , Icon = @""{ metadata . Icon } "", FilePath = @""{ metadata . FilePath } "", SampleIdReferences = new string[] {{ ""{ string . Join ( "\" ,\" " , metadata . SampleIdReferences ) } "" }}, IsExperimental = { metadata . IsExperimental . ToString ( ) . ToLowerInvariant ( ) } , CsProjName = @"" { metadata . CsProjName } "" }};"; // TODO: Add list of sample ids in document
296325 }
297326}
0 commit comments