Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
df851d5
Rename generator class to Adapter
tarrinneal Dec 21, 2022
e40d679
create new generator class and dart subclass
tarrinneal Dec 21, 2022
8c4e2a0
cpp and dart test gen
tarrinneal Dec 21, 2022
bcdbdbc
added files
tarrinneal Dec 21, 2022
31bb701
Adds Generator class to all generators
tarrinneal Dec 21, 2022
06315ce
adds swift
tarrinneal Dec 21, 2022
5675c64
Updates tests to use new Adapter naming scheme
tarrinneal Dec 21, 2022
a319b48
Dart generate methods
tarrinneal Dec 21, 2022
6d7405d
convert all generate functions to use new method
tarrinneal Dec 21, 2022
72a380c
Merge branch 'main' of github.com:flutter/packages into skeleton2
tarrinneal Dec 21, 2022
1e59fef
chagngelog
tarrinneal Dec 21, 2022
0964cd3
remove Generator class fields
tarrinneal Dec 21, 2022
f589da4
move paths to options
tarrinneal Dec 22, 2022
637679d
remove dartTestOptions
tarrinneal Dec 22, 2022
68a23e2
Nits and combines source and header generators
tarrinneal Dec 27, 2022
67282cf
renames Adapter to GeneratorAdapter
tarrinneal Dec 27, 2022
d08606c
Update version number for breaking changes
tarrinneal Dec 27, 2022
90fcb67
nits
tarrinneal Dec 27, 2022
ea0ec6c
more personal nits
tarrinneal Dec 27, 2022
6206bad
Fixes dart header bug
tarrinneal Dec 27, 2022
7c3d35c
add gen files for clarity
tarrinneal Dec 27, 2022
ef0b71a
Merge branch 'main' of github.com:flutter/packages into skeleton2
tarrinneal Dec 27, 2022
8bf2dfb
better field naming
tarrinneal Dec 28, 2022
8c6abf2
better field naming
tarrinneal Dec 28, 2022
93a5d1b
removed unneeded dart test generator
tarrinneal Dec 28, 2022
710d1a1
Add filetype to generator
tarrinneal Dec 28, 2022
d189d11
Adds filetype as field to generatorAdapters
tarrinneal Dec 29, 2022
daae569
Merge branch 'main' of github.com:flutter/packages into skeleton2
tarrinneal Dec 29, 2022
88905e3
Merge branch 'main' of github.com:flutter/packages into skeleton2
tarrinneal Dec 30, 2022
29decb9
nits
tarrinneal Dec 30, 2022
b7abbe4
assert
tarrinneal Jan 3, 2023
03c44ca
Default FileType
tarrinneal Jan 5, 2023
9901dc5
alt v4
tarrinneal Jan 5, 2023
fe4cb18
alt v5
tarrinneal Jan 5, 2023
922ba3c
nits
tarrinneal Jan 5, 2023
6ca70be
Merge branch 'main'
tarrinneal Jan 6, 2023
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
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.0

* Creates new Generator classes for each language.

## 4.2.15

* Relocates generator classes. (Reverted)
Expand Down
38 changes: 31 additions & 7 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';
import 'pigeon_lib.dart' show Error;

Expand All @@ -21,36 +22,41 @@ const String _defaultCodecSerializer = 'flutter::StandardCodecSerializer';
class CppOptions {
/// Creates a [CppOptions] object
const CppOptions({
this.header,
this.headerIncludePath,
this.namespace,
this.copyrightHeader,
this.headerOutPath,
});

/// The path to the header that will get placed in the source filed (example:
/// "foo.h").
final String? header;
final String? headerIncludePath;

/// The namespace where the generated class will live.
final String? namespace;

/// A copyright header that will get prepended to generated code.
final Iterable<String>? copyrightHeader;

/// The path to the output header file location.
final String? headerOutPath;

/// Creates a [CppOptions] from a Map representation where:
/// `x = CppOptions.fromMap(x.toMap())`.
static CppOptions fromMap(Map<String, Object> map) {
return CppOptions(
header: map['header'] as String?,
headerIncludePath: map['header'] as String?,
namespace: map['namespace'] as String?,
copyrightHeader: map['copyrightHeader'] as Iterable<String>?,
headerOutPath: map['cppHeaderOut'] as String?,
);
}

/// Converts a [CppOptions] to a Map representation where:
/// `x = CppOptions.fromMap(x.toMap())`.
Map<String, Object> toMap() {
final Map<String, Object> result = <String, Object>{
if (header != null) 'header': header!,
if (headerIncludePath != null) 'header': headerIncludePath!,
if (namespace != null) 'namespace': namespace!,
if (copyrightHeader != null) 'copyrightHeader': copyrightHeader!,
};
Expand All @@ -64,6 +70,24 @@ class CppOptions {
}
}

/// Class that manages all Cpp code generation.
class CppGenerator extends Generator<CppOptions> {
/// Instantiates a Cpp Generator.
CppGenerator();

/// Generates Cpp files with specified [CppOptions]
@override
void generate(CppOptions languageOptions, Root root, StringSink sink,
{required FileType fileType}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, this looks good to me. I'd just pull fileType into the CppOptions since it isn't applicable to all Generators.

Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It applies to two already, and will apply to three when we add Linux, so whatever solution we have shouldn't be specific to C++.

If we put it in options, we won't be able to pass the same immutable options to each generate call, which undermines the idea that all calls to generate need to happen with identical options (so that the generated code matches).

assert(fileType == FileType.header || fileType == FileType.source);
if (fileType == FileType.header) {
generateCppHeader(languageOptions, root, sink);
} else {
generateCppSource(languageOptions, root, sink);
}
}
}

String _getCodecSerializerName(Api api) => '${api.name}CodecSerializer';

const String _pointerPrefix = 'pointer';
Expand Down Expand Up @@ -1011,8 +1035,8 @@ void _writeSystemHeaderIncludeBlock(Indent indent, List<String> headers) {

/// Generates the ".h" file for the AST represented by [root] to [sink] with the
/// provided [options] and [headerFileName].
void generateCppHeader(
String? headerFileName, CppOptions options, Root root, StringSink sink) {
void generateCppHeader(CppOptions options, Root root, StringSink sink) {
final String? headerFileName = options.headerOutPath;
final Indent indent = Indent(sink);
if (options.copyrightHeader != null) {
addLines(indent, options.copyrightHeader!, linePrefix: '// ');
Expand Down Expand Up @@ -1113,7 +1137,7 @@ void generateCppSource(CppOptions options, Root root, StringSink sink) {
indent.addln('#undef _HAS_EXCEPTIONS');
indent.addln('');

indent.writeln('#include "${options.header}"');
indent.writeln('#include "${options.headerIncludePath}"');
indent.addln('');
_writeSystemHeaderIncludeBlock(indent, <String>[
'flutter/basic_message_channel.h',
Expand Down
52 changes: 47 additions & 5 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:yaml/yaml.dart' as yaml;

import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';

/// Documentation comment open symbol.
Expand All @@ -24,18 +25,30 @@ const String _standardMessageCodec = 'StandardMessageCodec';
/// Options that control how Dart code will be generated.
class DartOptions {
/// Constructor for DartOptions.
const DartOptions({this.copyrightHeader});
DartOptions({
this.copyrightHeader,
this.sourceOutPath,
this.testOutPath,
});

/// A copyright header that will get prepended to generated code.
final Iterable<String>? copyrightHeader;

/// Path to output generated Dart file.
String? sourceOutPath;

/// Path to output generated Test file for tests.
String? testOutPath;

/// Creates a [DartOptions] from a Map representation where:
/// `x = DartOptions.fromMap(x.toMap())`.
static DartOptions fromMap(Map<String, Object> map) {
final Iterable<dynamic>? copyrightHeader =
map['copyrightHeader'] as Iterable<dynamic>?;
return DartOptions(
copyrightHeader: copyrightHeader?.cast<String>(),
sourceOutPath: map['sourceOutPath'] as String?,
testOutPath: map['testOutPath'] as String?,
);
}

Expand All @@ -44,6 +57,8 @@ class DartOptions {
Map<String, Object> toMap() {
final Map<String, Object> result = <String, Object>{
if (copyrightHeader != null) 'copyrightHeader': copyrightHeader!,
if (sourceOutPath != null) 'sourceOutPath': sourceOutPath!,
if (testOutPath != null) 'testOutPath': testOutPath!,
};
return result;
}
Expand All @@ -55,6 +70,33 @@ class DartOptions {
}
}

/// Class that manages all Dart code generation.
class DartGenerator extends Generator<DartOptions> {
/// Instantiates a Dart Generator.
DartGenerator();

/// Generates Dart files with specified [DartOptions]
@override
void generate(DartOptions languageOptions, Root root, StringSink sink,
{FileType fileType = FileType.NA}) {
assert(fileType == FileType.NA);
generateDart(languageOptions, root, sink);
}

/// Generates Dart files for testing with specified [DartOptions]
void generateTest(DartOptions languageOptions, Root root, StringSink sink) {
final String sourceOutPath = languageOptions.sourceOutPath ?? '';
final String testOutPath = languageOptions.testOutPath ?? '';
generateTestDart(
languageOptions,
root,
sink,
sourceOutPath: sourceOutPath,
testOutPath: testOutPath,
);
}
}

String _escapeForDartSingleQuotedString(String raw) {
return raw
.replaceAll(r'\', r'\\')
Expand Down Expand Up @@ -695,14 +737,14 @@ String _posixify(String inputPath) {
}

/// Generates Dart source code for test support libraries based on the given AST
/// represented by [root], outputting the code to [sink]. [dartOutPath] is the
/// represented by [root], outputting the code to [sink]. [sourceOutPath] is the
/// path of the generated dart code to be tested. [testOutPath] is where the
/// test code will be generated.
void generateTestDart(
DartOptions opt,
Root root,
StringSink sink, {
required String dartOutPath,
required String sourceOutPath,
required String testOutPath,
}) {
final Indent indent = Indent(sink);
Expand All @@ -726,10 +768,10 @@ void generateTestDart(
indent.writeln('');
final String relativeDartPath =
path.Context(style: path.Style.posix).relative(
_posixify(dartOutPath),
_posixify(sourceOutPath),
from: _posixify(path.dirname(testOutPath)),
);
late final String? packageName = _deducePackageName(dartOutPath);
late final String? packageName = _deducePackageName(sourceOutPath);
if (!relativeDartPath.contains('/lib/') || packageName == null) {
// If we can't figure out the package name or the relative path doesn't
// include a 'lib' directory, try relative path import which only works in
Expand Down
15 changes: 15 additions & 0 deletions packages/pigeon/lib/generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 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 'ast.dart';
import 'generator_tools.dart';

/// A superclass of generator classes.
///
/// This provides the structure that is common across generators for different languages.
abstract class Generator<T> {
/// Generates files for specified language with specified [languageOptions]
void generate(T languageOptions, Root root, StringSink sink,
{required FileType fileType});
}
14 changes: 13 additions & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'dart:mirrors';
import 'ast.dart';

/// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '4.2.15';
const String pigeonVersion = '5.0.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down Expand Up @@ -497,3 +497,15 @@ Iterable<NamedType> getFieldsInSerializationOrder(Class klass) {
// This returns the fields in the order they are declared in the pigeon file.
return klass.fields;
}

/// Enum to specify which file will be generated for multi-file generators
enum FileType {
/// header file.
header,

/// source file.
source,

/// file type is not applicable.
NA,
}
15 changes: 15 additions & 0 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';
import 'pigeon_lib.dart' show TaskQueueType;

Expand Down Expand Up @@ -84,6 +85,20 @@ class JavaOptions {
}
}

/// Class that manages all Java code generation.
class JavaGenerator extends Generator<JavaOptions> {
/// Instantiates a Java Generator.
JavaGenerator();

/// Generates Java files with specified [JavaOptions]
@override
void generate(JavaOptions languageOptions, Root root, StringSink sink,
{FileType fileType = FileType.NA}) {
assert(fileType == FileType.NA);
generateJava(languageOptions, root, sink);
}
}

/// Calculates the name of the codec that will be generated for [api].
String _getCodecName(Api api) => '${api.name}Codec';

Expand Down
15 changes: 15 additions & 0 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';
import 'pigeon_lib.dart' show TaskQueueType;

Expand Down Expand Up @@ -64,6 +65,20 @@ class KotlinOptions {
}
}

/// Class that manages all Kotlin code generation.
class KotlinGenerator extends Generator<KotlinOptions> {
/// Instantiates a Kotlin Generator.
KotlinGenerator();

/// Generates Kotlin files with specified [KotlinOptions]
@override
void generate(KotlinOptions languageOptions, Root root, StringSink sink,
{FileType fileType = FileType.NA}) {
assert(fileType == FileType.NA);
generateKotlin(languageOptions, root, sink);
}
}

/// Calculates the name of the codec that will be generated for [api].
String _getCodecName(Api api) => '${api.name}Codec';

Expand Down
30 changes: 25 additions & 5 deletions packages/pigeon/lib/objc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';
import 'pigeon_lib.dart' show Error, TaskQueueType;

Expand All @@ -18,14 +19,14 @@ const DocumentCommentSpecification _docCommentSpec =
class ObjcOptions {
/// Parametric constructor for ObjcOptions.
const ObjcOptions({
this.header,
this.headerIncludePath,
this.prefix,
this.copyrightHeader,
});

/// The path to the header that will get placed in the source filed (example:
/// "foo.h").
final String? header;
final String? headerIncludePath;

/// Prefix that will be appended before all generated classes and protocols.
final String? prefix;
Expand All @@ -39,7 +40,7 @@ class ObjcOptions {
final Iterable<dynamic>? copyrightHeader =
map['copyrightHeader'] as Iterable<dynamic>?;
return ObjcOptions(
header: map['header'] as String?,
headerIncludePath: map['header'] as String?,
prefix: map['prefix'] as String?,
copyrightHeader: copyrightHeader?.cast<String>(),
);
Expand All @@ -49,7 +50,7 @@ class ObjcOptions {
/// `x = ObjcOptions.fromMap(x.toMap())`.
Map<String, Object> toMap() {
final Map<String, Object> result = <String, Object>{
if (header != null) 'header': header!,
if (headerIncludePath != null) 'header': headerIncludePath!,
if (prefix != null) 'prefix': prefix!,
if (copyrightHeader != null) 'copyrightHeader': copyrightHeader!,
};
Expand All @@ -63,6 +64,25 @@ class ObjcOptions {
}
}

/// Class that manages all Objc header code generation.
class ObjcGenerator extends Generator<ObjcOptions> {
/// Instantiates a Objc Generator.
ObjcGenerator();

/// Generates Objc files with specified [ObjcOptions]
@override
void generate(ObjcOptions languageOptions, Root root, StringSink sink,
{required FileType fileType}) {
assert(fileType == FileType.header || fileType == FileType.source);

if (fileType == FileType.header) {
generateObjcHeader(languageOptions, root, sink);
} else {
generateObjcSource(languageOptions, root, sink);
}
}
}

/// Calculates the ObjC class name, possibly prefixed.
String _className(String? prefix, String className) {
if (prefix != null) {
Expand Down Expand Up @@ -890,7 +910,7 @@ void generateObjcSource(ObjcOptions options, Root root, StringSink sink) {
}

void writeImports() {
indent.writeln('#import "${options.header}"');
indent.writeln('#import "${options.headerIncludePath}"');
indent.writeln('#import <Flutter/Flutter.h>');
}

Expand Down
Loading