Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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.16

* [swift] Fixes warnings with `Object` parameters.
Expand Down
39 changes: 32 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,25 @@ class CppOptions {
}
}

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

/// Generates Cpp files with specified [OutputFileOptions<CppOptions>]
@override
void generate(OutputFileOptions<CppOptions> languageOptions, Root root,
StringSink sink) {
final FileType fileType = languageOptions.fileType;
assert(fileType == FileType.header || fileType == FileType.source);
if (fileType == FileType.header) {
generateCppHeader(languageOptions.languageOptions, root, sink);
} else {
generateCppSource(languageOptions.languageOptions, root, sink);
}
}
}

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

const String _pointerPrefix = 'pointer';
Expand Down Expand Up @@ -1026,8 +1051,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 @@ -1128,7 +1153,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 @@ -699,14 +741,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 @@ -730,10 +772,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
13 changes: 13 additions & 0 deletions packages/pigeon/lib/generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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';

/// 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);
}
28 changes: 27 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.16';
const String pigeonVersion = '5.0.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down Expand Up @@ -497,3 +497,29 @@ 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,
}

/// Options for [Generator]'s that have multiple output file types.
///
/// Specifies which file to write as well as wraps all language options.
class OutputFileOptions<T> {
/// Constructor.
OutputFileOptions({required this.fileType, required this.languageOptions});

/// To specify which file type should be created.
FileType fileType;

/// Options for specified language across all file types.
T languageOptions;
}
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
Loading