Skip to content

Commit

Permalink
[pigeon] moved command line logic from bin/ to lib/ (flutter#313)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
gaaclarke authored Mar 19, 2021
1 parent 7c59510 commit 3337df6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 68 deletions.
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
67 changes: 2 additions & 65 deletions packages/pigeon/bin/pigeon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
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\';');
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:mirrors';
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() {
Expand Down
75 changes: 75 additions & 0 deletions packages/pigeon/lib/pigeon_cl.dart
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 3337df6

Please sign in to comment.