Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2021 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.lightbend.akkasls.codegen

trait Log {
def debug(message: String): Unit
def info(message: String): Unit
def warning(message: String): Unit
def error(message: String): Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ object ModelBuilder {
*/
def introspectProtobufClasses(
descriptors: Iterable[Descriptors.FileDescriptor]
): Model =
)(implicit log: Log): Model =
descriptors.foldLeft(Model(Map.empty, Map.empty)) {
case (Model(existingServices, existingEntities), descriptor) =>
log.debug("Looking at descriptor " + descriptor.getName)
val services = for {
serviceDescriptor <- descriptor.getServices.asScala
options = serviceDescriptor
Expand Down Expand Up @@ -271,11 +272,12 @@ object ModelBuilder {
*/
private def extractValueEntityDefinition(
descriptor: Descriptors.FileDescriptor
): Option[ValueEntity] = {
)(implicit log: Log): Option[ValueEntity] = {
val rawEntity =
descriptor.getOptions
.getExtension(com.akkaserverless.Annotations.file)
.getValueEntity
log.debug("Raw value entity name: " + rawEntity.getName)

val protoReference = PackageNaming.from(descriptor)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ import java.nio.file.Paths
import scala.jdk.CollectionConverters._
import scala.util.Using
import com.google.protobuf.ExtensionRegistry
import org.slf4j.LoggerFactory

import scala.collection.mutable

class ModelBuilderSuite extends munit.FunSuite {
val log = LoggerFactory.getLogger(getClass)
implicit val codegenLog = new Log {
override def debug(message: String): Unit = log.debug(message)
override def info(message: String): Unit = log.info(message)
override def warning(message: String): Unit = log.warn(message)
override def error(message: String): Unit = log.error(message)
}

test("EventSourcedEntity introspection") {
val testFilesPath = Paths.get(getClass.getClassLoader.getResource("test-files").toURI)
val descriptorFilePath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,32 @@ object SourceGenerator extends PrettyPrinter {
integrationTestSourceDirectory: Path,
generatedSourceDirectory: Path,
mainClass: String
): Iterable[Path] = {
)(implicit log: Log): Iterable[Path] = {

val (mainClassPackageName, mainClassName) = disassembleClassName(mainClass)

model.services.values.flatMap {
case service: ModelBuilder.EntityService =>
model.entities
.get(service.componentFullName)
.toSeq
.flatMap(
entity =>
EntityServiceSourceGenerator.generate(
entity,
service,
sourceDirectory,
testSourceDirectory,
integrationTestSourceDirectory,
generatedSourceDirectory,
mainClassPackageName,
mainClassName
)
)
model.entities.get(service.componentFullName) match {
case None =>
// TODO perhaps we even want to make this an error, to really go all-in on codegen?
log.warning(
"Service [" + service.fqn.fullName + "] refers to entity [" + service.componentFullName +
"], but no entity configuration is found for that component name"
)
Seq.empty
case Some(entity) =>
EntityServiceSourceGenerator.generate(
entity,
service,
sourceDirectory,
testSourceDirectory,
integrationTestSourceDirectory,
generatedSourceDirectory,
mainClassPackageName,
mainClassName
)
}
case service: ModelBuilder.ViewService if service.transformedUpdates.nonEmpty =>
ViewServiceSourceGenerator.generate(
service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ package com.lightbend.akkasls.codegen
package java

import org.apache.commons.io.FileUtils
import org.slf4j.LoggerFactory

import _root_.java.nio.file.Files

class SourceGeneratorSuite extends munit.FunSuite {
val log = LoggerFactory.getLogger(getClass)
implicit val codegenLog = new Log {
override def debug(message: String): Unit = log.debug(message)
override def info(message: String): Unit = log.info(message)
override def warning(message: String): Unit = log.warn(message)
override def error(message: String): Unit = log.error(message)
}

test("generate") {
val sourceDirectory = Files.createTempDirectory("source-generator-test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -16,6 +15,7 @@
import java.io.File;
import java.nio.file.Path;

import com.lightbend.akkasls.codegen.Log;
import com.lightbend.akkasls.codegen.ModelBuilder;
import scala.collection.Iterable;
import scala.util.Either;
Expand Down Expand Up @@ -71,7 +71,16 @@ public class GenerateMojo extends AbstractMojo {
@Parameter(defaultValue = ".*ServiceEntity", property = "serviceNamesFilter", required = true)
private String serviceNamesFilter;

private final Log log = getLog();
private final Log log = new Log() {
@Override
public void debug(String message) { getLog().debug(message); }
@Override
public void info(String message) { getLog().info(message); }
@Override
public void warning(String message) { getLog().warn(message); }
@Override
public void error(String message) { getLog().error(message); }
};

/**
* Given a protobuf descriptor, we inspect it and search for entities, commands,
Expand All @@ -85,7 +94,6 @@ public void execute() throws MojoExecutionException {
Either<DescriptorSet.CannotOpen, Iterable<Either<DescriptorSet.ReadFailure, Descriptors.FileDescriptor>>> descriptors = DescriptorSet
.fileDescriptors(protobufDescriptor);
if (descriptors.isRight()) {

Iterable<FileDescriptor> fileDescriptors = descriptors.right().get().map(descriptor -> {
if (descriptor.isRight()) {
return descriptor.right().get();
Expand All @@ -95,9 +103,16 @@ public void execute() throws MojoExecutionException {
+ descriptor.left().get().toString()));
}
});
ModelBuilder.Model model = ModelBuilder.introspectProtobufClasses(fileDescriptors);
Iterable<Path> generated = SourceGenerator.generate(model, sourceDirectory.toPath(),
testSourceDirectory.toPath(), integrationTestSourceDirectory.toPath(), generatedSourceDirectory.toPath(), mainClass);
ModelBuilder.Model model = ModelBuilder.introspectProtobufClasses(fileDescriptors, log);
log.debug("Model: " + model);
Iterable<Path> generated = SourceGenerator.generate(
model,
sourceDirectory.toPath(),
testSourceDirectory.toPath(),
integrationTestSourceDirectory.toPath(),
generatedSourceDirectory.toPath(),
mainClass,
log);
Path absBaseDir = baseDir.toPath().toAbsolutePath();
generated.foreach(p -> {
log.info("Generated: " + absBaseDir.relativize(p.toAbsolutePath()));
Expand Down
6 changes: 4 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ object Dependencies {

val codegenCore = deps ++= Seq(
protobufJava,
logback % Test,
munit % Test,
munitScalaCheck % Test
)

val codegenJava = deps ++= Seq(
kiama,
commonsIo,
munit % "test",
munitScalaCheck % "test"
logback % Test,
munit % Test,
munitScalaCheck % Test
)

val excludeTheseDependencies: Seq[ExclusionRule] = Seq(
Expand Down
20 changes: 18 additions & 2 deletions publishLocalM2.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# publish sbt and maven artifacts
sbt publishM2
#!/bin/bash

set -e

ONLY_MAVEN=0

for arg in "$@"; do case $arg in
-o|--only-maven)
ONLY_MAVEN=1
shift
;;
esac
done

if [ $ONLY_MAVEN == "0" ]; then
sbt publishM2
fi

SDK_VERSION=$(sbt "print sdk/version" | tail -1)
cd maven-java
mvn versions:set -DnewVersion=$SDK_VERSION
Expand Down