|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:colorize/colorize.dart'; |
| 8 | +import 'package:file/file.dart'; |
| 9 | +import 'package:pubspec_parse/pubspec_parse.dart'; |
| 10 | + |
| 11 | +import 'common.dart'; |
| 12 | + |
| 13 | +class PublishCheckCommand extends PluginCommand { |
| 14 | + PublishCheckCommand( |
| 15 | + Directory packagesDir, |
| 16 | + FileSystem fileSystem, { |
| 17 | + ProcessRunner processRunner = const ProcessRunner(), |
| 18 | + }) : super(packagesDir, fileSystem, processRunner: processRunner); |
| 19 | + |
| 20 | + @override |
| 21 | + final String name = 'publish-check'; |
| 22 | + |
| 23 | + @override |
| 24 | + final String description = |
| 25 | + 'Checks to make sure that a plugin *could* be published.'; |
| 26 | + |
| 27 | + @override |
| 28 | + Future<Null> run() async { |
| 29 | + checkSharding(); |
| 30 | + final List<Directory> failedPackages = <Directory>[]; |
| 31 | + |
| 32 | + await for (Directory plugin in getPlugins()) { |
| 33 | + if (!(await passesPublishCheck(plugin))) failedPackages.add(plugin); |
| 34 | + } |
| 35 | + |
| 36 | + if (failedPackages.isNotEmpty) { |
| 37 | + final String error = |
| 38 | + 'FAIL: The following ${failedPackages.length} package(s) failed the ' |
| 39 | + 'publishing check:'; |
| 40 | + final String joinedFailedPackages = failedPackages.join('\n'); |
| 41 | + |
| 42 | + final Colorize colorizedError = Colorize('$error\n$joinedFailedPackages') |
| 43 | + ..red(); |
| 44 | + print(colorizedError); |
| 45 | + throw ToolExit(1); |
| 46 | + } |
| 47 | + |
| 48 | + final Colorize passedMessage = |
| 49 | + Colorize('All packages passed publish check!')..green(); |
| 50 | + print(passedMessage); |
| 51 | + } |
| 52 | + |
| 53 | + Pubspec tryParsePubspec(Directory package) { |
| 54 | + final File pubspecFile = package.childFile('pubspec.yaml'); |
| 55 | + |
| 56 | + try { |
| 57 | + return Pubspec.parse(pubspecFile.readAsStringSync()); |
| 58 | + } on Exception catch (exception) { |
| 59 | + print( |
| 60 | + 'Failed to parse `pubspec.yaml` at ${pubspecFile.path}: $exception}', |
| 61 | + ); |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Future<bool> passesPublishCheck(Directory package) async { |
| 67 | + final String packageName = package.basename; |
| 68 | + print('Checking that $packageName can be published.'); |
| 69 | + |
| 70 | + final Pubspec pubspec = tryParsePubspec(package); |
| 71 | + if (pubspec == null) { |
| 72 | + return false; |
| 73 | + } else if (pubspec.publishTo == 'none') { |
| 74 | + print('Package $packageName is marked as unpublishable. Skipping.'); |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + final int exitCode = await processRunner.runAndStream( |
| 79 | + 'flutter', |
| 80 | + <String>['pub', 'publish', '--', '--dry-run'], |
| 81 | + workingDir: package, |
| 82 | + ); |
| 83 | + |
| 84 | + if (exitCode == 0) { |
| 85 | + print("Package $packageName is able to be published."); |
| 86 | + return true; |
| 87 | + } else { |
| 88 | + print('Unable to publish $packageName'); |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments