-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[pigeon] Relocates generator classes and updates imports #2985
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
Changes from all commits
508c182
9c3023f
99c5c01
68c3511
67fd994
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,14 @@ | |
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:io'; | ||
| import 'package:path/path.dart' as path; | ||
|
|
||
| import 'ast.dart'; | ||
| import 'functional.dart'; | ||
| import 'generator.dart'; | ||
| import 'generator_tools.dart'; | ||
| import 'pigeon_lib.dart' show Error; | ||
| import 'pigeon_lib.dart' show Error, PigeonOptions, lineReader, openSink; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason that the Generator was in the place it was previously is that it was keeping If you wanted to retain that organization it's going to be more difficult if you move the Generator implementations to the generator files. You'll have to make Generator generic: abstract class Generator<T> {
void generate(StringSink sink, T options, Root root);
List<Error> validate(T options, Root root);
}Then when you store adapters between PigeonOptions and the input that the Generator needs where you store all your generators. /// Adapts a Generator to PigeonOptions
class GeneratorAdapter {
GeneratorAdapter({this.generate, this.shouldGenerate, this.validate});
void Function(StringSink sink, PigeonOptions options, Root root) generate;
IOSink? Function(PigeonOptions options) shouldGenerate;
List<Error> Function(PigeonOptions options, Root root) validate;
}
var generator = Generator();
generators.add(GeneratorAdapter(
GeneratorAdapter(
generate:(sink, options, root) => generator.generate(sink, options.fooOptions, root),
shouldGenerate:(options) => _openSink(options.fooOut),
validate:(options, root) => generator.validate(options.fooOptions, root)));The benefits being:
How do you like that?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sonofabitch, late comment. For architecture questions can you guys touch base first. I didn't really have an opportunity to react to this.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can revert+retract if this is an issue. I didn't expect putting the generator classes in the generator files to be controversial.
This is why naming is important; I didn't expect that in a package where the most important conceptual thing are generators that the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, it should have been "_Generator" but was made public to help with testing. We don't need to retract. I think separating the adapter and the generator makes everything tidier and easier to maintain but we can do that in a different PR. I think having a Generator abstract class that people follow along in the generator files makes it a bit easier conceptually instead of having loose functions, so that is an improvement. |
||
|
|
||
| /// General comment opening token. | ||
| const String _commentPrefix = '//'; | ||
|
|
@@ -64,6 +68,54 @@ class CppOptions { | |
| } | ||
| } | ||
|
|
||
| /// A [Generator] that generates C++ header code. | ||
| class CppHeaderGenerator implements Generator { | ||
| /// Constructor for [CppHeaderGenerator]. | ||
| const CppHeaderGenerator(); | ||
|
|
||
| @override | ||
| void generate(StringSink sink, PigeonOptions options, Root root) { | ||
| final CppOptions cppOptions = options.cppOptions ?? const CppOptions(); | ||
| final CppOptions cppOptionsWithHeader = cppOptions.merge(CppOptions( | ||
| copyrightHeader: options.copyrightHeader != null | ||
| ? lineReader(options.copyrightHeader!) | ||
| : null)); | ||
| generateCppHeader(path.basenameWithoutExtension(options.cppHeaderOut!), | ||
| cppOptionsWithHeader, root, sink); | ||
| } | ||
|
|
||
| @override | ||
| IOSink? shouldGenerate(PigeonOptions options) => | ||
| openSink(options.cppHeaderOut); | ||
|
|
||
| @override | ||
| List<Error> validate(PigeonOptions options, Root root) => | ||
| validateCpp(options.cppOptions!, root); | ||
| } | ||
|
|
||
| /// A [Generator] that generates C++ source code. | ||
| class CppSourceGenerator implements Generator { | ||
| /// Constructor for [CppSourceGenerator]. | ||
| const CppSourceGenerator(); | ||
|
|
||
| @override | ||
| void generate(StringSink sink, PigeonOptions options, Root root) { | ||
| final CppOptions cppOptions = options.cppOptions ?? const CppOptions(); | ||
| final CppOptions cppOptionsWithHeader = cppOptions.merge(CppOptions( | ||
| copyrightHeader: options.copyrightHeader != null | ||
| ? lineReader(options.copyrightHeader!) | ||
| : null)); | ||
| generateCppSource(cppOptionsWithHeader, root, sink); | ||
| } | ||
|
|
||
| @override | ||
| IOSink? shouldGenerate(PigeonOptions options) => | ||
| openSink(options.cppSourceOut); | ||
|
|
||
| @override | ||
| List<Error> validate(PigeonOptions options, Root root) => <Error>[]; | ||
| } | ||
|
|
||
| String _getCodecSerializerName(Api api) => '${api.name}CodecSerializer'; | ||
|
|
||
| const String _pointerPrefix = 'pointer'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // 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 'dart:io'; | ||
|
|
||
| import 'ast.dart'; | ||
| import 'pigeon_lib.dart' show Error, PigeonOptions; | ||
|
|
||
| /// A generator that will write code to a sink based on the contents of [PigeonOptions]. | ||
| abstract class Generator { | ||
| /// Constructor for Generator. | ||
| Generator(); | ||
|
|
||
| /// Returns an [IOSink] instance to be written to if the [Generator] should | ||
| /// generate. If it returns `null`, the [Generator] will be skipped. | ||
| IOSink? shouldGenerate(PigeonOptions options); | ||
|
|
||
| /// Write the generated code described in [root] to [sink] using the | ||
| /// [options]. | ||
| void generate(StringSink sink, PigeonOptions options, Root root); | ||
|
|
||
| /// Generates errors that would only be appropriate for this [Generator]. For | ||
| /// example, maybe a certain feature isn't implemented in a [Generator] yet. | ||
| List<Error> validate(PigeonOptions options, Root root); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should ideally move these two utility methods to a utility file, and find somewhere for the other things to live, so we don't have circular includes. Than can be done later though.