OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of protoc; |
| 6 |
| 7 /// Configures where output of the protoc compiler should be placed and how to |
| 8 /// import one generated file from another. |
| 9 abstract class OutputConfiguration { |
| 10 |
| 11 /// Returns [filePath] with it's extension replaced with '.pb.dart'. |
| 12 String replacePathExtension(String filePath) => |
| 13 '${path.withoutExtension(filePath)}.pb.dart'; |
| 14 |
| 15 /// Returns [file] with it's extension replaced with '.pb.dart'. |
| 16 Uri replaceUriExtension(Uri file) => |
| 17 path.url.toUri(replacePathExtension(path.url.fromUri(file))); |
| 18 |
| 19 /// Resolves an import URI. Both [source] and [target] are .proto files, |
| 20 /// where [target] is imported from [source]. The result URI can be used to |
| 21 /// import [target]'s .pb.dart output from [source]'s .pb.dart output. |
| 22 Uri resolveImport(Uri target, Uri source); |
| 23 |
| 24 /// Returns the path, under the output folder, where the code will be |
| 25 /// generated for [inputPath]. The input is expected to be a .proto file, |
| 26 /// while the output is expected to be a .pb.dart file. |
| 27 Uri outputPathFor(Uri inputPath); |
| 28 } |
| 29 |
| 30 /// Default [OutputConfiguration] that uses the same path as the input |
| 31 /// file for the output file (just replaces the extension), and that uses |
| 32 /// relative paths to resolve imports. |
| 33 class DefaultOutputConfiguration extends OutputConfiguration { |
| 34 |
| 35 Uri outputPathFor(Uri input) => replaceUriExtension(input); |
| 36 |
| 37 Uri resolveImport(Uri target, Uri source) { |
| 38 var builder = path.url; |
| 39 var targetPath = builder.fromUri(target); |
| 40 var sourceDir = builder.dirname(builder.fromUri(source)); |
| 41 return builder.toUri(replacePathExtension( |
| 42 builder.relative(targetPath, from: sourceDir))); |
| 43 } |
| 44 } |
OLD | NEW |