From 8177b5a9fe0c3c72bbdc64341e22b37a7c990093 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Mon, 10 Jul 2023 16:08:25 -0400 Subject: [PATCH 1/3] Consolidate color handling --- script/tool/lib/src/analyze_command.dart | 2 +- script/tool/lib/src/build_examples_command.dart | 1 + script/tool/lib/src/common/cmake.dart | 1 + script/tool/lib/src/common/core.dart | 11 ----------- script/tool/lib/src/common/package_command.dart | 1 + .../tool/lib/src/common/package_looping_command.dart | 8 ++++---- script/tool/lib/src/common/xcode.dart | 2 +- .../tool/lib/src/create_all_packages_app_command.dart | 1 + script/tool/lib/src/dart_test_command.dart | 1 + script/tool/lib/src/dependabot_check_command.dart | 2 +- script/tool/lib/src/drive_examples_command.dart | 1 + .../tool/lib/src/federation_safety_check_command.dart | 2 +- script/tool/lib/src/firebase_test_lab_command.dart | 1 + script/tool/lib/src/fix_command.dart | 2 +- script/tool/lib/src/format_command.dart | 1 + script/tool/lib/src/gradle_check_command.dart | 1 + script/tool/lib/src/license_check_command.dart | 1 + script/tool/lib/src/lint_android_command.dart | 1 + script/tool/lib/src/make_deps_path_based_command.dart | 1 + script/tool/lib/src/native_test_command.dart | 1 + script/tool/lib/src/podspec_check_command.dart | 1 + script/tool/lib/src/publish_check_command.dart | 2 +- script/tool/lib/src/publish_command.dart | 1 + script/tool/lib/src/pubspec_check_command.dart | 1 + script/tool/lib/src/readme_check_command.dart | 1 + script/tool/lib/src/update_dependency_command.dart | 1 + script/tool/lib/src/update_excerpts_command.dart | 2 +- script/tool/lib/src/update_min_sdk_command.dart | 1 + script/tool/lib/src/update_release_info_command.dart | 2 +- script/tool/lib/src/version_check_command.dart | 2 +- script/tool/lib/src/xcode_analyze_command.dart | 1 + 31 files changed, 33 insertions(+), 24 deletions(-) diff --git a/script/tool/lib/src/analyze_command.dart b/script/tool/lib/src/analyze_command.dart index c7a953c50ca..ef37c4aab1f 100644 --- a/script/tool/lib/src/analyze_command.dart +++ b/script/tool/lib/src/analyze_command.dart @@ -8,7 +8,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'package:yaml/yaml.dart'; -import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index 29af2e69ebd..64174ff2552 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -9,6 +9,7 @@ import 'package:platform/platform.dart'; import 'package:yaml/yaml.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/common/cmake.dart b/script/tool/lib/src/common/cmake.dart index 3f5d8452bd4..7d11039edd2 100644 --- a/script/tool/lib/src/common/cmake.dart +++ b/script/tool/lib/src/common/cmake.dart @@ -6,6 +6,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'core.dart'; +import 'output_utils.dart'; import 'process_runner.dart'; const String _cacheCommandKey = 'CMAKE_COMMAND:INTERNAL'; diff --git a/script/tool/lib/src/common/core.dart b/script/tool/lib/src/common/core.dart index 020dd74667d..49c2d84fcf0 100644 --- a/script/tool/lib/src/common/core.dart +++ b/script/tool/lib/src/common/core.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:colorize/colorize.dart'; import 'package:file/file.dart'; import 'package:pub_semver/pub_semver.dart'; @@ -67,16 +66,6 @@ bool isPackage(FileSystemEntity entity) { return entity.childFile('pubspec.yaml').existsSync(); } -/// Prints `successMessage` in green. -void printSuccess(String successMessage) { - print(Colorize(successMessage)..green()); -} - -/// Prints `errorMessage` in red. -void printError(String errorMessage) { - print(Colorize(errorMessage)..red()); -} - /// Error thrown when a command needs to exit with a non-zero exit code. /// /// While there is no specific definition of the meaning of different non-zero diff --git a/script/tool/lib/src/common/package_command.dart b/script/tool/lib/src/common/package_command.dart index b9587ac2909..cbd97cb9e74 100644 --- a/script/tool/lib/src/common/package_command.dart +++ b/script/tool/lib/src/common/package_command.dart @@ -14,6 +14,7 @@ import 'package:yaml/yaml.dart'; import 'core.dart'; import 'git_version_finder.dart'; +import 'output_utils.dart'; import 'process_runner.dart'; import 'repository_package.dart'; diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index 5462f5378c7..99f7bf049be 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -4,7 +4,6 @@ import 'dart:async'; -import 'package:colorize/colorize.dart'; import 'package:file/file.dart'; import 'package:git/git.dart'; import 'package:path/path.dart' as p; @@ -12,6 +11,7 @@ import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; import 'core.dart'; +import 'output_utils.dart'; import 'package_command.dart'; import 'process_runner.dart'; import 'repository_package.dart'; @@ -218,7 +218,7 @@ abstract class PackageLoopingCommand extends PackageCommand { /// 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 logWarning(String warningMessage) { - _printColorized(warningMessage, Styles.YELLOW); + printWarning(warningMessage); if (_currentPackageEntry != null) { _packagesWithWarnings.add(_currentPackageEntry!); } else { @@ -477,7 +477,7 @@ abstract class PackageLoopingCommand extends PackageCommand { } if (!captureOutput) { - summary = (Colorize(summary)..apply(style)).toString(); + summary = colorizeString(summary, style); } print(' ${entry.package.displayName} - $summary'); } @@ -510,7 +510,7 @@ abstract class PackageLoopingCommand extends PackageCommand { if (captureOutput) { print(message); } else { - print(Colorize(message)..apply(color)); + print(colorizeString(message, color)); } } diff --git a/script/tool/lib/src/common/xcode.dart b/script/tool/lib/src/common/xcode.dart index fc0fdbc1336..e26735df122 100644 --- a/script/tool/lib/src/common/xcode.dart +++ b/script/tool/lib/src/common/xcode.dart @@ -7,7 +7,7 @@ import 'dart:io' as io; import 'package:file/file.dart'; -import 'core.dart'; +import 'output_utils.dart'; import 'process_runner.dart'; const String _xcodeBuildCommand = 'xcodebuild'; diff --git a/script/tool/lib/src/create_all_packages_app_command.dart b/script/tool/lib/src/create_all_packages_app_command.dart index 01f37f3a310..3e470aa8abd 100644 --- a/script/tool/lib/src/create_all_packages_app_command.dart +++ b/script/tool/lib/src/create_all_packages_app_command.dart @@ -11,6 +11,7 @@ import 'package:pubspec_parse/pubspec_parse.dart'; import 'common/core.dart'; import 'common/file_utils.dart'; +import 'common/output_utils.dart'; import 'common/package_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/dart_test_command.dart b/script/tool/lib/src/dart_test_command.dart index cd564a84060..cc20244dcbe 100644 --- a/script/tool/lib/src/dart_test_command.dart +++ b/script/tool/lib/src/dart_test_command.dart @@ -6,6 +6,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/dependabot_check_command.dart b/script/tool/lib/src/dependabot_check_command.dart index d16fb33e92f..03880bf505a 100644 --- a/script/tool/lib/src/dependabot_check_command.dart +++ b/script/tool/lib/src/dependabot_check_command.dart @@ -8,7 +8,7 @@ import 'package:file/file.dart'; import 'package:git/git.dart'; import 'package:yaml/yaml.dart'; -import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 18354d83777..6bfa0ffcd10 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -10,6 +10,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/federation_safety_check_command.dart b/script/tool/lib/src/federation_safety_check_command.dart index 837193b1ccc..8a8329f8904 100644 --- a/script/tool/lib/src/federation_safety_check_command.dart +++ b/script/tool/lib/src/federation_safety_check_command.dart @@ -8,9 +8,9 @@ import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; -import 'common/core.dart'; import 'common/file_utils.dart'; import 'common/git_version_finder.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index b679c1a27c5..a0a745ab881 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -11,6 +11,7 @@ import 'package:uuid/uuid.dart'; import 'common/core.dart'; import 'common/gradle.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/fix_command.dart b/script/tool/lib/src/fix_command.dart index 2819609eabb..312a4a4ce15 100644 --- a/script/tool/lib/src/fix_command.dart +++ b/script/tool/lib/src/fix_command.dart @@ -7,7 +7,7 @@ import 'dart:async'; import 'package:file/file.dart'; import 'package:platform/platform.dart'; -import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 30a5565f232..d72da05ba18 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -11,6 +11,7 @@ import 'package:meta/meta.dart'; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_command.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/gradle_check_command.dart b/script/tool/lib/src/gradle_check_command.dart index cbce766b376..cf433dd6f54 100644 --- a/script/tool/lib/src/gradle_check_command.dart +++ b/script/tool/lib/src/gradle_check_command.dart @@ -7,6 +7,7 @@ import 'package:meta/meta.dart'; import 'package:pub_semver/pub_semver.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/license_check_command.dart b/script/tool/lib/src/license_check_command.dart index a8d4a7be5f6..6eb63052783 100644 --- a/script/tool/lib/src/license_check_command.dart +++ b/script/tool/lib/src/license_check_command.dart @@ -8,6 +8,7 @@ import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_command.dart'; const Set _codeFileExtensions = { diff --git a/script/tool/lib/src/lint_android_command.dart b/script/tool/lib/src/lint_android_command.dart index 9b875e78560..aa1372a617e 100644 --- a/script/tool/lib/src/lint_android_command.dart +++ b/script/tool/lib/src/lint_android_command.dart @@ -7,6 +7,7 @@ import 'package:platform/platform.dart'; import 'common/core.dart'; import 'common/gradle.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/make_deps_path_based_command.dart b/script/tool/lib/src/make_deps_path_based_command.dart index 8884a3deddc..12832a8c496 100644 --- a/script/tool/lib/src/make_deps_path_based_command.dart +++ b/script/tool/lib/src/make_deps_path_based_command.dart @@ -12,6 +12,7 @@ import 'package:yaml_edit/yaml_edit.dart'; import 'common/core.dart'; import 'common/git_version_finder.dart'; +import 'common/output_utils.dart'; import 'common/package_command.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/native_test_command.dart b/script/tool/lib/src/native_test_command.dart index 978c41bdf8c..80e2019d69a 100644 --- a/script/tool/lib/src/native_test_command.dart +++ b/script/tool/lib/src/native_test_command.dart @@ -9,6 +9,7 @@ import 'package:platform/platform.dart'; import 'common/cmake.dart'; import 'common/core.dart'; import 'common/gradle.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/podspec_check_command.dart b/script/tool/lib/src/podspec_check_command.dart index 9fcaa469955..60c3e1b8ea5 100644 --- a/script/tool/lib/src/podspec_check_command.dart +++ b/script/tool/lib/src/podspec_check_command.dart @@ -9,6 +9,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/publish_check_command.dart b/script/tool/lib/src/publish_check_command.dart index 0ac01535b44..dfcc11b6d85 100644 --- a/script/tool/lib/src/publish_check_command.dart +++ b/script/tool/lib/src/publish_check_command.dart @@ -11,7 +11,7 @@ import 'package:http/http.dart' as http; import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; -import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/pub_version_finder.dart'; diff --git a/script/tool/lib/src/publish_command.dart b/script/tool/lib/src/publish_command.dart index fb197ccd3ab..db8fd53e018 100644 --- a/script/tool/lib/src/publish_command.dart +++ b/script/tool/lib/src/publish_command.dart @@ -18,6 +18,7 @@ import 'package:yaml/yaml.dart'; import 'common/core.dart'; import 'common/file_utils.dart'; import 'common/git_version_finder.dart'; +import 'common/output_utils.dart'; import 'common/package_command.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/pubspec_check_command.dart b/script/tool/lib/src/pubspec_check_command.dart index 838aac0541b..6353cecde7d 100644 --- a/script/tool/lib/src/pubspec_check_command.dart +++ b/script/tool/lib/src/pubspec_check_command.dart @@ -11,6 +11,7 @@ import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:yaml/yaml.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/readme_check_command.dart b/script/tool/lib/src/readme_check_command.dart index 7e28979c956..48678212738 100644 --- a/script/tool/lib/src/readme_check_command.dart +++ b/script/tool/lib/src/readme_check_command.dart @@ -8,6 +8,7 @@ import 'package:platform/platform.dart'; import 'package:yaml/yaml.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/update_dependency_command.dart b/script/tool/lib/src/update_dependency_command.dart index d3914ca73bf..842f21e7f5b 100644 --- a/script/tool/lib/src/update_dependency_command.dart +++ b/script/tool/lib/src/update_dependency_command.dart @@ -11,6 +11,7 @@ import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:yaml_edit/yaml_edit.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/pub_version_finder.dart'; diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index ecd05428dd2..8b9644100a8 100644 --- a/script/tool/lib/src/update_excerpts_command.dart +++ b/script/tool/lib/src/update_excerpts_command.dart @@ -11,7 +11,7 @@ import 'package:platform/platform.dart'; import 'package:yaml/yaml.dart'; import 'package:yaml_edit/yaml_edit.dart'; -import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/process_runner.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/update_min_sdk_command.dart b/script/tool/lib/src/update_min_sdk_command.dart index e75b4fe0543..5bf158bc271 100644 --- a/script/tool/lib/src/update_min_sdk_command.dart +++ b/script/tool/lib/src/update_min_sdk_command.dart @@ -7,6 +7,7 @@ import 'package:pub_semver/pub_semver.dart'; import 'package:yaml_edit/yaml_edit.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/update_release_info_command.dart b/script/tool/lib/src/update_release_info_command.dart index 240ae72eed7..220a1840862 100644 --- a/script/tool/lib/src/update_release_info_command.dart +++ b/script/tool/lib/src/update_release_info_command.dart @@ -8,8 +8,8 @@ import 'package:git/git.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:yaml_edit/yaml_edit.dart'; -import 'common/core.dart'; import 'common/git_version_finder.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/package_state_utils.dart'; import 'common/repository_package.dart'; diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index a2ea016c331..ae2420aa714 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -10,8 +10,8 @@ import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; -import 'common/core.dart'; import 'common/git_version_finder.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/package_state_utils.dart'; import 'common/process_runner.dart'; diff --git a/script/tool/lib/src/xcode_analyze_command.dart b/script/tool/lib/src/xcode_analyze_command.dart index 7a9a5953f52..dbb3848aa36 100644 --- a/script/tool/lib/src/xcode_analyze_command.dart +++ b/script/tool/lib/src/xcode_analyze_command.dart @@ -6,6 +6,7 @@ import 'package:file/file.dart'; import 'package:platform/platform.dart'; import 'common/core.dart'; +import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/process_runner.dart'; From e86c8f212343c2494a78fd8c99b0ec28d72d4770 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Mon, 10 Jul 2023 16:40:04 -0400 Subject: [PATCH 2/3] Fix tests --- script/tool/test/common/package_looping_command_test.dart | 8 ++++++++ script/tool/test/publish_command_test.dart | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/script/tool/test/common/package_looping_command_test.dart b/script/tool/test/common/package_looping_command_test.dart index 043fa0a77ce..59ae44e924b 100644 --- a/script/tool/test/common/package_looping_command_test.dart +++ b/script/tool/test/common/package_looping_command_test.dart @@ -9,6 +9,7 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; +import 'package:flutter_plugin_tools/src/common/output_utils.dart'; import 'package:flutter_plugin_tools/src/common/package_looping_command.dart'; import 'package:flutter_plugin_tools/src/common/process_runner.dart'; import 'package:git/git.dart'; @@ -85,6 +86,8 @@ void main() { late Directory thirdPartyPackagesDir; setUp(() { + // Correct color handling is part of the behavior being tested here. + useColorForOutput = true; fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); @@ -93,6 +96,11 @@ void main() { .childDirectory('packages'); }); + tearDown(() { + // Restore the default behavior. + useColorForOutput = io.stdout.supportsAnsiEscapes; + }); + /// Creates a TestPackageLoopingCommand instance that uses [gitDiffResponse] /// for git diffs, and logs output to [printOutput]. TestPackageLoopingCommand createTestCommand({ diff --git a/script/tool/test/publish_command_test.dart b/script/tool/test/publish_command_test.dart index c8c14a6eaaf..3af74a1607b 100644 --- a/script/tool/test/publish_command_test.dart +++ b/script/tool/test/publish_command_test.dart @@ -525,8 +525,8 @@ void main() { 'Publishing all packages that have changed relative to "HEAD~"'), contains('Running `pub publish ` in ${plugin1.path}...'), contains('Running `pub publish ` in ${plugin2.path}...'), - contains('plugin1 - \x1B[32mpublished\x1B[0m'), - contains('plugin2/plugin2 - \x1B[32mpublished\x1B[0m'), + contains('plugin1 - published'), + contains('plugin2/plugin2 - published'), ])); expect( processRunner.recordedCalls, From 20b959d109a6f5471b76f22f9e8eca461e14e89a Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 11 Jul 2023 12:11:17 -0400 Subject: [PATCH 3/3] unit tests --- script/tool/lib/src/common/output_utils.dart | 44 ++++++++ .../tool/test/common/output_utils_test.dart | 100 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 script/tool/lib/src/common/output_utils.dart create mode 100644 script/tool/test/common/output_utils_test.dart diff --git a/script/tool/lib/src/common/output_utils.dart b/script/tool/lib/src/common/output_utils.dart new file mode 100644 index 00000000000..7fd39f6854e --- /dev/null +++ b/script/tool/lib/src/common/output_utils.dart @@ -0,0 +1,44 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:io'; + +import 'package:colorize/colorize.dart'; +import 'package:meta/meta.dart'; + +export 'package:colorize/colorize.dart' show Styles; + +/// True if color should be applied. +/// +/// Defaults to autodetecting stdout. +@visibleForTesting +bool useColorForOutput = stdout.supportsAnsiEscapes; + +String _colorizeIfAppropriate(String string, Styles color) { + if (!useColorForOutput) { + return string; + } + return Colorize(string).apply(color).toString(); +} + +/// Prints [message] in green, if the environment supports color. +void printSuccess(String message) { + print(_colorizeIfAppropriate(message, Styles.GREEN)); +} + +/// Prints [message] in yellow, if the environment supports color. +void printWarning(String message) { + print(_colorizeIfAppropriate(message, Styles.YELLOW)); +} + +/// Prints [message] in red, if the environment supports color. +void printError(String message) { + print(_colorizeIfAppropriate(message, Styles.RED)); +} + +/// Returns [message] with escapes to print it in [color], if the environment +/// supports color. +String colorizeString(String message, Styles color) { + return _colorizeIfAppropriate(message, color); +} diff --git a/script/tool/test/common/output_utils_test.dart b/script/tool/test/common/output_utils_test.dart new file mode 100644 index 00000000000..757202380c5 --- /dev/null +++ b/script/tool/test/common/output_utils_test.dart @@ -0,0 +1,100 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter_plugin_tools/src/common/output_utils.dart'; +import 'package:test/test.dart'; + +void main() { + group('with color support', () { + setUp(() { + useColorForOutput = true; + }); + + tearDown(() { + useColorForOutput = stdout.supportsAnsiEscapes; + }); + + test('colorize works', () async { + const String message = 'a message'; + + expect( + colorizeString(message, Styles.MAGENTA), '\x1B[35m$message\x1B[0m'); + }); + + test('printSuccess is green', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printSuccess(message)), + '\x1B[32m$message\x1B[0m'); + }); + + test('printWarning is yellow', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printWarning(message)), + '\x1B[33m$message\x1B[0m'); + }); + + test('printError is red', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printError(message)), + '\x1B[31m$message\x1B[0m'); + }); + }); + + group('without color support', () { + setUp(() { + useColorForOutput = false; + }); + + tearDown(() { + useColorForOutput = stdout.supportsAnsiEscapes; + }); + + test('colorize no-ops', () async { + const String message = 'a message'; + + expect(colorizeString(message, Styles.MAGENTA), message); + }); + + test('printSuccess just prints', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printSuccess(message)), message); + }); + + test('printWarning just prints', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printWarning(message)), message); + }); + + test('printError just prints', () async { + const String message = 'a message'; + + expect(await _capturePrint(() => printError(message)), message); + }); + }); +} + +/// Run the command [runner] with the given [args] and return +/// what was printed. +/// A custom [errorHandler] can be used to handle the runner error as desired without throwing. +Future _capturePrint(void Function() printFunction) async { + final StringBuffer output = StringBuffer(); + final ZoneSpecification spec = ZoneSpecification( + print: (_, __, ___, String message) { + output.write(message); + }, + ); + await Zone.current + .fork(specification: spec) + .run>(() async => printFunction()); + + return output.toString(); +}