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

Add channel-specific dartpad URL to snippet generation (#49845)

Adds the channel to docs generation for dartpad samples, since the new query parameter was added in dart-lang/dart-pad#1462
parent b953c3e5
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
</div> </div>
<div class="snippet-container"> <div class="snippet-container">
<div class="snippet" id="longSnippet{{serial}}"> <div class="snippet" id="longSnippet{{serial}}">
<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}"></iframe> <iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}&sample_channel={{channel}}"></iframe>
</div> </div>
<div class="snippet" id="shortSnippet{{serial}}" hidden> <div class="snippet" id="shortSnippet{{serial}}" hidden>
{{description}} {{description}}
......
...@@ -22,6 +22,16 @@ const String _kTemplateOption = 'template'; ...@@ -22,6 +22,16 @@ const String _kTemplateOption = 'template';
const String _kTypeOption = 'type'; const String _kTypeOption = 'type';
const String _kShowDartPad = 'dartpad'; const String _kShowDartPad = 'dartpad';
String getChannelName() {
final RegExp gitBranchRegexp = RegExp(r'^## (.*)');
final ProcessResult gitResult = Process.runSync('git', <String>['status', '-b', '--porcelain']);
if (gitResult.exitCode != 0)
throw 'git status exit with non-zero exit code: ${gitResult.exitCode}';
final Match gitBranchMatch = gitBranchRegexp.firstMatch(
(gitResult.stdout as String).trim().split('\n').first);
return gitBranchMatch == null ? '<unknown>' : gitBranchMatch.group(1).split('...').first;
}
/// Generates snippet dartdoc output for a given input, and creates any sample /// Generates snippet dartdoc output for a given input, and creates any sample
/// applications needed by the snippet. /// applications needed by the snippet.
void main(List<String> argList) { void main(List<String> argList) {
...@@ -176,6 +186,7 @@ void main(List<String> argList) { ...@@ -176,6 +186,7 @@ void main(List<String> argList) {
? int.tryParse(environment['SOURCE_LINE']) ? int.tryParse(environment['SOURCE_LINE'])
: null, : null,
'id': id.join('.'), 'id': id.join('.'),
'channel': getChannelName(),
'serial': serial, 'serial': serial,
'package': packageName, 'package': packageName,
'library': libraryName, 'library': libraryName,
......
...@@ -124,12 +124,19 @@ class SnippetGenerator { ...@@ -124,12 +124,19 @@ class SnippetGenerator {
description = description.trim().isNotEmpty description = description.trim().isNotEmpty
? '<div class="snippet-description">{@end-inject-html}$description{@inject-html}</div>' ? '<div class="snippet-description">{@end-inject-html}$description{@inject-html}</div>'
: ''; : '';
// DartPad only supports stable or master as valid channels. Use master
// if not on stable so that local runs will work (although they will
// still take their sample code from the master docs server).
final String channel = metadata['channel'] == 'stable' ? 'stable' : 'master';
final Map<String, String> substitutions = <String, String>{ final Map<String, String> substitutions = <String, String>{
'description': description, 'description': description,
'code': htmlEscape.convert(result.join('\n')), 'code': htmlEscape.convert(result.join('\n')),
'language': language ?? 'dart', 'language': language ?? 'dart',
'serial': '', 'serial': '',
'id': metadata['id'] as String, 'id': metadata['id'] as String,
'channel': channel,
'element': metadata['element'] as String ?? '', 'element': metadata['element'] as String ?? '',
'app': '', 'app': '',
}; };
...@@ -226,8 +233,7 @@ class SnippetGenerator { ...@@ -226,8 +233,7 @@ class SnippetGenerator {
assert(template != null || type != SnippetType.sample); assert(template != null || type != SnippetType.sample);
assert(metadata != null && metadata['id'] != null); assert(metadata != null && metadata['id'] != null);
assert(input != null); assert(input != null);
assert(!showDartPad || type == SnippetType.sample, assert(!showDartPad || type == SnippetType.sample, 'Only application samples work with dartpad.');
'Only application snippets work with dartpad.');
final List<_ComponentTuple> snippetData = parseInput(_loadFileAsUtf8(input)); final List<_ComponentTuple> snippetData = parseInput(_loadFileAsUtf8(input));
switch (type) { switch (type) {
case SnippetType.sample: case SnippetType.sample:
...@@ -264,9 +270,7 @@ class SnippetGenerator { ...@@ -264,9 +270,7 @@ class SnippetGenerator {
(_ComponentTuple data) => data.name == 'description', (_ComponentTuple data) => data.name == 'description',
orElse: () => null, orElse: () => null,
); );
metadata ??= <String, Object>{};
metadata.addAll(<String, Object>{ metadata.addAll(<String, Object>{
'id': metadata['id'],
'file': path.basename(outputFile.path), 'file': path.basename(outputFile.path),
'description': description?.mergedContent, 'description': description?.mergedContent,
}); });
......
...@@ -52,7 +52,7 @@ main() { ...@@ -52,7 +52,7 @@ main() {
'''); ''');
configuration.getHtmlSkeletonFile(SnippetType.sample, showDartPad: true).writeAsStringSync(''' configuration.getHtmlSkeletonFile(SnippetType.sample, showDartPad: true).writeAsStringSync('''
<div>HTML Bits (DartPad-style)</div> <div>HTML Bits (DartPad-style)</div>
<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}"></iframe> <iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id={{id}}&sample_channel={{channel}}"></iframe>
<div>More HTML Bits</div> <div>More HTML Bits</div>
'''); ''');
generator = SnippetGenerator(configuration: configuration); generator = SnippetGenerator(configuration: configuration);
...@@ -61,7 +61,7 @@ main() { ...@@ -61,7 +61,7 @@ main() {
tmpDir.deleteSync(recursive: true); tmpDir.deleteSync(recursive: true);
}); });
test('generates application snippets', () async { test('generates samples', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt')) final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(r''' ..writeAsStringSync(r'''
...@@ -87,6 +87,7 @@ void main() { ...@@ -87,6 +87,7 @@ void main() {
template: 'template', template: 'template',
metadata: <String, Object>{ metadata: <String, Object>{
'id': 'id', 'id': 'id',
'channel': 'stable',
'element': 'MyElement', 'element': 'MyElement',
}, },
output: outputFile, output: outputFile,
...@@ -95,6 +96,7 @@ void main() { ...@@ -95,6 +96,7 @@ void main() {
expect(html, contains('<div>More HTML Bits</div>')); expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains(r'print(&#39;The actual $name.&#39;);')); expect(html, contains(r'print(&#39;The actual $name.&#39;);'));
expect(html, contains('A description of the snippet.\n')); expect(html, contains('A description of the snippet.\n'));
expect(html, isNot(contains('sample_channel=stable')));
expect( expect(
html, html,
contains('&#47;&#47; A description of the snippet.\n' contains('&#47;&#47; A description of the snippet.\n'
...@@ -109,7 +111,7 @@ void main() { ...@@ -109,7 +111,7 @@ void main() {
expect(outputContents, contains("const String name = 'snippet';")); expect(outputContents, contains("const String name = 'snippet';"));
}); });
test('generates sample snippets', () async { test('generates snippets', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt')) final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(r''' ..writeAsStringSync(r'''
...@@ -137,7 +139,7 @@ void main() { ...@@ -137,7 +139,7 @@ void main() {
expect(html, contains('main() {')); expect(html, contains('main() {'));
}); });
test('generates dartpad snippets', () async { test('generates dartpad samples', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt')) final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(r''' ..writeAsStringSync(r'''
...@@ -157,14 +159,14 @@ void main() { ...@@ -157,14 +159,14 @@ void main() {
SnippetType.sample, SnippetType.sample,
showDartPad: true, showDartPad: true,
template: 'template', template: 'template',
metadata: <String, Object>{'id': 'id'}, metadata: <String, Object>{'id': 'id', 'channel': 'stable'},
); );
expect(html, contains('<div>HTML Bits (DartPad-style)</div>')); expect(html, contains('<div>HTML Bits (DartPad-style)</div>'));
expect(html, contains('<div>More HTML Bits</div>')); expect(html, contains('<div>More HTML Bits</div>'));
expect(html, contains('<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=id"></iframe>')); expect(html, contains('<iframe class="snippet-dartpad" src="https://dartpad.dev/embed-flutter.html?split=60&run=true&sample_id=id&sample_channel=stable"></iframe>'));
}); });
test('generates snippet application metadata', () async { test('generates sample metadata', () async {
final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt')) final File inputFile = File(path.join(tmpDir.absolute.path, 'snippet_in.txt'))
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(r''' ..writeAsStringSync(r'''
...@@ -187,11 +189,12 @@ void main() { ...@@ -187,11 +189,12 @@ void main() {
SnippetType.sample, SnippetType.sample,
template: 'template', template: 'template',
output: outputFile, output: outputFile,
metadata: <String, Object>{'sourcePath': 'some/path.dart', 'id': 'id'}, metadata: <String, Object>{'sourcePath': 'some/path.dart', 'id': 'id', 'channel': 'stable'},
); );
expect(expectedMetadataFile.existsSync(), isTrue); expect(expectedMetadataFile.existsSync(), isTrue);
final Map<String, dynamic> json = jsonDecode(expectedMetadataFile.readAsStringSync()) as Map<String, dynamic>; final Map<String, dynamic> json = jsonDecode(expectedMetadataFile.readAsStringSync()) as Map<String, dynamic>;
expect(json['id'], equals('id')); expect(json['id'], equals('id'));
expect(json['channel'], equals('stable'));
expect(json['file'], equals('snippet_out.dart')); expect(json['file'], equals('snippet_out.dart'));
expect(json['description'], equals('A description of the snippet.\n\nOn several lines.')); expect(json['description'], equals('A description of the snippet.\n\nOn several lines.'));
// Ensure any passed metadata is included in the output JSON too. // Ensure any passed metadata is included in the output JSON too.
......
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