Unverified Commit dcc4b825 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Fix snippets to include element ID in the output sample. (#44787)

This fixes the snippet code output so that it includes the name of the element that it is a snippet for in the first line.
parent 29c026fa
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
// Flutter code sample for {{id}} // Flutter code sample for {{element}}
{{description}} {{description}}
......
...@@ -61,7 +61,7 @@ class SnippetGenerator { ...@@ -61,7 +61,7 @@ class SnippetGenerator {
/// Injects the [injections] into the [template], and turning the /// Injects the [injections] into the [template], and turning the
/// "description" injection into a comment. Only used for /// "description" injection into a comment. Only used for
/// [SnippetType.application] snippets. /// [SnippetType.application] snippets.
String interpolateTemplate(List<_ComponentTuple> injections, String template) { String interpolateTemplate(List<_ComponentTuple> injections, String template, Map<String, Object> metadata) {
final RegExp moustacheRegExp = RegExp('{{([^}]+)}}'); final RegExp moustacheRegExp = RegExp('{{([^}]+)}}');
return template.replaceAllMapped(moustacheRegExp, (Match match) { return template.replaceAllMapped(moustacheRegExp, (Match match) {
if (match[1] == 'description') { if (match[1] == 'description') {
...@@ -86,9 +86,9 @@ class SnippetGenerator { ...@@ -86,9 +86,9 @@ class SnippetGenerator {
// mustache reference, since we want to allow the sections to be // mustache reference, since we want to allow the sections to be
// "optional" in the input: users shouldn't be forced to add an empty // "optional" in the input: users shouldn't be forced to add an empty
// "```dart preamble" section if that section would be empty. // "```dart preamble" section if that section would be empty.
return injections final _ComponentTuple result = injections
.firstWhere((_ComponentTuple tuple) => tuple.name == match[1], orElse: () => null) .firstWhere((_ComponentTuple tuple) => tuple.name == match[1], orElse: () => null);
?.mergedContent ?? ''; return result?.mergedContent ?? (metadata[match[1]] ?? '').toString();
} }
}).trim(); }).trim();
} }
...@@ -130,6 +130,7 @@ class SnippetGenerator { ...@@ -130,6 +130,7 @@ class SnippetGenerator {
'language': language ?? 'dart', 'language': language ?? 'dart',
'serial': '', 'serial': '',
'id': metadata['id'], 'id': metadata['id'],
'element': metadata['element'] ?? '',
'app': '', 'app': '',
}; };
if (type == SnippetType.application) { if (type == SnippetType.application) {
...@@ -242,7 +243,7 @@ class SnippetGenerator { ...@@ -242,7 +243,7 @@ class SnippetGenerator {
exit(1); exit(1);
} }
final String templateContents = _loadFileAsUtf8(templateFile); final String templateContents = _loadFileAsUtf8(templateFile);
String app = interpolateTemplate(snippetData, templateContents); String app = interpolateTemplate(snippetData, templateContents, metadata);
try { try {
app = formatter.format(app); app = formatter.format(app);
......
...@@ -27,10 +27,11 @@ void main() { ...@@ -27,10 +27,11 @@ void main() {
configuration.skeletonsDirectory.createSync(recursive: true); configuration.skeletonsDirectory.createSync(recursive: true);
template = File(path.join(configuration.templatesDirectory.path, 'template.tmpl')); template = File(path.join(configuration.templatesDirectory.path, 'template.tmpl'));
template.writeAsStringSync(''' template.writeAsStringSync('''
// Flutter code sample for {{element}}
{{description}} {{description}}
{{code-preamble}} {{code-my-preamble}}
main() { main() {
{{code}} {{code}}
...@@ -78,6 +79,7 @@ void main() { ...@@ -78,6 +79,7 @@ void main() {
} }
``` ```
'''); ''');
final File outputFile = File(path.join(tmpDir.absolute.path, 'snippet_out.txt'));
final String html = generator.generate( final String html = generator.generate(
inputFile, inputFile,
...@@ -85,7 +87,9 @@ void main() { ...@@ -85,7 +87,9 @@ void main() {
template: 'template', template: 'template',
metadata: <String, Object>{ metadata: <String, Object>{
'id': 'id', 'id': 'id',
'element': 'MyElement',
}, },
output: outputFile,
); );
expect(html, contains('<div>HTML Bits</div>')); expect(html, contains('<div>HTML Bits</div>'));
expect(html, contains('<div>More HTML Bits</div>')); expect(html, contains('<div>More HTML Bits</div>'));
...@@ -97,6 +101,12 @@ void main() { ...@@ -97,6 +101,12 @@ void main() {
'&#47;&#47;\n' '&#47;&#47;\n'
'&#47;&#47; On several lines.\n')); '&#47;&#47; On several lines.\n'));
expect(html, contains('void main() {')); expect(html, contains('void main() {'));
final String outputContents = outputFile.readAsStringSync();
expect(outputContents, contains('// Flutter code sample for MyElement'));
expect(outputContents, contains('A description of the snippet.'));
expect(outputContents, contains('void main() {'));
expect(outputContents, contains("const String name = 'snippet';"));
}); });
test('generates sample snippets', () async { test('generates sample snippets', () async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment