|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// INSTRUCTIONS: |
| 6 | + |
| 7 | +// Builds the unminifed dart2js extension (see DDC issue: |
| 8 | +// see DDC issue: https://github.com/dart-lang/sdk/issues/49869). |
| 9 | + |
| 10 | +// Run from the extension root directory: |
| 11 | +// - For dev: dart run tool/build_extension.dart |
| 12 | +// - For prod: dart run tool/build_extension.dart prod |
| 13 | + |
| 14 | +import 'dart:async'; |
| 15 | +import 'dart:convert'; |
| 16 | +import 'dart:io'; |
| 17 | + |
| 18 | +import 'package:args/args.dart'; |
| 19 | +import 'package:path/path.dart' as p; |
| 20 | + |
| 21 | +const _prodFlag = 'prod'; |
| 22 | + |
| 23 | +void main(List<String> arguments) async { |
| 24 | + final parser = ArgParser() |
| 25 | + ..addFlag(_prodFlag, negatable: true, defaultsTo: false); |
| 26 | + final argResults = parser.parse(arguments); |
| 27 | + |
| 28 | + exitCode = await run(isProd: argResults[_prodFlag] as bool); |
| 29 | + if (exitCode != 0) { |
| 30 | + _logWarning('Run terminated unexpectedly with exit code: $exitCode'); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +Future<int> run({required bool isProd}) async { |
| 35 | + _logInfo('Building extension for ${isProd ? 'prod' : 'dev'}'); |
| 36 | + _logInfo('Compiling extension with dart2js to /compiled directory'); |
| 37 | + final compileStep = await Process.start( |
| 38 | + 'dart', |
| 39 | + ['run', 'build_runner', 'build', 'web', '--output', 'build', '--release'], |
| 40 | + ); |
| 41 | + final compileExitCode = await _handleProcess(compileStep); |
| 42 | + // Terminate early if compilation failed: |
| 43 | + if (compileExitCode != 0) { |
| 44 | + return compileExitCode; |
| 45 | + } |
| 46 | + _logInfo('Updating manifest.json in /compiled directory.'); |
| 47 | + final updateStep = await Process.start( |
| 48 | + 'dart', |
| 49 | + [p.join('tool', 'update_dev_files.dart')], |
| 50 | + ); |
| 51 | + final updateExitCode = await _handleProcess(updateStep); |
| 52 | + // Return exit code (0 indicates success): |
| 53 | + return updateExitCode; |
| 54 | +} |
| 55 | + |
| 56 | +Future<int> _handleProcess(Process process) async { |
| 57 | + _handleOutput(process.stdout, isStdout: true); |
| 58 | + _handleOutput(process.stderr, isStdout: false); |
| 59 | + return process.exitCode; |
| 60 | +} |
| 61 | + |
| 62 | +void _handleOutput(Stream<List<int>> output, {bool isStdout = true}) { |
| 63 | + output |
| 64 | + .transform(utf8.decoder) |
| 65 | + .transform(const LineSplitter()) |
| 66 | + .listen((line) => _handleOutputLine(line, isStdout: isStdout)); |
| 67 | +} |
| 68 | + |
| 69 | +void _handleOutputLine(String line, {bool isStdout = true}) { |
| 70 | + // Skip empty lines: |
| 71 | + if (line.isEmpty) return; |
| 72 | + // Log any unexpected errors and throw: |
| 73 | + final outputName = isStdout ? 'stdout' : 'stderr'; |
| 74 | + if (line.toUpperCase().contains('SEVERE') || |
| 75 | + line.toUpperCase().contains('ERROR')) { |
| 76 | + final error = 'Unexpected error in $outputName: $line'; |
| 77 | + _logWarning(error); |
| 78 | + throw Exception(error); |
| 79 | + } |
| 80 | + // Log message to the terminal: |
| 81 | + final message = '$outputName: $line'; |
| 82 | + isStdout ? _logInfo(message) : _logWarning(message); |
| 83 | +} |
| 84 | + |
| 85 | +void _logInfo(String message) { |
| 86 | + stdout.writeln(message); |
| 87 | +} |
| 88 | + |
| 89 | +void _logWarning(String warning) { |
| 90 | + stderr.writeln(warning); |
| 91 | +} |
0 commit comments