Skip to content

[MV3 Debug Extension] Compile extension with Dart instead of shell script #1954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 56 additions & 0 deletions dwds/debug_extension_mv3/tool/build_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.

// 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<String> 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<void> 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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add printing on errors, for example if the exist code is not 0? Or maybe return the exit code from run so the caller can check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added, now checking if the exit code is not 0, and is so printing the stderr.

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably need to log the stderr as well.

Suggestion - use transformers to listen to both and use a logger to report severe errors on failures (for example, any message from stderr, non-0 exit codes, or [SEVERE] output on stdout)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Logging stderr if the exit code is not 0, and also terminating the entire process with that exit code.

final outputList = output is List ? output : [output ?? ''];
print(outputList.map((output) => '$output').join('\n'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we switch to using transformers on the stream to listen to lines instead and logging them one by one? That way we can see the output in progress, even if the process never ends, for example. Also it will allow us to fail early if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the documentation:

"Use the static start and run methods to start a new process. The run method executes the process non-interactively to completion. In contrast, the start method allows your code to interact with the running process."

Process.start returns a Process with stdout and stderr streams that can be listened to. I think it makes more sense to use Process.run here.

}
33 changes: 0 additions & 33 deletions dwds/debug_extension_mv3/tool/build_extension.sh

This file was deleted.

5 changes: 2 additions & 3 deletions dwds/test/puppeteer/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import '../fixtures/utilities.dart';

Future<String> 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');
Expand Down