|
7 | 7 | import 'dart:async'; |
8 | 8 | import 'dart:convert'; |
9 | 9 | import 'dart:io'; |
| 10 | +import 'dart:isolate'; |
10 | 11 |
|
11 | 12 | import 'package:args/args.dart'; |
12 | 13 | import 'package:build_integration/file_system/multi_root.dart'; |
@@ -54,7 +55,7 @@ import 'target.dart'; |
54 | 55 | /// |
55 | 56 | /// - debugger creates an isolate using dartdevc's main method with |
56 | 57 | /// '--experimental-expression-compiler' flag and passes a send port |
57 | | -/// to dartdevc for sending responces from the service to the debugger. |
| 58 | +/// to dartdevc for sending responses from the service to the debugger. |
58 | 59 | /// |
59 | 60 | /// - dartdevc creates a new send port to receive requests on, and sends |
60 | 61 | /// it back to the debugger, for sending requests to the service. |
@@ -96,6 +97,48 @@ class ExpressionCompilerWorker { |
96 | 97 | this.sendResponse, |
97 | 98 | ); |
98 | 99 |
|
| 100 | + /// Create expression compiler worker from [args] and start it. |
| 101 | + /// |
| 102 | + /// If [sendPort] is provided, creates a `receivePort` and sends it to |
| 103 | + /// the consumer to establish communication. Otherwise, uses stdin/stdout |
| 104 | + /// for communication with the consumer. |
| 105 | + /// |
| 106 | + /// Details: |
| 107 | + /// |
| 108 | + /// Consumer uses (`consumerSendPort`, `consumerReceivePort`) pair to |
| 109 | + /// send requests and receive responses: |
| 110 | + /// |
| 111 | + /// `consumerReceivePort.sendport` = [sendPort] |
| 112 | + /// `consumerSendPort = receivePort.sendport` |
| 113 | + /// |
| 114 | + /// Worker uses the opposite ports connected to the consumer ports - |
| 115 | + /// (`receivePort`, [sendPort]) to receive requests and send responses. |
| 116 | + /// |
| 117 | + /// The worker stops on start failure or after the consumer closes its |
| 118 | + /// receive port corresponding to [sendPort]. |
| 119 | + static Future<void> createAndStart(List<String> args, |
| 120 | + {SendPort sendPort}) async { |
| 121 | + if (sendPort != null) { |
| 122 | + var receivePort = ReceivePort(); |
| 123 | + sendPort.send(receivePort.sendPort); |
| 124 | + try { |
| 125 | + var worker = await createFromArgs(args, |
| 126 | + requestStream: receivePort.cast<Map<String, dynamic>>(), |
| 127 | + sendResponse: sendPort.send); |
| 128 | + await worker.start(); |
| 129 | + } catch (e, s) { |
| 130 | + sendPort |
| 131 | + .send({'exception': '$e', 'stackTrace': '$s', 'succeeded': false}); |
| 132 | + rethrow; |
| 133 | + } finally { |
| 134 | + receivePort.close(); |
| 135 | + } |
| 136 | + } else { |
| 137 | + var worker = await createFromArgs(args); |
| 138 | + await worker.start(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
99 | 142 | static Future<ExpressionCompilerWorker> createFromArgs( |
100 | 143 | List<String> args, { |
101 | 144 | Stream<Map<String, dynamic>> requestStream, |
@@ -193,6 +236,9 @@ class ExpressionCompilerWorker { |
193 | 236 | return processedOptions.loadSdkSummary(null); |
194 | 237 | }); |
195 | 238 |
|
| 239 | + if (sdkComponent == null) { |
| 240 | + throw Exception('Could not load SDK component: $sdkSummary'); |
| 241 | + } |
196 | 242 | return ExpressionCompilerWorker._(processedOptions, compilerOptions, |
197 | 243 | moduleFormat, sdkComponent, requestStream, sendResponse) |
198 | 244 | .._updateCache(sdkComponent, dartSdkModule, true); |
|
0 commit comments