[pigeon] moved command line logic from bin/ to lib/ (#313)
* [pigeon] moved the logic from bin to lib to help others wrap the
command-line tools logic with their own logic.
* Added a linter ignore
diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md
index c3522f0..eaa7efc 100644
--- a/packages/pigeon/CHANGELOG.md
+++ b/packages/pigeon/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.1.24
+
+* Moved logic from bin/ to lib/ to help customers wrap up the behavior.
+* Added some more linter ignores for Dart.
+
## 0.1.23
* More Java linter and linter fixes.
diff --git a/packages/pigeon/bin/pigeon.dart b/packages/pigeon/bin/pigeon.dart
index 33f44a1..ec65f9d 100644
--- a/packages/pigeon/bin/pigeon.dart
+++ b/packages/pigeon/bin/pigeon.dart
@@ -4,71 +4,8 @@
// @dart = 2.2
-import 'dart:async';
-import 'dart:io';
-import 'dart:isolate';
-
-import 'package:path/path.dart' as path;
-import 'package:pigeon/pigeon_lib.dart';
-
-/// This creates a relative path from `from` to `input`, the output being a
-/// posix path on all platforms.
-String _posixRelative(String input, {String from}) {
- final path.Context context = path.Context(style: path.Style.posix);
- final String rawInputPath = input;
- final String absInputPath = File(rawInputPath).absolute.path;
- // By going through URI's we can make sure paths can go between drives in
- // Windows.
- final Uri inputUri = path.toUri(absInputPath);
- final String posixAbsInputPath = context.fromUri(inputUri);
- final Uri tempUri = path.toUri(from);
- final String posixTempPath = context.fromUri(tempUri);
- return context.relative(posixAbsInputPath, from: posixTempPath);
-}
+import 'package:pigeon/pigeon_cl.dart';
Future<void> main(List<String> args) async {
- final PigeonOptions opts = Pigeon.parseArgs(args);
- final Directory tempDir = Directory.systemTemp.createTempSync(
- 'flutter_pigeon.',
- );
-
- String importLine = '';
- if (opts.input != null) {
- final String relInputPath = _posixRelative(opts.input, from: tempDir.path);
- importLine = 'import \'$relInputPath\';\n';
- }
- final String code = """
-// @dart = 2.2
-$importLine
-import 'dart:io';
-import 'dart:isolate';
-import 'package:pigeon/pigeon_lib.dart';
-
-void main(List<String> args, SendPort sendPort) async {
- sendPort.send(await Pigeon.run(args));
-}
-""";
- final File tempFile = File(path.join(tempDir.path, '_pigeon_temp_.dart'));
- await tempFile.writeAsString(code);
- final ReceivePort receivePort = ReceivePort();
- Isolate.spawnUri(
- // Using Uri.file instead of Uri.parse in order to parse backslashes as
- // path segment separator with Windows semantics.
- Uri.file(tempFile.path),
- args,
- receivePort.sendPort,
- );
-
- final Completer<int> completer = Completer<int>();
- receivePort.listen((dynamic message) {
- try {
- // ignore: avoid_as
- completer.complete(message as int);
- } catch (exception) {
- completer.completeError(exception);
- }
- });
- final int exitCode = await completer.future;
- tempDir.deleteSync(recursive: true);
- exit(exitCode);
+ await runCommandLine(args);
}
diff --git a/packages/pigeon/lib/dart_generator.dart b/packages/pigeon/lib/dart_generator.dart
index 2d4d823..168ac86 100644
--- a/packages/pigeon/lib/dart_generator.dart
+++ b/packages/pigeon/lib/dart_generator.dart
@@ -197,7 +197,7 @@
indent.writeln('// $generatedCodeWarning');
indent.writeln('// $seeAlsoWarning');
indent.writeln(
- '// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators',
+ '// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types',
);
indent.writeln('// @dart = ${opt.isNullSafe ? '2.12' : '2.8'}');
indent.writeln('import \'dart:async\';');
diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart
index 6b64189..895c58e 100644
--- a/packages/pigeon/lib/generator_tools.dart
+++ b/packages/pigeon/lib/generator_tools.dart
@@ -8,7 +8,7 @@
import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml.
-const String pigeonVersion = '0.1.23';
+const String pigeonVersion = '0.1.24';
/// Read all the content from [stdin] to a String.
String readStdin() {
diff --git a/packages/pigeon/lib/pigeon_cl.dart b/packages/pigeon/lib/pigeon_cl.dart
new file mode 100644
index 0000000..b605d9d
--- /dev/null
+++ b/packages/pigeon/lib/pigeon_cl.dart
@@ -0,0 +1,75 @@
+// Copyright 2020 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:io';
+import 'dart:isolate';
+
+import 'package:path/path.dart' as path;
+import 'package:pigeon/pigeon_lib.dart';
+
+/// This creates a relative path from `from` to `input`, the output being a
+/// posix path on all platforms.
+String _posixRelative(String input, {String from}) {
+ final path.Context context = path.Context(style: path.Style.posix);
+ final String rawInputPath = input;
+ final String absInputPath = File(rawInputPath).absolute.path;
+ // By going through URI's we can make sure paths can go between drives in
+ // Windows.
+ final Uri inputUri = path.toUri(absInputPath);
+ final String posixAbsInputPath = context.fromUri(inputUri);
+ final Uri tempUri = path.toUri(from);
+ final String posixTempPath = context.fromUri(tempUri);
+ return context.relative(posixAbsInputPath, from: posixTempPath);
+}
+
+/// This is the main entrypoint for the command-line tool. [args] are the
+/// commmand line arguments and there is an optional [packageConfig] to
+/// accomodate users that want to integrate pigeon with other build systems.
+Future<void> runCommandLine(List<String> args, {Uri packageConfig}) async {
+ final PigeonOptions opts = Pigeon.parseArgs(args);
+ final Directory tempDir = Directory.systemTemp.createTempSync(
+ 'flutter_pigeon.',
+ );
+
+ String importLine = '';
+ if (opts.input != null) {
+ final String relInputPath = _posixRelative(opts.input, from: tempDir.path);
+ importLine = 'import \'$relInputPath\';\n';
+ }
+ final String code = """
+// @dart = 2.2
+$importLine
+import 'dart:io';
+import 'dart:isolate';
+import 'package:pigeon/pigeon_lib.dart';
+void main(List<String> args, SendPort sendPort) async {
+ sendPort.send(await Pigeon.run(args));
+}
+""";
+ final File tempFile = File(path.join(tempDir.path, '_pigeon_temp_.dart'));
+ await tempFile.writeAsString(code);
+ final ReceivePort receivePort = ReceivePort();
+ Isolate.spawnUri(
+ // Using Uri.file instead of Uri.parse in order to parse backslashes as
+ // path segment separator with Windows semantics.
+ Uri.file(tempFile.path),
+ args,
+ receivePort.sendPort,
+ packageConfig: packageConfig,
+ );
+
+ final Completer<int> completer = Completer<int>();
+ receivePort.listen((dynamic message) {
+ try {
+ // ignore: avoid_as
+ completer.complete(message as int);
+ } catch (exception) {
+ completer.completeError(exception);
+ }
+ });
+ final int exitCode = await completer.future;
+ tempDir.deleteSync(recursive: true);
+ exit(exitCode);
+}
diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml
index 66894c2..13a23694 100644
--- a/packages/pigeon/pubspec.yaml
+++ b/packages/pigeon/pubspec.yaml
@@ -1,5 +1,5 @@
name: pigeon
-version: 0.1.23 # This must match the version in lib/generator_tools.dart
+version: 0.1.24 # This must match the version in lib/generator_tools.dart
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
dependencies: