Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[flutter_plugin_tools] Restructure version-check #4111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions script/tool/lib/src/common/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ void printSuccess(String successMessage) {
print(Colorize(successMessage)..green());
}

/// Prints `warningMessage` in yellow.
///
/// Warnings are not surfaced in CI summaries, so this is only useful for
/// highlighting something when someone is already looking though the log
/// messages. DO NOT RELY on someone noticing a warning; instead, use it for
/// things that might be useful to someone debugging an unexpected result.
void printWarning(String warningMessage) {
print(Colorize(warningMessage)..yellow());
}

/// Prints `errorMessage` in red.
void printError(String errorMessage) {
print(Colorize(errorMessage)..red());
Expand Down
9 changes: 9 additions & 0 deletions script/tool/lib/src/common/package_looping_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ abstract class PackageLoopingCommand extends PluginCommand {
/// in the final summary. An empty list indicates success.
Future<List<String>> runForPackage(Directory package);

/// Called during [run] after all calls to [runForPackage]. This provides an
/// opportunity to do any cleanup of run-level state.
Future<void> completeRun() async {}

/// Whether or not the output (if any) of [runForPackage] is long, or short.
///
/// This changes the logging that happens at the start of each package's
Expand Down Expand Up @@ -99,6 +103,9 @@ abstract class PackageLoopingCommand extends PluginCommand {
return packageName;
}

/// The suggested indentation for printed output.
String get indentation => hasLongOutput ? '' : ' ';

// ----------------------------------------

@override
Expand All @@ -115,6 +122,8 @@ abstract class PackageLoopingCommand extends PluginCommand {
results[package] = await runForPackage(package);
}

completeRun();

// If there were any errors reported, summarize them and exit.
if (results.values.any((List<String> failures) => failures.isNotEmpty)) {
const String indentation = ' ';
Expand Down
43 changes: 26 additions & 17 deletions script/tool/lib/src/common/plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ abstract class PluginCommand extends Command<void> {
PluginCommand(
this.packagesDir, {
this.processRunner = const ProcessRunner(),
this.gitDir,
}) {
GitDir? gitDir,
}) : _gitDir = gitDir {
argParser.addMultiOption(
_pluginsArg,
splitCommas: true,
Expand Down Expand Up @@ -76,10 +76,11 @@ abstract class PluginCommand extends Command<void> {
/// This can be overridden for testing.
final ProcessRunner processRunner;

/// The git directory to use. By default it uses the parent directory.
/// The git directory to use. If unset, [gitDir] populates it from the
/// packages directory's enclosing repository.
///
/// This can be mocked for testing.
final GitDir? gitDir;
GitDir? _gitDir;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a late final non-null field, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used as a cache, so cannot be late final.


int? _shardIndex;
int? _shardCount;
Expand All @@ -100,6 +101,26 @@ abstract class PluginCommand extends Command<void> {
return _shardCount!;
}

/// Returns the [GitDir] containing [packagesDir].
Future<GitDir> get gitDir async {
GitDir? gitDir = _gitDir;
if (gitDir != null) {
return gitDir;
}

// Ensure there are no symlinks in the path, as it can break
// GitDir's allowSubdirectory:true.
final String packagesPath = packagesDir.resolveSymbolicLinksSync();
if (!await GitDir.isGitDir(packagesPath)) {
printError('$packagesPath is not a valid Git repository.');
throw ToolExit(2);
}
gitDir =
await GitDir.fromExisting(packagesDir.path, allowSubdirectory: true);
_gitDir = gitDir;
return gitDir;
}

/// Convenience accessor for boolean arguments.
bool getBoolArg(String key) {
return (argResults![key] as bool?) ?? false;
Expand Down Expand Up @@ -285,22 +306,10 @@ abstract class PluginCommand extends Command<void> {
///
/// Throws tool exit if [gitDir] nor root directory is a git directory.
Future<GitVersionFinder> retrieveVersionFinder() async {
final String rootDir = packagesDir.parent.absolute.path;
final String baseSha = getStringArg(_kBaseSha);

GitDir? baseGitDir = gitDir;
if (baseGitDir == null) {
if (!await GitDir.isGitDir(rootDir)) {
printError(
'$rootDir is not a valid Git repository.',
);
throw ToolExit(2);
}
baseGitDir = await GitDir.fromExisting(rootDir);
}

final GitVersionFinder gitVersionFinder =
GitVersionFinder(baseGitDir, baseSha);
GitVersionFinder(await gitDir, baseSha);
return gitVersionFinder;
}

Expand Down
12 changes: 2 additions & 10 deletions script/tool/lib/src/publish_plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,7 @@ class PublishPluginCommand extends PluginCommand {
}

_print('Checking local repo...');
// Ensure there are no symlinks in the path, as it can break
// GitDir's allowSubdirectory:true.
final String packagesPath = packagesDir.resolveSymbolicLinksSync();
if (!await GitDir.isGitDir(packagesPath)) {
_print('$packagesPath is not a valid Git repository.');
throw ToolExit(1);
}
final GitDir baseGitDir = gitDir ??
await GitDir.fromExisting(packagesPath, allowSubdirectory: true);
final GitDir repository = await gitDir;

final bool shouldPushTag = getBoolArg(_pushTagsOption);
_RemoteInfo? remote;
Expand All @@ -179,7 +171,7 @@ class PublishPluginCommand extends PluginCommand {
bool successful;
if (publishAllChanged) {
successful = await _publishAllChangedPackages(
baseGitDir: baseGitDir,
baseGitDir: repository,
remoteForTagPush: remote,
);
} else {
Expand Down
1 change: 0 additions & 1 deletion script/tool/lib/src/pubspec_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class PubspecCheckCommand extends PackageLoopingCommand {
File pubspecFile, {
required String packageName,
}) async {
const String indentation = ' ';
final String contents = pubspecFile.readAsStringSync();
final Pubspec? pubspec = _tryParsePubspec(contents);
if (pubspec == null) {
Expand Down
Loading