import 'dart:io';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
@visibleForTesting
const List<String> dartdocDirectiveCanaryLibraries = <String>[
'animation',
'cupertino',
'material',
'widgets',
'rendering',
'flutter_driver',
];
@visibleForTesting
const List<String> dartdocDirectiveCanaryFiles = <String>[
'Widget-class.html',
'Material-class.html',
'Canvas-class.html',
];
void checkForUnresolvedDirectives(Directory dartDocDir) {
if (!dartDocDir.existsSync()) {
throw Exception('Directory with dartdoc output (${dartDocDir.path}) does not exist.');
}
final List<String> canaryLibraries = dartdocDirectiveCanaryLibraries.toList();
final List<String> canaryFiles = dartdocDirectiveCanaryFiles.toList();
print('Scanning for unresolved dartdoc directives...');
final List<FileSystemEntity> toScan = dartDocDir.listSync();
var count = 0;
while (toScan.isNotEmpty) {
final FileSystemEntity entity = toScan.removeLast();
if (entity is File) {
if (path.extension(entity.path) != '.html') {
continue;
}
canaryFiles.remove(path.basename(entity.path));
count += _scanFile(entity);
} else if (entity is Directory) {
canaryLibraries.remove(path.basename(entity.path));
toScan.addAll(entity.listSync());
} else {
throw Exception('$entity is neither file nor directory.');
}
}
if (canaryLibraries.isNotEmpty) {
throw Exception(
'Did not find docs for the following libraries: ${canaryLibraries.join(', ')}.',
);
}
if (canaryFiles.isNotEmpty) {
throw Exception('Did not find docs for the following files: ${canaryFiles.join(', ')}.');
}
if (count > 0) {
throw Exception('Found $count unresolved dartdoc directives (see log above).');
}
print('No unresolved dartdoc directives detected.');
}
int _scanFile(File file) {
assert(path.extension(file.path) == '.html');
final Iterable<String> matches = _pattern
.allMatches(file.readAsStringSync())
.map((RegExpMatch m) => m.group(0)!);
if (matches.isNotEmpty) {
stderr.writeln('Found unresolved dartdoc directives in ${file.path}:');
for (final match in matches) {
stderr.writeln(' $match');
}
}
return matches.length;
}
final RegExp _pattern = RegExp(r'({@[^}\n]*}?)(?![^<>]*</code)');
void main(List<String> args) {
if (args.length != 1) {
throw Exception('Must provide the path to the dartdoc HTML output as argument.');
}
if (!Directory(args.single).existsSync()) {
throw Exception('The dartdoc HTML output directory ${args.single} does not exist.');
}
checkForUnresolvedDirectives(Directory(args.single));
}