|
| 1 | +// Copyright 2020 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | +import 'dart:isolate'; |
| 8 | + |
| 9 | +import 'package:path/path.dart' as path; |
| 10 | +import 'package:pigeon/pigeon_lib.dart'; |
| 11 | + |
| 12 | +/// This creates a relative path from `from` to `input`, the output being a |
| 13 | +/// posix path on all platforms. |
| 14 | +String _posixRelative(String input, {String from}) { |
| 15 | + final path.Context context = path.Context(style: path.Style.posix); |
| 16 | + final String rawInputPath = input; |
| 17 | + final String absInputPath = File(rawInputPath).absolute.path; |
| 18 | + // By going through URI's we can make sure paths can go between drives in |
| 19 | + // Windows. |
| 20 | + final Uri inputUri = path.toUri(absInputPath); |
| 21 | + final String posixAbsInputPath = context.fromUri(inputUri); |
| 22 | + final Uri tempUri = path.toUri(from); |
| 23 | + final String posixTempPath = context.fromUri(tempUri); |
| 24 | + return context.relative(posixAbsInputPath, from: posixTempPath); |
| 25 | +} |
| 26 | + |
| 27 | +/// This is the main entrypoint for the command-line tool. [args] are the |
| 28 | +/// commmand line arguments and there is an optional [packageConfig] to |
| 29 | +/// accomodate users that want to integrate pigeon with other build systems. |
| 30 | +Future<void> runCommandLine(List<String> args, {Uri packageConfig}) async { |
| 31 | + final PigeonOptions opts = Pigeon.parseArgs(args); |
| 32 | + final Directory tempDir = Directory.systemTemp.createTempSync( |
| 33 | + 'flutter_pigeon.', |
| 34 | + ); |
| 35 | + |
| 36 | + String importLine = ''; |
| 37 | + if (opts.input != null) { |
| 38 | + final String relInputPath = _posixRelative(opts.input, from: tempDir.path); |
| 39 | + importLine = 'import \'$relInputPath\';\n'; |
| 40 | + } |
| 41 | + final String code = """ |
| 42 | +// @dart = 2.2 |
| 43 | +$importLine |
| 44 | +import 'dart:io'; |
| 45 | +import 'dart:isolate'; |
| 46 | +import 'package:pigeon/pigeon_lib.dart'; |
| 47 | +void main(List<String> args, SendPort sendPort) async { |
| 48 | + sendPort.send(await Pigeon.run(args)); |
| 49 | +} |
| 50 | +"""; |
| 51 | + final File tempFile = File(path.join(tempDir.path, '_pigeon_temp_.dart')); |
| 52 | + await tempFile.writeAsString(code); |
| 53 | + final ReceivePort receivePort = ReceivePort(); |
| 54 | + Isolate.spawnUri( |
| 55 | + // Using Uri.file instead of Uri.parse in order to parse backslashes as |
| 56 | + // path segment separator with Windows semantics. |
| 57 | + Uri.file(tempFile.path), |
| 58 | + args, |
| 59 | + receivePort.sendPort, |
| 60 | + packageConfig: packageConfig, |
| 61 | + ); |
| 62 | + |
| 63 | + final Completer<int> completer = Completer<int>(); |
| 64 | + receivePort.listen((dynamic message) { |
| 65 | + try { |
| 66 | + // ignore: avoid_as |
| 67 | + completer.complete(message as int); |
| 68 | + } catch (exception) { |
| 69 | + completer.completeError(exception); |
| 70 | + } |
| 71 | + }); |
| 72 | + final int exitCode = await completer.future; |
| 73 | + tempDir.deleteSync(recursive: true); |
| 74 | + exit(exitCode); |
| 75 | +} |
0 commit comments