Skip to content

Commit cb309bc

Browse files
Publish check (flutter#3556)
1 parent cbee856 commit cb309bc

File tree

3 files changed

+95
-25
lines changed

3 files changed

+95
-25
lines changed

script/check_publish.sh

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,9 @@ readonly REPO_DIR="$(dirname "$SCRIPT_DIR")"
1010

1111
source "$SCRIPT_DIR/common.sh"
1212

13-
function check_publish() {
14-
local failures=()
15-
for dir in $(plugin_tools list --plugins="$1"); do
16-
local package_name=$(basename "$dir")
17-
18-
echo "Checking that $package_name can be published."
19-
if [[ $(cd "$dir" && cat pubspec.yaml | grep -E "^publish_to: none") ]]; then
20-
echo "Package $package_name is marked as unpublishable. Skipping."
21-
elif (cd "$dir" && flutter pub publish -- --dry-run > /dev/null); then
22-
echo "Package $package_name is able to be published."
23-
else
24-
error "Unable to publish $package_name"
25-
failures=("${failures[@]}" "$package_name")
26-
fi
27-
done
28-
if [[ "${#failures[@]}" != 0 ]]; then
29-
error "FAIL: The following ${#failures[@]} package(s) failed the publishing check:"
30-
for failure in "${failures[@]}"; do
31-
error "$failure"
32-
done
33-
fi
34-
return "${#failures[@]}"
35-
}
36-
3713
# Sets CHANGED_PACKAGE_LIST and CHANGED_PACKAGES
3814
check_changed_packages
3915

4016
if [[ "${#CHANGED_PACKAGE_LIST[@]}" != 0 ]]; then
41-
check_publish "${CHANGED_PACKAGES}"
17+
plugin_tools publish-check --plugins="${CHANGED_PACKAGES}"
4218
fi

script/tool/lib/src/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:io' as io;
77
import 'package:args/command_runner.dart';
88
import 'package:file/file.dart';
99
import 'package:file/local.dart';
10+
import 'package:flutter_plugin_tools/src/publish_check_command.dart';
1011
import 'package:flutter_plugin_tools/src/publish_plugin_command.dart';
1112
import 'package:path/path.dart' as p;
1213

@@ -51,6 +52,7 @@ void main(List<String> args) {
5152
..addCommand(JavaTestCommand(packagesDir, fileSystem))
5253
..addCommand(LintPodspecsCommand(packagesDir, fileSystem))
5354
..addCommand(ListCommand(packagesDir, fileSystem))
55+
..addCommand(PublishCheckCommand(packagesDir, fileSystem))
5456
..addCommand(PublishPluginCommand(packagesDir, fileSystem))
5557
..addCommand(TestCommand(packagesDir, fileSystem))
5658
..addCommand(VersionCheckCommand(packagesDir, fileSystem))
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)