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 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
91 changes: 91 additions & 0 deletions dwds/debug_extension_mv3/tool/build_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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:async';
import 'dart:convert';
import 'dart:io';

import 'package:args/args.dart';
import 'package:path/path.dart' as p;

const _prodFlag = 'prod';

void main(List<String> arguments) async {
final parser = ArgParser()
..addFlag(_prodFlag, negatable: true, defaultsTo: false);
final argResults = parser.parse(arguments);

exitCode = await run(isProd: argResults[_prodFlag] as bool);
if (exitCode != 0) {
_logWarning('Run terminated unexpectedly with exit code: $exitCode');
}
}

Future<int> run({required bool isProd}) async {
_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'],
);
final compileExitCode = await _handleProcess(compileStep);
// Terminate early if compilation failed:
if (compileExitCode != 0) {
return compileExitCode;
}
_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);
// Return exit code (0 indicates success):
return updateExitCode;
}

Future<int> _handleProcess(Process process) async {
_handleOutput(process.stdout, isStdout: true);
_handleOutput(process.stderr, isStdout: false);
return process.exitCode;
}

void _handleOutput(Stream<List<int>> output, {bool isStdout = true}) {
output
.transform(utf8.decoder)
.transform(const LineSplitter())
.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);
throw Exception(error);
}
// Log message to the terminal:
final message = '$outputName: $line';
isStdout ? _logInfo(message) : _logWarning(message);
}

void _logInfo(String message) {
stdout.writeln(message);
}

void _logWarning(String warning) {
stderr.writeln(warning);
}
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