-
Notifications
You must be signed in to change notification settings - Fork 83
[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
Changes from 9 commits
00580bb
8de9846
2ca0da4
ff15136
dfa1c5f
5c24de5
4e7d541
30d5aba
304ebc1
86c5602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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 { | ||
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'], | ||
); | ||
final compileExitCode = await _handleProcess(compileStep); | ||
// Terminate early if compilation failed: | ||
if (compileExitCode != 0) { | ||
return compileExitCode; | ||
} | ||
logStep('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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return updateExitCode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can we log that the process exited abnormally? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updateExitCode is being returned, and logging happens in main (line 29-30) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant that this logic (lines 53-57) can be simplified to just "return updateExitCode`, but that is optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, done |
||
return updateExitCode; | ||
} | ||
// Return 0 to indicate success: | ||
return 0; | ||
} | ||
|
||
Future<int> _handleProcess(Process process) async { | ||
_handleOutput(process.stdout); | ||
_handleOutput(process.stderr); | ||
return process.exitCode; | ||
} | ||
|
||
void _handleOutput(Stream<List<int>> output) { | ||
output | ||
.transform(utf8.decoder) | ||
.transform(const LineSplitter()) | ||
.listen(_handleOutputLine); | ||
} | ||
|
||
void _handleOutputLine(String line, {bool isStdout = true}) { | ||
annagrin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
if (line.isNotEmpty) { | ||
print('$outputName: $line'); | ||
} | ||
} | ||
|
||
void logStep(String message) { | ||
print('[BUILD STEP] $message'); | ||
} | ||
|
||
void logWarning(String message) { | ||
print('[WARNING] $message'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional - should we just use a logger(from package:logging/logging.dart) instead of printing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe change [WARNING] to [ERROR], to indicate irrecoverable failure? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now writing to sdout/stderr following example in https://dart.dev/tutorials/server/cmdline ( |
||
} |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.