@@ -46,6 +46,9 @@ public partial class ToolkitSampleMetadataGenerator
4646 private const string MarkdownRegexSampleTagExpression = @"^>\s*\[!SAMPLE\s*(?<sampleid>.*)\s*\]\s*$" ;
4747 private static readonly Regex MarkdownRegexSampleTag = new Regex ( MarkdownRegexSampleTagExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
4848
49+ private const string FrontMatterRegexIsExperimentalExpression = @"^experimental:\s*(?<experimental>.*)$" ;
50+ private static readonly Regex FrontMatterRegexIsExperimental = new Regex ( FrontMatterRegexIsExperimentalExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
51+
4952 private static void ReportDocumentDiagnostics ( SourceProductionContext ctx , Dictionary < string , ToolkitSampleRecord > sampleMetadata , IEnumerable < AdditionalText > markdownFileData , IEnumerable < ( ToolkitSampleAttribute Attribute , string AttachedQualifiedTypeName , ISymbol Symbol ) > toolkitSampleAttributeData , ImmutableArray < ToolkitFrontMatter > docFrontMatter )
5053 {
5154 // Keep track of all sample ids and remove them as we reference them so we know if we have any unreferenced samples.
@@ -126,6 +129,8 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
126129 var discussion = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexDiscussionId , "discussionid" ) ? . Trim ( ) ;
127130 var issue = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexIssueId , "issueid" ) ? . Trim ( ) ;
128131
132+ var experimental = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexIsExperimental , "experimental" , true ) ? . Trim ( ) ;
133+
129134 // Check we have all the fields we expect to continue (errors will have been spit out otherwise already from the ParseYamlField method)
130135 if ( title == null || description == null || keywords == null ||
131136 category == null || subcategory == null || discussion == null || issue == null || icon == null )
@@ -204,6 +209,18 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
204209 return null ;
205210 }
206211
212+ bool isExperimental = false ;
213+ if ( experimental != null && ! bool . TryParse ( experimental , out isExperimental ) )
214+ {
215+ ctx . ReportDiagnostic (
216+ Diagnostic . Create (
217+ DiagnosticDescriptors . MarkdownYAMLFrontMatterException ,
218+ Location . Create ( file . Path , TextSpan . FromBounds ( 0 , 1 ) , new LinePositionSpan ( LinePosition . Zero , LinePosition . Zero ) ) ,
219+ file . Path ,
220+ "Can't parse optional experimental field, must be a boolean value like 'true' or 'false' or remove it." ) ) ;
221+ return null ;
222+ }
223+
207224 // Finally, construct the complete object.
208225 return new ToolkitFrontMatter ( )
209226 {
@@ -218,16 +235,17 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
218235 DiscussionId = discussionId ,
219236 IssueId = issueId ,
220237 Icon = iconpath ,
238+ IsExperimental = isExperimental ,
221239 } ;
222240 }
223241 } ) . OfType < ToolkitFrontMatter > ( ) . ToImmutableArray ( ) ;
224242 }
225243
226- private string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string captureGroupName )
244+ private string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string captureGroupName , bool optional = false )
227245 {
228246 var match = pattern . Match ( content ) ;
229247
230- if ( ! match . Success )
248+ if ( ! optional && ! match . Success )
231249 {
232250 ctx . ReportDiagnostic (
233251 Diagnostic . Create (
@@ -237,6 +255,10 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
237255 captureGroupName ) ) ;
238256 return null ;
239257 }
258+ else if ( optional && ! match . Success )
259+ {
260+ return null ;
261+ }
240262
241263 return match . Groups [ captureGroupName ] . Value . Trim ( ) ;
242264 }
@@ -270,6 +292,6 @@ private static string FrontMatterToRegistryCall(ToolkitFrontMatter metadata)
270292 var categoryParam = $ "{ nameof ( ToolkitSampleCategory ) } .{ metadata . Category } ";
271293 var subcategoryParam = $ "{ nameof ( ToolkitSampleSubcategory ) } .{ metadata . Subcategory } ";
272294
273- 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 ) } "" }} }};"; // TODO: Add list of sample ids in document
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
274296 }
275297}
0 commit comments