Visitar URL original
[pigeon] moved the logic from bin to lib to help others wrap the · flutter/packages@9b2ea4a · GitHub
Skip to content

Commit 9b2ea4a

Browse files
committed
[pigeon] moved the logic from bin to lib to help others wrap the
command-line tools logic with their own logic.
1 parent 4e51e34 commit 9b2ea4a

5 files changed

Lines changed: 83 additions & 67 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.24
2+
3+
* Moved logic from bin/ to lib/ to help customers wrap up the behavior.
4+
15
## 0.1.23
26

37
* More Java linter and linter fixes.

‎packages/pigeon/bin/pigeon.dart‎

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,8 @@
44

55
// @dart = 2.2
66

7-
import 'dart:async';
8-
import 'dart:io';
9-
import 'dart:isolate';
10-
11-
import 'package:path/path.dart' as path;
12-
import 'package:pigeon/pigeon_lib.dart';
13-
14-
/// This creates a relative path from `from` to `input`, the output being a
15-
/// posix path on all platforms.
16-
String _posixRelative(String input, {String from}) {
17-
final path.Context context = path.Context(style: path.Style.posix);
18-
final String rawInputPath = input;
19-
final String absInputPath = File(rawInputPath).absolute.path;
20-
// By going through URI's we can make sure paths can go between drives in
21-
// Windows.
22-
final Uri inputUri = path.toUri(absInputPath);
23-
final String posixAbsInputPath = context.fromUri(inputUri);
24-
final Uri tempUri = path.toUri(from);
25-
final String posixTempPath = context.fromUri(tempUri);
26-
return context.relative(posixAbsInputPath, from: posixTempPath);
27-
}
7+
import 'package:pigeon/pigeon_cl.dart';
288

299
Future<void> main(List<String> args) async {
30-
final PigeonOptions opts = Pigeon.parseArgs(args);
31-
final Directory tempDir = Directory.systemTemp.createTempSync(
32-
'flutter_pigeon.',
33-
);
34-
35-
String importLine = '';
36-
if (opts.input != null) {
37-
final String relInputPath = _posixRelative(opts.input, from: tempDir.path);
38-
importLine = 'import \'$relInputPath\';\n';
39-
}
40-
final String code = """
41-
// @dart = 2.2
42-
$importLine
43-
import 'dart:io';
44-
import 'dart:isolate';
45-
import 'package:pigeon/pigeon_lib.dart';
46-
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-
);
61-
62-
final Completer<int> completer = Completer<int>();
63-
receivePort.listen((dynamic message) {
64-
try {
65-
// ignore: avoid_as
66-
completer.complete(message as int);
67-
} catch (exception) {
68-
completer.completeError(exception);
69-
}
70-
});
71-
final int exitCode = await completer.future;
72-
tempDir.deleteSync(recursive: true);
73-
exit(exitCode);
10+
await runCommandLine(args);
7411
}

‎packages/pigeon/lib/generator_tools.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:mirrors';
88
import 'ast.dart';
99

1010
/// The current version of pigeon. This must match the version in pubspec.yaml.
11-
const String pigeonVersion = '0.1.23';
11+
const String pigeonVersion = '0.1.24';
1212

1313
/// Read all the content from [stdin] to a String.
1414
String readStdin() {

‎packages/pigeon/lib/pigeon_cl.dart‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

‎packages/pigeon/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pigeon
2-
version: 0.1.23 # This must match the version in lib/generator_tools.dart
2+
version: 0.1.24 # This must match the version in lib/generator_tools.dart
33
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
44
homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
55
dependencies:

0 commit comments

Comments
 (0)