Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Unified Diff: lib/file_generator.dart

Issue 269823003: Parameterize uri resolution and parsing of options, use package:path (Closed) Base URL: git@github.com:dart-lang/dart-protoc-plugin.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/file_generator.dart
diff --git a/lib/file_generator.dart b/lib/file_generator.dart
index 982a8131dfee9acd3c87c0f1f779f461327537c7..35802d506c0cdc1cb7b07d5ecfac082018cd4197 100644
--- a/lib/file_generator.dart
+++ b/lib/file_generator.dart
@@ -39,13 +39,6 @@ class FileGenerator extends ProtobufContainer {
return index == -1 ? fileName : fileName.substring(0, index);
}
- // Create the URI for the generated Dart file from the URI of the
- // .proto file.
- Uri _generatedFilePath(Uri protoFilePath) {
- var dartFileName = _fileNameWithoutExtension(protoFilePath) + ".pb.dart";
- return protoFilePath.resolve(dartFileName);
- }
-
String _generateClassName(Uri protoFilePath) {
String s = _fileNameWithoutExtension(protoFilePath).replaceAll('-', '_');
return '${s[0].toUpperCase()}${s.substring(1)}';
@@ -56,49 +49,15 @@ class FileGenerator extends ProtobufContainer {
return _fileNameWithoutExtension(protoFilePath).replaceAll('-', '_');
}
- Uri _relative(Uri target, Uri base) {
- // Ignore the last segment of the base.
- List<String> baseSegments =
- base.pathSegments.sublist(0, base.pathSegments.length - 1);
- List<String> targetSegments = target.pathSegments;
- if (baseSegments.length == 1 && baseSegments[0] == '.') {
- baseSegments = [];
- }
- if (targetSegments.length == 1 && targetSegments[0] == '.') {
- targetSegments = [];
- }
- int common = 0;
- int length = min(targetSegments.length, baseSegments.length);
- while (common < length && targetSegments[common] == baseSegments[common]) {
- common++;
- }
-
- final segments = <String>[];
- if (common < baseSegments.length && baseSegments[common] == '..') {
- throw new ArgumentError(
- "Cannot create a relative path from $base to $target");
- }
- for (int i = common; i < baseSegments.length; i++) {
- segments.add('..');
- }
- for (int i = common; i < targetSegments.length; i++) {
- segments.add('${targetSegments[i]}');
- }
- if (segments.isEmpty) {
- segments.add('.');
- }
- return new Uri(pathSegments: segments);
- }
-
CodeGeneratorResponse_File generateResponse() {
MemoryWriter writer = new MemoryWriter();
IndentingWriter out = new IndentingWriter(' ', writer);
generate(out);
- Uri filePath = new Uri(scheme: 'file', path: _fileDescriptor.name);
+ Uri filePath = new Uri.file(_fileDescriptor.name);
return new CodeGeneratorResponse_File()
- ..name = _generatedFilePath(filePath).path
+ ..name = _context.outputConfiguration.outputPathFor(filePath).path
..content = writer.toString();
}
@@ -127,12 +86,13 @@ class FileGenerator extends ProtobufContainer {
// protoc should never generate an import with an absolute path.
throw("FAILURE: Import with absolute path is not supported");
}
- // Create a relative path from the current file to the import.
- Uri relativeProtoPath = _relative(importPath, filePath);
+ // Create a path from the current file to the imported proto.
+ Uri resolvedImport = _context.outputConfiguration.resolveImport(
+ importPath, filePath);
// Find the file generator for this import as it contains the
// package name.
FileGenerator fileGenerator = _context.lookupFile(import);
- out.print("import '${_generatedFilePath(relativeProtoPath)}'");
+ out.print("import '$resolvedImport'");
if (package != fileGenerator.package && !fileGenerator.package.isEmpty) {
out.print(' as ${fileGenerator.packageImportPrefix}');
}
@@ -175,12 +135,13 @@ class FileGenerator extends ProtobufContainer {
class GenerationContext {
final GenerationOptions options;
+ final OutputConfiguration outputConfiguration;
final Map<String, ProtobufContainer> _registry =
<String, ProtobufContainer>{};
final Map<String, FileGenerator> _files =
<String, FileGenerator>{};
- GenerationContext(this.options);
+ GenerationContext(this.options, this.outputConfiguration);
void register(ProtobufContainer container) {
_registry[container.fqname] = container;
« no previous file with comments | « lib/code_generator.dart ('k') | lib/options.dart » ('j') | lib/protobuf_field.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698