-
Notifications
You must be signed in to change notification settings - Fork 197
Add dart_package option #298
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -104,7 +104,9 @@ class FileGenerator extends ProtobufContainer { | |
| final FileDescriptorProto descriptor; | ||
| final GenerationOptions options; | ||
|
|
||
| // The relative path used to import the .proto file, as a URI. | ||
| // The path used to import the .proto file, as a URI. | ||
| // This is a package: URI if a `dart_package` option is given. | ||
| // Otherwise this is a relative path. | ||
| final Uri protoFileUri; | ||
|
|
||
| final enumGenerators = <EnumGenerator>[]; | ||
|
|
@@ -130,12 +132,22 @@ class FileGenerator extends ProtobufContainer { | |
| /// True if cross-references have been resolved. | ||
| bool _linked = false; | ||
|
|
||
| FileGenerator(this.descriptor, this.options) | ||
| : protoFileUri = Uri.file(descriptor.name) { | ||
| if (protoFileUri.isAbsolute) { | ||
| // protoc should never generate an import with an absolute path. | ||
| throw "FAILURE: Import with absolute path is not supported"; | ||
| static Uri calculateUri(FileDescriptorProto descriptor) { | ||
| if (descriptor.options.hasExtension(Dart_options.dartPackage)) { | ||
| String dartPackage = | ||
| descriptor.options.getExtension(Dart_options.dartPackage); | ||
| return Uri(scheme: 'package', path: '${dartPackage}/${descriptor.name}'); | ||
| } else { | ||
| return Uri.file(descriptor.name); | ||
| } | ||
| } | ||
|
|
||
| FileGenerator(this.descriptor, this.options) | ||
| : protoFileUri = calculateUri(descriptor) { | ||
| // if (protoFileUri.isAbsolute) { | ||
|
||
| // // protoc should never generate an import with an absolute path. | ||
| // throw "FAILURE: Import with absolute path is not supported"; | ||
| // } | ||
|
|
||
| var declaredMixins = _getDeclaredMixins(descriptor); | ||
| var defaultMixinName = | ||
|
|
@@ -194,15 +206,18 @@ class FileGenerator extends ProtobufContainer { | |
| FileGenerator get fileGen => this; | ||
| List<int> get fieldPath => []; | ||
|
|
||
| Uri outputFile(OutputConfiguration config, String extension) { | ||
| Uri protoUrl = Uri.file(descriptor.name); | ||
| return config.outputPathFor(protoUrl, extension); | ||
| } | ||
|
|
||
| /// Generates all the Dart files for this .proto file. | ||
| List<CodeGeneratorResponse_File> generateFiles(OutputConfiguration config) { | ||
| if (!_linked) throw StateError("not linked"); | ||
|
|
||
| makeFile(String extension, String content) { | ||
| Uri protoUrl = Uri.file(descriptor.name); | ||
| Uri dartUrl = config.outputPathFor(protoUrl, extension); | ||
| return CodeGeneratorResponse_File() | ||
| ..name = dartUrl.path | ||
| ..name = outputFile(config, extension).path | ||
| ..content = content; | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Hey @sigurdm I've been having a look at this, and I think this should be:
descriptor.namereturns the full path of the proto file.In my project the proto root is not the same as the package directory, so I get something like:
dartPackage: "foo/bar"
descriptor.name: "bar/baz.proto"
Combining them together gives "foo/bar/bar/baz.proto" when it should be "foo/bar/baz.proto".
I believe it would be better to ignore the path from the descriptor because the dart package name should always point directly to the directory that contains the dart source file. Right?
Uh oh!
There was an error while loading. Please reload this page.
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.
I believe the dart package should not have slashes. It should just be a single name (the name of the package where the proto belongs)
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.
ooh interesting... That would be tricky in my project... I have one directory structure with several dart packages, and they all share a common proto root.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah it seems like an unnecessary restriction that your proto file structure should be the same as your dart package structure. Let me explain my project structure:
It's convenient to have the proto files all in the same directory structure, but the file structure of the Dart packages doesn't match this. I'm trying to ensure my proto files stay platform independent, so I don't really want to rearrange my proto files to match my Dart directory structure. Nor do I really want to have a separate proto root for each package.
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.
I think it makes sense to have one proto-root per package, if the protos truly belongs in that package. (At least the generated files have to be moved into
lib/of that package.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.
OK I'll have a go at rearranging my project / protoc commands. I'll let you know how I get on!