Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 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
af5e0e6
Moves write header to generator class method
tarrinneal Dec 22, 2022
ae817ad
Updates tests to use new generator class
tarrinneal Dec 22, 2022
5cecc21
source -> header
tarrinneal Dec 22, 2022
99f25b5
correct options
tarrinneal Dec 22, 2022
16df207
header -> source
tarrinneal Dec 22, 2022
049c6be
header -> prefix, source -> header
tarrinneal Dec 22, 2022
a2dcb1c
remove headers from generateTestDart
tarrinneal Dec 22, 2022
e4c78f5
changelog
tarrinneal Dec 22, 2022
4f36bf4
Merge branch 'main' of github.com:flutter/packages into skelington3
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
e99526d
Merge branch 'skeleton2' into skelington3
tarrinneal Dec 27, 2022
d5c5f1f
update tests to match new merged generators
tarrinneal Dec 27, 2022
6856812
cleaner header methods
tarrinneal Dec 27, 2022
6206bad
Fixes dart header bug
tarrinneal Dec 27, 2022
f17c0a3
Merge branch 'skeleton2' into skelington3
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
8e068fa
Merge branch 'skeleton2' into skelington3
tarrinneal Dec 29, 2022
859c6cb
merge
tarrinneal Dec 29, 2022
daae569
Merge branch 'main' of github.com:flutter/packages into skeleton2
tarrinneal Dec 29, 2022
f23b5b6
Merge branch 'main' of github.com:flutter/packages into skelington3
tarrinneal Dec 29, 2022
6dff8cb
analyze
tarrinneal Dec 29, 2022
c17c1e4
add import method
tarrinneal Dec 29, 2022
d9f116f
re-remove DartTestGenerator
tarrinneal Dec 29, 2022
1094eeb
Merge branch 'skelington3' into imports-skeleton
tarrinneal Dec 29, 2022
e57de98
Moves imports to new method
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
00ea54b
Merge branch 'skeleton2' into skelington3
tarrinneal Jan 3, 2023
bd3d2a3
Merge branch 'skelington3' into imports-skeleton
tarrinneal Jan 3, 2023
9664a06
Creates writeHeader method on Generator classes
tarrinneal Jan 6, 2023
ec289bc
Merge branch 'writeHeaders' into imports-skeleton
tarrinneal Jan 6, 2023
3a01ade
changelog
tarrinneal Jan 7, 2023
02fd734
prologue
tarrinneal Jan 7, 2023
31189c8
Merge branch 'writeHeaders' into imports-skeleton
tarrinneal Jan 7, 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
8 changes: 8 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 5.0.2

* Adds writeImports method to Generator classes.

## 5.0.1

* Adds writeHeaders method to Generator classes and updates tests to use new Generators.

## 5.0.0

* Creates new Generator classes for each language.
Expand Down
82 changes: 65 additions & 17 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,46 @@ class CppGenerator extends Generator<OutputFileOptions<CppOptions>> {
/// Instantiates a Cpp Generator.
CppGenerator();

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

final Indent indent = Indent(sink);
writeFilePrologue(generatorOptions, root, sink, indent);
writeFileImports(generatorOptions, root, sink, indent);
if (fileType == FileType.header) {
generateCppHeader(generatorOptions.languageOptions, root, sink, indent);
} else {
generateCppSource(generatorOptions.languageOptions, root, sink, indent);
}
}

@override
void writeFilePrologue(OutputFileOptions<CppOptions> generatorOptions,
Root root, StringSink sink, Indent indent) {
final FileType fileType = generatorOptions.fileType;
if (fileType == FileType.header) {
generateCppHeader(languageOptions.languageOptions, root, sink);
writeCppHeaderPrologue(
generatorOptions.languageOptions, root, sink, indent);
} else {
generateCppSource(languageOptions.languageOptions, root, sink);
writeCppSourcePrologue(
generatorOptions.languageOptions, root, sink, indent);
}
}

@override
void writeFileImports(OutputFileOptions<CppOptions> generatorOptions,
Root root, StringSink sink, Indent indent) {
final FileType fileType = generatorOptions.fileType;
if (fileType == FileType.header) {
writeCppHeaderImports(
generatorOptions.languageOptions, root, sink, indent);
} else {
writeCppSourceImports(
generatorOptions.languageOptions, root, sink, indent);
}
}
}
Expand Down Expand Up @@ -1049,18 +1079,22 @@ 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(CppOptions options, Root root, StringSink sink) {
final String? headerFileName = options.headerOutPath;
final Indent indent = Indent(sink);
/// Writes Cpp header file header to sink.
void writeCppHeaderPrologue(
CppOptions options, Root root, StringSink sink, Indent indent) {
if (options.copyrightHeader != null) {
addLines(indent, options.copyrightHeader!, linePrefix: '// ');
}
indent.writeln('$_commentPrefix $generatedCodeWarning');
indent.writeln('$_commentPrefix $seeAlsoWarning');
indent.addln('');
final String guardName = _getGuardName(headerFileName, options.namespace);
}

/// Writes Cpp header file imports to sink.
void writeCppHeaderImports(
CppOptions options, Root root, StringSink sink, Indent indent) {
final String guardName =
_getGuardName(options.headerIncludePath, options.namespace);
indent.writeln('#ifndef $guardName');
indent.writeln('#define $guardName');

Expand All @@ -1077,11 +1111,16 @@ void generateCppHeader(CppOptions options, Root root, StringSink sink) {
'optional',
]);
indent.addln('');

if (options.namespace != null) {
indent.writeln('namespace ${options.namespace} {');
}
indent.addln('');
}

/// Generates the ".h" file for the AST represented by [root] to [sink] with the
/// provided [options] and [headerFileName].
void generateCppHeader(
CppOptions options, Root root, StringSink sink, Indent indent) {
// When generating for a Pigeon unit test, add a test fixture friend class to
// allow unit testing private methods, since testing serialization via public
// methods is essentially an end-to-end test.
Expand Down Expand Up @@ -1136,14 +1175,14 @@ void generateCppHeader(CppOptions options, Root root, StringSink sink) {
if (options.namespace != null) {
indent.writeln('} // namespace ${options.namespace}');
}

final String guardName =
_getGuardName(options.headerIncludePath, options.namespace);
indent.writeln('#endif // $guardName');
}

/// Generates the ".cpp" file for the AST represented by [root] to [sink] with the
/// provided [options].
void generateCppSource(CppOptions options, Root root, StringSink sink) {
final Indent indent = Indent(sink);
/// Writes Cpp source file header to sink.
void writeCppSourcePrologue(
CppOptions options, Root root, StringSink sink, Indent indent) {
if (options.copyrightHeader != null) {
addLines(indent, options.copyrightHeader!, linePrefix: '// ');
}
Expand All @@ -1152,7 +1191,11 @@ void generateCppSource(CppOptions options, Root root, StringSink sink) {
indent.addln('');
indent.addln('#undef _HAS_EXCEPTIONS');
indent.addln('');
}

/// Writes Cpp source file imports to sink.
void writeCppSourceImports(
CppOptions options, Root root, StringSink sink, Indent indent) {
indent.writeln('#include "${options.headerIncludePath}"');
indent.addln('');
_writeSystemHeaderIncludeBlock(indent, <String>[
Expand All @@ -1172,7 +1215,12 @@ void generateCppSource(CppOptions options, Root root, StringSink sink) {
if (options.namespace != null) {
indent.writeln('namespace ${options.namespace} {');
}
}

/// Generates the ".cpp" file for the AST represented by [root] to [sink] with the
/// provided [options].
void generateCppSource(
CppOptions options, Root root, StringSink sink, Indent indent) {
for (final Class klass in root.classes) {
_writeDataClassImplementation(indent, klass, root);
}
Expand Down
120 changes: 74 additions & 46 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,38 @@ class DartGenerator extends Generator<DartOptions> {

/// 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);
void generate(DartOptions generatorOptions, Root root, StringSink sink) {
final Indent indent = Indent(sink);

writeFilePrologue(generatorOptions, root, sink, indent);
writeFileImports(generatorOptions, root, sink, indent);
generateDart(generatorOptions, root, sink, indent);
}

@override
void writeFilePrologue(
DartOptions generatorOptions, Root root, StringSink sink, Indent indent) {
writePrologue(generatorOptions, root, sink, indent);
}

@override
void writeFileImports(
DartOptions generatorOptions, Root root, StringSink sink, Indent indent) {
writeImports(generatorOptions, root, sink, indent);
}

/// 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 ?? '';
void generateTest(DartOptions generatorOptions, Root root, StringSink sink) {
final Indent indent = Indent(sink);
final String sourceOutPath = generatorOptions.sourceOutPath ?? '';
final String testOutPath = generatorOptions.testOutPath ?? '';
writeTestHeader(generatorOptions, root, sink, indent);
writeTestImports(generatorOptions, root, sink, indent);
generateTestDart(
languageOptions,
generatorOptions,
root,
sink,
indent,
sourceOutPath: sourceOutPath,
testOutPath: testOutPath,
);
Expand Down Expand Up @@ -501,25 +519,38 @@ String _addGenericTypesNullable(TypeDeclaration type) {
return type.isNullable ? '$genericdType?' : genericdType;
}

/// Writes file header to sink.
void writePrologue(DartOptions opt, Root root, StringSink sink, Indent indent) {
if (opt.copyrightHeader != null) {
addLines(indent, opt.copyrightHeader!, linePrefix: '// ');
}
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, omit_local_variable_types, unused_shown_name, unnecessary_import',
);
indent.addln('');
}

/// Writes file imports to sink.
void writeImports(DartOptions opt, Root root, StringSink sink, Indent indent) {
indent.writeln("import 'dart:async';");
indent.writeln(
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
);
indent.addln('');
indent.writeln(
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
indent.writeln("import 'package:flutter/services.dart';");
}

/// Generates Dart source code for the given AST represented by [root],
/// outputting the code to [sink].
void generateDart(DartOptions opt, Root root, StringSink sink) {
void generateDart(DartOptions opt, Root root, StringSink sink, Indent indent) {
final List<String> customClassNames =
root.classes.map((Class x) => x.name).toList();
final List<String> customEnumNames =
root.enums.map((Enum x) => x.name).toList();
final Indent indent = Indent(sink);

void writeHeader() {
if (opt.copyrightHeader != null) {
addLines(indent, opt.copyrightHeader!, linePrefix: '// ');
}
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, omit_local_variable_types, unused_shown_name, unnecessary_import',
);
}

void writeEnums() {
for (final Enum anEnum in root.enums) {
Expand All @@ -537,17 +568,6 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
}
}

void writeImports() {
indent.writeln("import 'dart:async';");
indent.writeln(
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
);
indent.addln('');
indent.writeln(
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;");
indent.writeln("import 'package:flutter/services.dart';");
}

void writeDataClass(Class klass) {
void writeConstructor() {
indent.write(klass.name);
Expand Down Expand Up @@ -678,8 +698,6 @@ $resultAt != null
}
}

writeHeader();
writeImports();
writeEnums();
for (final Class klass in root.classes) {
indent.writeln('');
Expand Down Expand Up @@ -740,18 +758,9 @@ String _posixify(String inputPath) {
return context.fromUri(path.toUri(path.absolute(inputPath)));
}

/// Generates Dart source code for test support libraries based on the given AST
/// 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 sourceOutPath,
required String testOutPath,
}) {
final Indent indent = Indent(sink);
/// Writes file header to sink.
void writeTestHeader(
DartOptions opt, Root root, StringSink sink, Indent indent) {
if (opt.copyrightHeader != null) {
addLines(indent, opt.copyrightHeader!, linePrefix: '// ');
}
Expand All @@ -761,6 +770,11 @@ void generateTestDart(
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import',
);
indent.writeln('// ignore_for_file: avoid_relative_lib_imports');
}

/// Writes file imports to sink.
void writeTestImports(
DartOptions opt, Root root, StringSink sink, Indent indent) {
indent.writeln("import 'dart:async';");
indent.writeln(
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;",
Expand All @@ -770,6 +784,20 @@ void generateTestDart(
indent.writeln("import 'package:flutter/services.dart';");
indent.writeln("import 'package:flutter_test/flutter_test.dart';");
indent.writeln('');
}

/// Generates Dart source code for test support libraries based on the given AST
/// represented by [root], outputting the code to [sink]. [dartOutPath] 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,
Indent indent, {
required String sourceOutPath,
required String testOutPath,
}) {
final String relativeDartPath =
path.Context(style: path.Style.posix).relative(
_posixify(sourceOutPath),
Expand Down
31 changes: 29 additions & 2 deletions packages/pigeon/lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,38 @@
// 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);
/// Generates files for specified language with specified [generatorOptions]
///
/// This method, when overridden, should follow a generic structure that is currently:
/// 1. Create Indent
/// 2. Write File Headers
/// 3. Write Imports
/// 4. Generate File
void generate(
T generatorOptions,
Root root,
StringSink sink,
);

/// Adds specified file headers.
void writeFilePrologue(
T generatorOptions,
Root root,
StringSink sink,
Indent indent,
);

/// Adds specified imports.
void writeFileImports(
T generatorOptions,
Root root,
StringSink sink,
Indent indent,
);
}
4 changes: 2 additions & 2 deletions 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 = '5.0.0';
const String pigeonVersion = '5.0.2';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down Expand Up @@ -510,7 +510,7 @@ enum FileType {
na,
}

/// Options for [Generator]'s that have multiple output file types.
/// 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> {
Expand Down
Loading