Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert to walking examples internally
  • Loading branch information
stuartmorgan-g committed May 17, 2022
commit d1c2710c54d862ababfc0ec1df20820bfa1f1c7d
6 changes: 0 additions & 6 deletions script/tool/lib/src/common/repository_package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ class RepositoryPackage {
!isPlatformInterface &&
directory.basename != directory.parent.basename;

/// True if the package is an example for another package.
bool get isExample =>
(directory.basename == 'example' && isPackage(directory.parent)) ||
(directory.parent.basename == 'example' &&
isPackage(directory.parent.parent));

/// Returns the Flutter example packages contained in the package, if any.
Iterable<RepositoryPackage> getExamples() {
final Directory exampleDirectory = directory.childDirectory('example');
Expand Down
52 changes: 32 additions & 20 deletions script/tool/lib/src/readme_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,37 @@ class ReadmeCheckCommand extends PackageLoopingCommand {
@override
bool get hasLongOutput => false;

@override
PackageLoopingType get packageLoopingType =>
PackageLoopingType.includeExamples;

@override
Future<PackageResult> runForPackage(RepositoryPackage package) async {
final File readme = package.readmeFile;
final List<String> errors = _validateReadme(package.readmeFile,
mainPackage: package, isExample: false);
for (final RepositoryPackage packageToCheck in package.getExamples()) {
errors.addAll(_validateReadme(packageToCheck.readmeFile,
mainPackage: package, isExample: true));
}

return errors.isEmpty
? PackageResult.success()
: PackageResult.fail(errors);
}

List<String> _validateReadme(File readme,
{required RepositoryPackage mainPackage, required bool isExample}) {
if (!readme.existsSync()) {
if (package.isExample) {
return PackageResult.skip('No README.md for example');
if (isExample) {
printError('No README for '
'${getRelativePosixPath(readme.parent, from: mainPackage.directory)}');
return <String>[];
} else {
return PackageResult.fail(<String>['Missing README.md']);
printError('No README found at '
'${getRelativePosixPath(readme, from: mainPackage.directory)}');
return <String>['Missing README.md'];
}
}

final List<String> readmeLines = readme.readAsLinesSync();
final List<String> errors = <String>[];

final Pubspec pubspec = package.parsePubspec();
final bool isPlugin = pubspec.flutter?['plugin'] != null;

final List<String> readmeLines = package.readmeFile.readAsLinesSync();

final String? blockValidationError = _validateCodeBlocks(readmeLines);
if (blockValidationError != null) {
errors.add(blockValidationError);
Expand All @@ -86,16 +94,20 @@ class ReadmeCheckCommand extends PackageLoopingCommand {
errors.add('Contains template boilerplate');
}

if (isPlugin && (!package.isFederated || package.isAppFacing)) {
final String? error = _validateSupportedPlatforms(readmeLines, pubspec);
if (error != null) {
errors.add(error);
// Check if this is the main readme for a plugin, and if so enforce extra
// checks.
if (!isExample) {
final Pubspec pubspec = mainPackage.parsePubspec();
final bool isPlugin = pubspec.flutter?['plugin'] != null;
if (isPlugin && (!mainPackage.isFederated || mainPackage.isAppFacing)) {
final String? error = _validateSupportedPlatforms(readmeLines, pubspec);
if (error != null) {
errors.add(error);
}
}
}

return errors.isEmpty
? PackageResult.success()
: PackageResult.fail(errors);
return errors;
}

/// Validates that code blocks (``` ... ```) follow repository standards.
Expand Down
4 changes: 2 additions & 2 deletions script/tool/test/readme_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void main() {
);
});

test('skips when example README is missing', () async {
test('passes when example README is missing', () async {
createFakePackage('a_package', packagesDir);

final List<String> output =
Expand All @@ -66,7 +66,7 @@ void main() {
expect(
output,
containsAllInOrder(<Matcher>[
contains('No README.md for example'),
contains('No README for example'),
]),
);
});
Expand Down