Fix samples so that they present properly in the docs (#97224)
This commit is contained in:
@@ -381,6 +381,25 @@ void cleanOutSnippets() {
|
||||
}
|
||||
}
|
||||
|
||||
void _sanityCheckExample(File file, RegExp regExp) {
|
||||
if (file.existsSync()) {
|
||||
final List<String> contents = file.readAsLinesSync();
|
||||
bool found = false;
|
||||
for (final String line in contents) {
|
||||
if (regExp.matchAsPrefix(line) != null) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
throw Exception("Missing example code in ${file.path}. Either it didn't get published, publishing has changed, or the example no longer exists.");
|
||||
}
|
||||
} else {
|
||||
throw Exception("Missing example code sanity test file ${file.path}. Either it didn't get published, or you might have to update the test to look at a different file.");
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs a sanity check by running a test.
|
||||
void sanityCheckDocs() {
|
||||
final List<String> canaries = <String>[
|
||||
'$kPublishRoot/assets/overrides.css',
|
||||
@@ -397,6 +416,22 @@ void sanityCheckDocs() {
|
||||
if (!File(canary).existsSync())
|
||||
throw Exception('Missing "$canary", which probably means the documentation failed to build correctly.');
|
||||
}
|
||||
// Make sure at least one example of each kind includes source code.
|
||||
|
||||
// Check a "sample" example, any one will do.
|
||||
final File sampleExample = File('$kPublishRoot/api/widgets/showGeneralDialog.html');
|
||||
final RegExp sampleRegExp = RegExp(r'\s*<pre id="longSnippet1" class="language-dart">\s*<code class="language-dart">\s*import 'package:flutter/material.dart';');
|
||||
_sanityCheckExample(sampleExample, sampleRegExp);
|
||||
|
||||
// Check a "snippet" example, any one will do.
|
||||
final File snippetExample = File('$kPublishRoot/api/widgets/ModalRoute/barrierColor.html');
|
||||
final RegExp snippetRegExp = RegExp(r'\s*<pre class="language-dart" id="sample-code">.*Color get barrierColor => Theme\.of\(navigator\.context\)\.backgroundColor;.*</pre>');
|
||||
_sanityCheckExample(snippetExample, snippetRegExp);
|
||||
|
||||
// Check a "dartpad" example, any one will do.
|
||||
final File dartpadExample = File('$kPublishRoot/api/widgets/PhysicalShape-class.html');
|
||||
final RegExp dartpadRegExp = RegExp(r'\s*<iframe class="snippet-dartpad" src="https://dartpad\.dev.*sample_id=widgets\.PhysicalShape\.\d+.*">\s*</iframe>');
|
||||
_sanityCheckExample(dartpadExample, dartpadRegExp);
|
||||
}
|
||||
|
||||
/// Creates a custom index.html because we try to maintain old
|
||||
|
||||
Reference in New Issue
Block a user