private static Scope<Object> buildScopeTree(PluginProtos.CodeGeneratorRequest request) { Scope<Object> root = Scope.newRoot(); List<String> filesToGenerate = request.getFileToGenerateList(); for (FileDescriptorProto fdp : request.getProtoFileList()) { Scope<?> packageScope = fdp.hasPackage() ? root.findOrCreate(fdp.getPackage()) : root; boolean export = filesToGenerate.contains(fdp.getName()); for (ServiceDescriptorProto sdp : fdp.getServiceList()) { addServiceToScope(packageScope, sdp, export); } for (FieldDescriptorProto efdp : fdp.getExtensionList()) { addExtensionToScope(packageScope, efdp, export); } for (EnumDescriptorProto edp : fdp.getEnumTypeList()) { addEnumToScope(packageScope, edp, export); } for (DescriptorProto dp : fdp.getMessageTypeList()) { addMessageToScope(packageScope, dp, export); } } return root; }
public static void main(String[] args) throws IOException { ExtensionRegistry registry = ExtensionRegistry.newInstance(); Options.registerAllExtensions(registry); PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest. parseFrom(System.in, registry); PluginProtos.CodeGeneratorResponse response; try { Scope<Object> root = buildScopeTree(request); PluginProtos.CodeGeneratorResponse.Builder responseBuilder = PluginProtos.CodeGeneratorResponse.newBuilder(); writeFiles(root, responseBuilder); response = responseBuilder.build(); } catch (Exception e) { // 出错,报告给 protoc ,然后退出 StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); PluginProtos.CodeGeneratorResponse.newBuilder().setError(sw.toString()). build().writeTo(System.out); System.out.flush(); return; } response.writeTo(System.out); System.out.flush(); }
@Override public Stream<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request) throws GeneratorException { final ProtoTypeMap typeMap = ProtoTypeMap.of(request.getProtoFileList()); List<FileDescriptorProto> protosToGenerate = request.getProtoFileList().stream() .filter(protoFile -> request.getFileToGenerateList().contains(protoFile.getName())) .collect(Collectors.toList()); List<ServiceContext> services = findServices(protosToGenerate, typeMap); List<PluginProtos.CodeGeneratorResponse.File> files = generateFiles(services); return files.stream(); }
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) { String content = applyTemplate(getClassPrefix() + "Stub.mustache", context); return PluginProtos.CodeGeneratorResponse.File .newBuilder() .setName(absoluteFileName(context)) .setContent(content) .build(); }
@Override public Stream<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request) throws GeneratorException { final ProtoTypeMap protoTypeMap = ProtoTypeMap.of(request.getProtoFileList()); return request.getProtoFileList().stream() .filter(protoFile -> request.getFileToGenerateList().contains(protoFile.getName())) .flatMap(f -> extractContext(protoTypeMap, f)) .map(this::buildFile); }
private PluginProtos.CodeGeneratorResponse.File buildFile(Context context) { String content = applyTemplate("Jdk8Stub.mustache", context); return PluginProtos.CodeGeneratorResponse.File .newBuilder() .setName(absoluteFileName(context)) .setContent(content) .build(); }
private static void writeFiles(Scope<?> root, PluginProtos.CodeGeneratorResponse.Builder responseBuilder) { StringBuilder initializerContent = new StringBuilder(); initializerContent.append("{\n"); writeFiles(root, responseBuilder, initializerContent); initializerContent.append("}\n"); responseBuilder.addFile( PluginProtos.CodeGeneratorResponse.File.newBuilder(). setName("initializer.as.inc"). setContent(initializerContent.toString()). build() ); }
private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) { return services.stream() .map(this::buildFile) .collect(Collectors.toList()); }
@SuppressWarnings("unchecked") private static void writeFiles(Scope<?> root, PluginProtos.CodeGeneratorResponse.Builder responseBuilder, StringBuilder initializerContent) { for (Map.Entry<String, Scope<?>> entry : root.children.entrySet()) { Scope<?> scope = entry.getValue(); if (scope.export) { if (scope.proto instanceof ServiceDescriptorProto) { ServiceDescriptorProto serviceProto = (ServiceDescriptorProto)scope.proto; if (serviceProto.getOptions().getExtension(Options.as3ClientSideService) || serviceProto.getOptions().getExtension(Options.as3ServerSideService)) { StringBuilder classContent = new StringBuilder(); writeServiceClass((Scope<ServiceDescriptorProto>)scope, classContent); responseBuilder.addFile( PluginProtos.CodeGeneratorResponse.File.newBuilder(). setName(scope.fullName.replace('.', '/') + ".as"). setContent(classContent.toString()). build() ); StringBuilder interfaceContent = new StringBuilder(); writeServiceInterface((Scope<ServiceDescriptorProto>)scope, interfaceContent); String[] servicePath = scope.fullName.split("\\."); StringBuilder sb = new StringBuilder(); int i = 0; for (; i < servicePath.length - 1; i++) { sb.append(servicePath[i]); sb.append('/'); } sb.append('I'); sb.append(servicePath[i]); sb.append(".as"); responseBuilder.addFile( PluginProtos.CodeGeneratorResponse.File.newBuilder(). setName(sb.toString()). setContent(interfaceContent.toString()). build() ); } } else { StringBuilder content = new StringBuilder(); writeFile(scope, content, initializerContent); responseBuilder.addFile( PluginProtos.CodeGeneratorResponse.File.newBuilder(). setName(scope.fullName.replace('.', '/') + ".as"). setContent(content.toString()). build() ); } } writeFiles(scope, responseBuilder, initializerContent); } }
/** * Processes a generator request into a set of files to output. * @param request The raw generator request from protoc. * @return The completed files to write out. */ public abstract Stream<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request) throws GeneratorException;