[pigeon] switched to using isolates instead of subprocesses to run pigeon_lib (#210)

This commit is contained in:
gaaclarke
2020-09-25 16:02:15 -07:00
committed by GitHub
parent 8b9e8eee88
commit 306bc50006
4 changed files with 35 additions and 15 deletions

View File

@ -1,3 +1,9 @@
## 0.1.8
* Started spawning pigeon_lib in an isolate instead of a subprocess. The
subprocess could have lead to errors if the dart version on $PATH didn't match
the one that comes with flutter.
## 0.1.7 ## 0.1.7
* Fixed Dart compilation for later versions that support null safety, opting out * Fixed Dart compilation for later versions that support null safety, opting out

View File

@ -2,31 +2,45 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:convert'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:isolate';
import 'package:path/path.dart' as path;
import 'package:pigeon/pigeon_lib.dart'; import 'package:pigeon/pigeon_lib.dart';
Future<void> main(List<String> args) async { Future<void> main(List<String> args) async {
final PigeonOptions opts = Pigeon.parseArgs(args); final PigeonOptions opts = Pigeon.parseArgs(args);
assert(opts.input != null); assert(opts.input != null);
final String rawInputPath = opts.input;
final Directory tempDir = Directory.systemTemp.createTempSync();
final String absInputPath = File(rawInputPath).absolute.path;
final String relInputPath = path.relative(absInputPath, from: tempDir.path);
final String importLine = final String importLine =
(opts.input != null) ? 'import \'${opts.input}\';\n' : ''; (opts.input != null) ? 'import \'$relInputPath\';\n' : '';
final String code = """$importLine final String code = """$importLine
import 'dart:io'; import 'dart:io';
import 'dart:isolate';
import 'package:pigeon/pigeon_lib.dart'; import 'package:pigeon/pigeon_lib.dart';
void main(List<String> args) async { void main(List<String> args, SendPort sendPort) async {
exit(await Pigeon.run(args)); sendPort.send(await Pigeon.run(args));
} }
"""; """;
// TODO(aaclarke): Start using a system temp file. final String tempFilename = path.join(tempDir.path, '_pigeon_temp_.dart');
const String tempFilename = '_pigeon_temp_.dart'; await File(tempFilename).writeAsString(code);
final File tempFile = await File(tempFilename).writeAsString(code); final ReceivePort receivePort = ReceivePort();
final Process process = Isolate.spawnUri(Uri.parse(tempFilename), args, receivePort.sendPort);
await Process.start('dart', <String>[tempFilename] + args); final Completer<int> completer = Completer<int>();
process.stdout.transform(utf8.decoder).listen((String data) => print(data)); receivePort.listen((dynamic message) {
process.stderr.transform(utf8.decoder).listen((String data) => print(data)); try {
final int exitCode = await process.exitCode; // ignore: avoid_as
tempFile.deleteSync(); completer.complete(message as int);
} catch (exception) {
completer.completeError(exception);
}
});
final int exitCode = await completer.future;
tempDir.deleteSync(recursive: true);
exit(exitCode); exit(exitCode);
} }

View File

@ -8,7 +8,7 @@ import 'dart:mirrors';
import 'ast.dart'; import 'ast.dart';
/// The current version of pigeon. /// The current version of pigeon.
const String pigeonVersion = '0.1.7'; const String pigeonVersion = '0.1.8';
/// Read all the content from [stdin] to a String. /// Read all the content from [stdin] to a String.
String readStdin() { String readStdin() {

View File

@ -1,5 +1,5 @@
name: pigeon name: pigeon
version: 0.1.7 version: 0.1.8
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
homepage: https://github.com/flutter/packages/tree/master/packages/pigeon homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
dependencies: dependencies: