From 00580bb0ff34bd5545c9f42b98de3ad2f83d9fb5 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 10:59:19 -0800 Subject: [PATCH 01/10] Compile extension with Dart instead of shell script --- .../tool/build_extension.dart | 56 +++++++++++++++++++ .../tool/build_extension.sh | 33 ----------- dwds/test/puppeteer/test_utils.dart | 5 +- 3 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 dwds/debug_extension_mv3/tool/build_extension.dart delete mode 100755 dwds/debug_extension_mv3/tool/build_extension.sh diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart new file mode 100644 index 000000000..4d6f87821 --- /dev/null +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -0,0 +1,56 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// INSTRUCTIONS: + +// Builds the unminifed dart2js extension (see DDC issue: +// see DDC issue: https://github.com/dart-lang/sdk/issues/49869). + +// Run from the extension root directory: +// - For dev: dart run tool/build_extension.dart +// - For prod: dart run tool/build_extension.dart prod + +import 'dart:io'; + +import 'package:args/args.dart'; +import 'package:path/path.dart' as p; + +const prodFlag = 'prod'; + +void main(List arguments) { + exitCode = 0; // presume success + final parser = ArgParser() + ..addFlag(prodFlag, negatable: true, defaultsTo: false); + final argResults = parser.parse(arguments); + + run(isProd: argResults[prodFlag] as bool); +} + +Future run({required bool isProd}) async { + logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); + logInfo('Compiling extension with dart2js to /compiled directory'); + logOutput( + await Process.run( + 'dart', + ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], + ), + ); + logInfo('Updating manifest.json in /compiled directory.'); + logOutput( + await Process.run( + 'dart', + [p.join('tool', 'update_dev_files.dart')], + ), + ); +} + +void logInfo(String message) { + print('[BUILD STEP] $message'); +} + +void logOutput(ProcessResult result) { + final output = result.stdout; + final outputList = output is List ? output : [output ?? '']; + print(outputList.map((output) => '$output').join('\n')); +} diff --git a/dwds/debug_extension_mv3/tool/build_extension.sh b/dwds/debug_extension_mv3/tool/build_extension.sh deleted file mode 100755 index 9df8f4fa9..000000000 --- a/dwds/debug_extension_mv3/tool/build_extension.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -# Copyright 2022 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -# INSTRUCTIONS: - -# Builds the unminifed dart2js app (see DDC issue: https://github.com/dart-lang/sdk/issues/49869): -# ./tool/build_extension.sh - - -prod="false" - -case "$1" in - prod) - prod="true" - shift;; -esac - -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -echo "Building dart2js-compiled extension to /compiled directory." -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -dart run build_runner build web --output build --release - -if [ $prod == true ]; then - exit 1 -fi - -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -echo "Updating manifest.json in /compiled directory." -echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -dart tool/update_dev_files.dart diff --git a/dwds/test/puppeteer/test_utils.dart b/dwds/test/puppeteer/test_utils.dart index 5ef0719c9..0c8c93e98 100644 --- a/dwds/test/puppeteer/test_utils.dart +++ b/dwds/test/puppeteer/test_utils.dart @@ -13,10 +13,9 @@ import '../fixtures/utilities.dart'; Future buildDebugExtension() async { final extensionDir = absolutePath(pathFromDwds: 'debug_extension_mv3'); - // TODO(elliette): This doesn't work on Windows, see https://github.com/dart-lang/webdev/issues/1724. await Process.run( - p.join('tool', 'build_extension.sh'), - [], + 'dart', + [p.join('tool', 'build_extension.dart')], workingDirectory: extensionDir, ); return p.join(extensionDir, 'compiled'); From 8de984680843c28388dc4b71b9026249022883c7 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 11:01:31 -0800 Subject: [PATCH 02/10] Update year --- dwds/debug_extension_mv3/tool/build_extension.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 4d6f87821..95b6c6695 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. From 2ca0da42828e05ad33ef5c96d7806bf95ba9d8ca Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 13:18:16 -0800 Subject: [PATCH 03/10] Respond to PR comments --- .../tool/build_extension.dart | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 95b6c6695..1564e09b6 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -18,39 +18,47 @@ import 'package:path/path.dart' as p; const prodFlag = 'prod'; -void main(List arguments) { - exitCode = 0; // presume success +void main(List arguments) async { final parser = ArgParser() ..addFlag(prodFlag, negatable: true, defaultsTo: false); final argResults = parser.parse(arguments); - run(isProd: argResults[prodFlag] as bool); + exitCode = await run(isProd: argResults[prodFlag] as bool); } -Future run({required bool isProd}) async { +Future run({required bool isProd}) async { logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); logInfo('Compiling extension with dart2js to /compiled directory'); - logOutput( - await Process.run( - 'dart', - ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], - ), + final compileStep = await Process.run( + 'dart', + ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], ); + final compileStepSucceeded = _handleProcessResult(compileStep); + // Terminate early if compilation failed: + if (!compileStepSucceeded) return compileStep.exitCode; logInfo('Updating manifest.json in /compiled directory.'); - logOutput( - await Process.run( - 'dart', - [p.join('tool', 'update_dev_files.dart')], - ), + final updateStep = await Process.run( + 'dart', + [p.join('tool', 'update_dev_files.dart')], ); + final updateStepSucceeded = _handleProcessResult(updateStep); + // Terminate early if updating dev files failed: + if (!updateStepSucceeded) return updateStep.exitCode; + // Return 0 to indicate success: + return 0; +} + +bool _handleProcessResult(ProcessResult result) { + final success = result.exitCode == 0; + logOutput(success ? result.stdout : result.stderr); + return success; } void logInfo(String message) { print('[BUILD STEP] $message'); } -void logOutput(ProcessResult result) { - final output = result.stdout; +void logOutput(dynamic output) { final outputList = output is List ? output : [output ?? '']; print(outputList.map((output) => '$output').join('\n')); } From ff15136b53a31d2f140a2f30b73aeb7bf4576128 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 13:25:11 -0800 Subject: [PATCH 04/10] Log failure --- dwds/debug_extension_mv3/tool/build_extension.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 1564e09b6..3286142b0 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -24,6 +24,9 @@ void main(List arguments) async { final argResults = parser.parse(arguments); exitCode = await run(isProd: argResults[prodFlag] as bool); + if (exitCode != 0) { + print('BUILDING THE EXTENSION FAILED.'); + } } Future run({required bool isProd}) async { From dfa1c5f132cd39081019e6b328c9039f3d1a16b9 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 13:25:56 -0800 Subject: [PATCH 05/10] Format --- dwds/debug_extension_mv3/tool/build_extension.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 3286142b0..1167af72b 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -26,7 +26,7 @@ void main(List arguments) async { exitCode = await run(isProd: argResults[prodFlag] as bool); if (exitCode != 0) { print('BUILDING THE EXTENSION FAILED.'); - } + } } Future run({required bool isProd}) async { From 5c24de5e7cde1a877b958bb68232e2451ccddf8f Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:21:10 -0800 Subject: [PATCH 06/10] Using Process.start instead of Process.run --- .../tool/build_extension.dart | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 1167af72b..afbd6da38 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -11,6 +11,8 @@ // - For dev: dart run tool/build_extension.dart // - For prod: dart run tool/build_extension.dart prod +import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:args/args.dart'; @@ -25,36 +27,57 @@ void main(List arguments) async { exitCode = await run(isProd: argResults[prodFlag] as bool); if (exitCode != 0) { - print('BUILDING THE EXTENSION FAILED.'); + print('BUILDING THE EXTENSION FAILED WITH EXIT CODE $exitCode.'); } } Future run({required bool isProd}) async { logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); logInfo('Compiling extension with dart2js to /compiled directory'); - final compileStep = await Process.run( + final compileStep = await Process.start( 'dart', ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], ); - final compileStepSucceeded = _handleProcessResult(compileStep); + final compileExitCode = await _handleProcess(compileStep); // Terminate early if compilation failed: - if (!compileStepSucceeded) return compileStep.exitCode; + if (compileExitCode != 0) { + return compileExitCode; + } logInfo('Updating manifest.json in /compiled directory.'); - final updateStep = await Process.run( + final updateStep = await Process.start( 'dart', [p.join('tool', 'update_dev_files.dart')], ); - final updateStepSucceeded = _handleProcessResult(updateStep); + final updateExitCode = await _handleProcess(updateStep); // Terminate early if updating dev files failed: - if (!updateStepSucceeded) return updateStep.exitCode; + if (updateExitCode != 0) { + return updateExitCode; + } // Return 0 to indicate success: return 0; } -bool _handleProcessResult(ProcessResult result) { - final success = result.exitCode == 0; - logOutput(success ? result.stdout : result.stderr); - return success; +Future _handleProcess(Process process) async { + _handleOutput(process.stdout); + _handleOutput(process.stderr); + return process.exitCode; +} + +void _handleOutput(Stream> output) { + output + .transform(utf8.decoder) + .transform(const LineSplitter()) + .listen(_handleOutputLine); +} + +void _handleOutputLine(String line, {bool isStdout = true}) { + if (line.toUpperCase().contains('SEVERE') || + line.toUpperCase().contains('ERROR')) { + throw Exception(line); + } + if (line.isNotEmpty) { + print('${isStdout ? 'stdout' : 'stderr'}: $line'); + } } void logInfo(String message) { From 4e7d541383f4abc80d8541313e0b7f901fc38cca Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:18:09 -0800 Subject: [PATCH 07/10] small tweaks --- .../tool/build_extension.dart | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index afbd6da38..b67961ee3 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -27,13 +27,13 @@ void main(List arguments) async { exitCode = await run(isProd: argResults[prodFlag] as bool); if (exitCode != 0) { - print('BUILDING THE EXTENSION FAILED WITH EXIT CODE $exitCode.'); + logWarning('Run terminated unexpectedly with exit code: $exitCode'); } } Future run({required bool isProd}) async { - logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); - logInfo('Compiling extension with dart2js to /compiled directory'); + logStep('Building extension for ${isProd ? 'prod' : 'dev'}'); + logStep('Compiling extension with dart2js to /compiled directory'); final compileStep = await Process.start( 'dart', ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], @@ -43,7 +43,7 @@ Future run({required bool isProd}) async { if (compileExitCode != 0) { return compileExitCode; } - logInfo('Updating manifest.json in /compiled directory.'); + logStep('Updating manifest.json in /compiled directory.'); final updateStep = await Process.start( 'dart', [p.join('tool', 'update_dev_files.dart')], @@ -71,19 +71,26 @@ void _handleOutput(Stream> output) { } void _handleOutputLine(String line, {bool isStdout = true}) { + final outputName = isStdout ? 'stdout' : 'stderr'; if (line.toUpperCase().contains('SEVERE') || line.toUpperCase().contains('ERROR')) { - throw Exception(line); + final error = 'Unexpected error in $outputName: $line'; + logWarning(error); + throw Exception(error); } if (line.isNotEmpty) { print('${isStdout ? 'stdout' : 'stderr'}: $line'); } } -void logInfo(String message) { +void logStep(String message) { print('[BUILD STEP] $message'); } +void logWarning(String message) { + print('[WARNING] $message'); +} + void logOutput(dynamic output) { final outputList = output is List ? output : [output ?? '']; print(outputList.map((output) => '$output').join('\n')); From 30d5aba9a8c3862aaf3a445f76177b539b2388d1 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:20:40 -0800 Subject: [PATCH 08/10] Use outputName --- dwds/debug_extension_mv3/tool/build_extension.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index b67961ee3..36223bedc 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -79,7 +79,7 @@ void _handleOutputLine(String line, {bool isStdout = true}) { throw Exception(error); } if (line.isNotEmpty) { - print('${isStdout ? 'stdout' : 'stderr'}: $line'); + print('$outputName: $line'); } } From 304ebc1e36617348c8867745e932c122ae2e7746 Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:21:29 -0800 Subject: [PATCH 09/10] Remove logOutput --- dwds/debug_extension_mv3/tool/build_extension.dart | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index 36223bedc..a07bea7c0 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -90,8 +90,3 @@ void logStep(String message) { void logWarning(String message) { print('[WARNING] $message'); } - -void logOutput(dynamic output) { - final outputList = output is List ? output : [output ?? '']; - print(outputList.map((output) => '$output').join('\n')); -} From 86c5602d5433d6790cbcbd30abf1e9becc4cc83b Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Tue, 14 Feb 2023 09:49:54 -0800 Subject: [PATCH 10/10] Respond to PR comments --- .../tool/build_extension.dart | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/dwds/debug_extension_mv3/tool/build_extension.dart b/dwds/debug_extension_mv3/tool/build_extension.dart index a07bea7c0..4ed884ed9 100644 --- a/dwds/debug_extension_mv3/tool/build_extension.dart +++ b/dwds/debug_extension_mv3/tool/build_extension.dart @@ -18,22 +18,22 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:path/path.dart' as p; -const prodFlag = 'prod'; +const _prodFlag = 'prod'; void main(List arguments) async { final parser = ArgParser() - ..addFlag(prodFlag, negatable: true, defaultsTo: false); + ..addFlag(_prodFlag, negatable: true, defaultsTo: false); final argResults = parser.parse(arguments); - exitCode = await run(isProd: argResults[prodFlag] as bool); + exitCode = await run(isProd: argResults[_prodFlag] as bool); if (exitCode != 0) { - logWarning('Run terminated unexpectedly with exit code: $exitCode'); + _logWarning('Run terminated unexpectedly with exit code: $exitCode'); } } Future run({required bool isProd}) async { - logStep('Building extension for ${isProd ? 'prod' : 'dev'}'); - logStep('Compiling extension with dart2js to /compiled directory'); + _logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); + _logInfo('Compiling extension with dart2js to /compiled directory'); final compileStep = await Process.start( 'dart', ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], @@ -43,50 +43,49 @@ Future run({required bool isProd}) async { if (compileExitCode != 0) { return compileExitCode; } - logStep('Updating manifest.json in /compiled directory.'); + _logInfo('Updating manifest.json in /compiled directory.'); final updateStep = await Process.start( 'dart', [p.join('tool', 'update_dev_files.dart')], ); final updateExitCode = await _handleProcess(updateStep); - // Terminate early if updating dev files failed: - if (updateExitCode != 0) { - return updateExitCode; - } - // Return 0 to indicate success: - return 0; + // Return exit code (0 indicates success): + return updateExitCode; } Future _handleProcess(Process process) async { - _handleOutput(process.stdout); - _handleOutput(process.stderr); + _handleOutput(process.stdout, isStdout: true); + _handleOutput(process.stderr, isStdout: false); return process.exitCode; } -void _handleOutput(Stream> output) { +void _handleOutput(Stream> output, {bool isStdout = true}) { output .transform(utf8.decoder) .transform(const LineSplitter()) - .listen(_handleOutputLine); + .listen((line) => _handleOutputLine(line, isStdout: isStdout)); } void _handleOutputLine(String line, {bool isStdout = true}) { + // Skip empty lines: + if (line.isEmpty) return; + // Log any unexpected errors and throw: final outputName = isStdout ? 'stdout' : 'stderr'; if (line.toUpperCase().contains('SEVERE') || line.toUpperCase().contains('ERROR')) { final error = 'Unexpected error in $outputName: $line'; - logWarning(error); + _logWarning(error); throw Exception(error); } - if (line.isNotEmpty) { - print('$outputName: $line'); - } + // Log message to the terminal: + final message = '$outputName: $line'; + isStdout ? _logInfo(message) : _logWarning(message); } -void logStep(String message) { - print('[BUILD STEP] $message'); +void _logInfo(String message) { + stdout.writeln(message); } -void logWarning(String message) { - print('[WARNING] $message'); +void _logWarning(String warning) { + stderr.writeln(warning); }