|
| 1 | +/* |
| 2 | + * Copyright 2021 Lightbend Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.akkaserverless.codegen.scalasdk |
| 18 | + |
| 19 | +import com.google.protobuf.ExtensionRegistry |
| 20 | +import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse |
| 21 | +import com.lightbend.akkasls.codegen.ModelBuilder |
| 22 | +import protocbridge.Artifact |
| 23 | +import protocgen.{ CodeGenApp, CodeGenRequest, CodeGenResponse } |
| 24 | + |
| 25 | +object AkkaserverlessTestGenerator extends CodeGenApp { |
| 26 | + val enableDebug = "enableDebug" |
| 27 | + |
| 28 | + override def registerExtensions(registry: ExtensionRegistry): Unit = { |
| 29 | + registry.add(com.akkaserverless.Annotations.service) |
| 30 | + registry.add(com.akkaserverless.Annotations.file) |
| 31 | + } |
| 32 | + |
| 33 | + override def process(request: CodeGenRequest): CodeGenResponse = { |
| 34 | + val debugEnabled = request.parameter.contains(enableDebug) |
| 35 | + val model = ModelBuilder.introspectProtobufClasses(request.filesToGenerate)(FatalWarningsLog(debugEnabled)) |
| 36 | + try { |
| 37 | + CodeGenResponse.succeed( |
| 38 | + SourceGenerator |
| 39 | + .generateManagedTest(model) |
| 40 | + .map(file => |
| 41 | + CodeGeneratorResponse.File |
| 42 | + .newBuilder() |
| 43 | + .setName(file.name) |
| 44 | + .setContent(file.content) |
| 45 | + .build())) |
| 46 | + } catch { |
| 47 | + case t: Throwable => |
| 48 | + t.printStackTrace() |
| 49 | + CodeGenResponse.fail(t.getMessage) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // FIXME #382 add reference to the runtime lib here |
| 54 | + override def suggestedDependencies: Seq[Artifact] = Nil |
| 55 | +} |
0 commit comments