Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] a few tweaks to the output after having upgraded video_player #663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.0.17

* [dart_test] Adds missing linter ignores.
* [objc] Factors out helper function for reading from NSDictionary's.
* [objc] Renames static variables to match Google style.

## 1.0.16

* Updates behavior of run\_tests.dart with no arguments.
Expand Down
35 changes: 34 additions & 1 deletion packages/pigeon/bin/run_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
///
/// usage: pub run pigeon:run_tests
////////////////////////////////////////////////////////////////////////////////
import 'dart:io' show Process, Platform, exit, stdout, stderr;
import 'dart:io' show File, Process, Platform, exit, stderr, stdout;
import 'package:args/args.dart';
import 'package:meta/meta.dart';
import 'package:pigeon/functional.dart';
Expand Down Expand Up @@ -103,6 +103,34 @@ Future<int> _generateDart(Map<String, String> jobs) async {
return 0;
}

Future<int> _analyzeFlutterUnitTests(String flutterUnitTestsPath) async {
final String messagePath = '$flutterUnitTestsPath/lib/message.gen.dart';
final String messageTestPath = '$flutterUnitTestsPath/test/message_test.dart';
final int generateTestCode = await _runPigeon(
input: 'pigeons/message.dart',
dartOut: messagePath,
dartTestOut: messageTestPath,
streamOutput: true,
);
if (generateTestCode != 0) {
return generateTestCode;
}

final int analyzeCode = await _runProcess(
'flutter',
<String>['analyze'],
workingDirectory: flutterUnitTestsPath,
);
if (analyzeCode != 0) {
return analyzeCode;
}

// Delete these files that were just generated to help with the analyzer step.
File(messagePath).deleteSync();
File(messageTestPath).deleteSync();
return 0;
}

Future<int> _runFlutterUnitTests() async {
const String flutterUnitTestsPath =
'platform_tests/flutter_null_safe_unit_tests';
Expand All @@ -121,6 +149,11 @@ Future<int> _runFlutterUnitTests() async {
return generateCode;
}

final int analyzeCode = await _analyzeFlutterUnitTests(flutterUnitTestsPath);
if (analyzeCode != 0) {
return analyzeCode;
}

final int testCode = await _runProcess(
'flutter',
<String>['test'],
Expand Down
5 changes: 4 additions & 1 deletion packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,9 @@ void generateTestDart(
indent.writeln('// $generatedCodeWarning');
indent.writeln('// $seeAlsoWarning');
indent.writeln(
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import',
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis',
);
indent.writeln('// ignore_for_file: avoid_relative_lib_imports');
indent.writeln('// @dart = ${opt.isNullSafe ? '2.12' : '2.8'}');
indent.writeln('import \'dart:async\';');
indent.writeln(
Expand All @@ -590,6 +591,8 @@ void generateTestDart(
indent.writeln('import \'package:flutter/services.dart\';');
indent.writeln('import \'package:flutter_test/flutter_test.dart\';');
indent.writeln('');
// TODO(gaaclarke): Switch from relative paths to URIs. This fails in new versions of Dart,
// https://github.com/flutter/flutter/issues/97744.
indent.writeln(
'import \'${_escapeForDartSingleQuotedString(mainDartFile)}\';',
);
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 = '1.0.16';
const String pigeonVersion = '1.0.17';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
36 changes: 18 additions & 18 deletions packages/pigeon/lib/objc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ void _writeCodec(
@end

NSObject<FlutterMessageCodec> *${_getCodecGetterName(options.prefix, api.name)}() {
\tstatic dispatch_once_t s_pred = 0;
\tstatic FlutterStandardMessageCodec *s_sharedObject = nil;
\tdispatch_once(&s_pred, ^{
\tstatic dispatch_once_t sPred = 0;
\tstatic FlutterStandardMessageCodec *sSharedObject = nil;
\tdispatch_once(&sPred, ^{
\t\t$readerWriterName *readerWriter = [[$readerWriterName alloc] init];
\t\ts_sharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter];
\t\tsSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter];
\t});
\treturn s_sharedObject;
\treturn sSharedObject;
}
''');
}
Expand Down Expand Up @@ -551,9 +551,9 @@ String _dictGetter(
if (prefix != null) {
className = '$prefix$className';
}
return '[$className fromMap:$dict[@"${field.name}"]]';
return '[$className fromMap:GetNullableObject($dict, @"${field.name}")]';
} else {
return '$dict[@"${field.name}"]';
return 'GetNullableObject($dict, @"${field.name}")';
}
}

Expand Down Expand Up @@ -811,7 +811,7 @@ void generateObjcSource(ObjcOptions options, Root root, StringSink sink) {
indent.writeln('#endif');
}

void writeWrapResultFunction() {
void writeHelperFunctions() {
indent.format('''
static NSDictionary<NSString *, id> *wrapResult(id result, FlutterError *error) {
\tNSDictionary *errorDict = (NSDictionary *)[NSNull null];
Expand All @@ -827,6 +827,12 @@ static NSDictionary<NSString *, id> *wrapResult(id result, FlutterError *error)
\t\t\t@"${Keys.error}": errorDict,
\t\t\t};
}''');
indent.format('''
static id GetNullableObject(NSDictionary* dict, id key) {
\tid result = dict[key];
\treturn (result == [NSNull null]) ? nil : result;
}
''');
}

void writeDataClassExtension(Class klass) {
Expand Down Expand Up @@ -864,15 +870,9 @@ static NSDictionary<NSString *, id> *wrapResult(id result, FlutterError *error)
} else {
indent.writeln(
'$resultName.${field.name} = ${_dictGetter(classNames, 'dict', field, options.prefix)};');
if (field.type.isNullable) {
indent.write(
'if ((NSNull *)$resultName.${field.name} == [NSNull null]) ');
indent.scoped('{', '}', () {
indent.writeln('$resultName.${field.name} = nil;');
});
} else {
indent.writeln(
'NSAssert((NSNull *)$resultName.${field.name} != [NSNull null], @"");');
if (!field.type.isNullable) {
indent
.writeln('NSAssert($resultName.${field.name} != nil, @"");');
}
}
}
Expand Down Expand Up @@ -915,7 +915,7 @@ static NSDictionary<NSString *, id> *wrapResult(id result, FlutterError *error)
indent.writeln('');
writeArcEnforcer();
indent.addln('');
writeWrapResultFunction();
writeHelperFunctions();
indent.addln('');
root.classes.forEach(writeDataClassExtension);
indent.writeln('');
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class DartTestGenerator implements Generator {

@override
IOSink? shouldGenerate(PigeonOptions options) {
if (options.dartTestOut != null && options.dartOut != null) {
if (options.dartTestOut != null) {
return _openSink(options.dartTestOut);
} else {
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
version: 1.0.16 # This must match the version in lib/generator_tools.dart
version: 1.0.17 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
13 changes: 9 additions & 4 deletions packages/pigeon/test/objc_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ void main() {
expect(code, contains('#import "foo.h"'));
expect(code, contains('@implementation Foobar'));
expect(
code, contains('pigeonResult.enum1 = [dict[@"enum1"] integerValue];'));
code,
contains(
'pigeonResult.enum1 = [GetNullableObject(dict, @"enum1") integerValue];'));
});

test('gen one class header with enum', () {
Expand Down Expand Up @@ -308,7 +310,8 @@ void main() {
generateObjcSource(const ObjcOptions(header: 'foo.h'), root, sink);
final String code = sink.toString();
expect(code, contains('@implementation Foobar'));
expect(code, contains('pigeonResult.aBool = dict[@"aBool"];'));
expect(code,
contains('pigeonResult.aBool = GetNullableObject(dict, @"aBool");'));
});

test('nested class header', () {
Expand Down Expand Up @@ -351,8 +354,10 @@ void main() {
final StringBuffer sink = StringBuffer();
generateObjcSource(const ObjcOptions(header: 'foo.h'), root, sink);
final String code = sink.toString();
expect(code,
contains('pigeonResult.nested = [Input fromMap:dict[@"nested"]];'));
expect(
code,
contains(
'pigeonResult.nested = [Input fromMap:GetNullableObject(dict, @"nested")];'));
expect(code, matches('[self.nested toMap].*@"nested"'));
});

Expand Down