Skip to content

Commit

Permalink
add kroto configuration gradle dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoferrer committed Dec 4, 2019
1 parent 3f42efb commit 198b8b9
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
- ./gradlew assemble
script:
- ./gradlew check
- cd example-project && ./gradlew test
- cd example-project && ./gradlew test && ./gradlew clean test -PuseKrotoConfigDsl=true
after_success:
- bash <(curl -s https://codecov.io/bash)
122 changes: 108 additions & 14 deletions example-project/build.gradle
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
buildscript {
ext {
versions = [
"protobuf": '3.9.0',
"grpc": '1.23.0',
"kotlin": '1.3.50',
"protobuf" : '3.9.0',
"grpc" : '1.23.0',
"kotlin" : '1.3.50',
"coroutines": '1.3.0',
"krotoplus": '0.6.0-SNAPSHOT'
"krotoplus" : '0.6.0-SNAPSHOT'
]
}

repositories {
jcenter()
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
flatDir dirs: "${rootDir}/../kroto-plus-gradle-plugin/build/libs/"
}

dependencies {
classpath "com.github.marcoferrer.krotoplus:kroto-plus-gradle-plugin:${versions.krotoplus}"
classpath "com.google.protobuf:protobuf-java:${versions.protobuf}"
}
}

plugins{
plugins {
id 'idea'
id 'com.google.protobuf' version '0.8.7'
id "org.jetbrains.kotlin.jvm" version "1.3.50"
}

apply plugin: "com.github.marcoferrer.kroto-plus"

group = 'com.github.marcoferrer.krotoplus'
version = versions.krotoplus

compileKotlin {
kotlinOptions{
kotlinOptions {
jvmTarget = "1.8"
}
}

compileTestKotlin {
kotlinOptions{
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xuse-experimental=kotlin.Experimental"]
}
Expand Down Expand Up @@ -62,8 +70,8 @@ dependencies {
implementation "com.github.marcoferrer.krotoplus:kroto-plus-message:${versions.krotoplus}"

implementation "io.grpc:grpc-protobuf:${versions.grpc}",
"io.grpc:grpc-stub:${versions.grpc}",
"io.grpc:grpc-netty:${versions.grpc}"
"io.grpc:grpc-stub:${versions.grpc}",
"io.grpc:grpc-netty:${versions.grpc}"

testImplementation "io.grpc:grpc-testing:${versions.grpc}"

Expand All @@ -80,8 +88,87 @@ idea {
}
}

// Experimental config DSL example
krotoPlus {
config {
//noinspection GroovyAssignabilityCheck
main {

mockServices {
filter {
addIncludePath("jojo/*")
}
implementAsObject = true
generateServiceList = true
serviceListPackage = "jojo.bizarre.adventure"
serviceListName = "MockJojoServices"
}

mockServices {
filter {
addIncludePath("test/message/*")
}
implementAsObject = true
generateServiceList = true
serviceListPackage = "test.message"
}

protoBuilders {
filter {
addExcludePath("google/*")
}
unwrapBuilders = true
useDslMarkers = true
}

grpcStubExts {
supportCoroutines = true
}
grpcCoroutines()
extendableMessages {
filter {
addIncludePath("jojo/bizarre/adventure/stand/*")
}
}
generatorScripts {
addScriptPath("helloThere.kts")
scriptBundle = file("kp-scripts/build/libs/kp-scripts.jar")
}
generatorScripts {
addScriptPath("varArgExtensionGenerator.kts")
scriptBundle = file("kp-scripts/build/libs/kp-scripts.jar")
}
insertions {
filter {
addIncludePath("jojo/bizarre/adventure/character/*")
}
entry {
point = 'MESSAGE_IMPLEMENTS'
addScriptPath "extendableMessages.kts"
scriptBundle = file("kp-scripts/build/libs/kp-scripts.jar")
}
entry {
point = 'BUILDER_IMPLEMENTS'
addScriptPath "extendableMessages.kts"
scriptBundle = file("kp-scripts/build/libs/kp-scripts.jar")
}
entry {
point = 'CLASS_SCOPE'
addScriptPath "extendableMessages.kts"
scriptBundle = file("kp-scripts/build/libs/kp-scripts.jar")
}
entry {
point = 'OUTER_CLASS_SCOPE'
addScriptPath(file("kp-scripts/src/main/kotlin/sampleInsertionScript.kts").toString())
}
}
}
}

}

protobuf {
protoc { artifact = "com.google.protobuf:protoc:${versions.protobuf}"}
protoc { artifact = "com.google.protobuf:protoc:${versions.protobuf}" }

generatedFilesBaseDir = "$buildDir/generated-sources"

Expand All @@ -96,11 +183,11 @@ protobuf {
generateProtoTasks {
def krotoConfig = file("krotoPlusConfig.asciipb")

all().each{ task ->
all().each { task ->
// Adding the config file to the task inputs lets UP-TO-DATE checks
// include changes to configuration
task.inputs.files krotoConfig

// If we're using precompiled scripts for improved performance then we
// should depend on the jar task for our scripts
task.dependsOn ':kp-scripts:jar'
Expand All @@ -112,9 +199,16 @@ protobuf {
// to be the same as 'task.builtins.java.outputSubDir' since
// it relies on the insertion_point api from protoc.
outputSubDir = "java"
option "ConfigPath=$krotoConfig"

if (project.findProperty("useKrotoConfigDsl")?.toBoolean() ?: false) {
option krotoPlus.config.main.asOption()
} else {
option "ConfigPath=$krotoConfig"
}

}
}
}
}
}

}
26 changes: 18 additions & 8 deletions kroto-plus-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ apply from: "$rootDir/publishing.gradle"

def pluginId = 'com.github.marcoferrer.kroto-plus'

repositories {
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://repo.gradle.org/gradle/libs-releases-local/" }
google()
// Needed for resolving 'kotlinx-metadata-jvm'
// A transitive dependency of gradle Kotlin DSL
maven { url "https://kotlin.bintray.com/kotlinx/" }
}

gradlePlugin {
plugins {
krotoPlusPlugin {
Expand All @@ -17,6 +26,11 @@ gradlePlugin {

dependencies{
implementation "com.google.protobuf:protobuf-java:${Versions.protobuf}"
compileOnly gradleApi()
compileOnly "org.gradle:gradle-kotlin-dsl:1.0.4"
compileOnly "com.google.protobuf:protobuf-gradle-plugin:0.8.8"
compileOnly 'com.google.gradle:osdetector-gradle-plugin:1.6.2'
compileOnly project(":protoc-gen-kroto-plus")
protobuf project(":protoc-gen-kroto-plus")
}

Expand Down Expand Up @@ -50,14 +64,10 @@ protobuf {
generateProtoTasks {
def krotoConfig = file("krotoPlusConfig.asciipb")

all().each { task ->
task.inputs.files krotoConfig
task.dependsOn ':protoc-gen-kroto-plus:buildCanteenArtifacts'
task.plugins {
kroto {
outputSubDir = "java"
}
}
all().each{ task ->
task.dependsOn ':kroto-plus-gradle-plugin:gen-config-dsl:jar'
configProtoTaskWithKroto(task, krotoConfig)
}
}

}
28 changes: 28 additions & 0 deletions kroto-plus-gradle-plugin/gen-config-dsl/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Kroto+ Contributors
*
* 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.
*/

dependencies{
implementation project(":protoc-gen-kroto-plus")
implementation "com.google.protobuf:protobuf-java:3.6.1"
implementation "org.jetbrains.kotlin:kotlin-script-util"
implementation ("com.squareup:kotlinpoet:0.7.0") {
exclude group: 'org.jetbrains.kotlin'
}
}

jar {
archiveName = 'bundle.jar'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2019 Kroto+ Contributors
*
* 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.
*/

import com.github.marcoferrer.krotoplus.proto.ProtoMessage
import com.github.marcoferrer.krotoplus.utils.memoize
import com.google.protobuf.DescriptorProtos

// language=java
fun builderScope(message: ProtoMessage): String? = buildString {
val schema = message.protoFile.schema

message.descriptorProto.fieldList.asSequence()
.filter { it.type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE }
.map {
it to requireNotNull(schema.protoTypes[it.typeName] as? ProtoMessage) {
"${it.typeName} was not found in schema type map."
}
}
.filterNot { it.second.isMapEntry }
.forEach { (fieldDescriptorProto, protoMessageForField) ->

val fieldNameCamelCase = camelCaseFieldName(fieldDescriptorProto.name)

val addStatement= if (fieldDescriptorProto.label == DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED)
"add$fieldNameCamelCase(builder)" else "set$fieldNameCamelCase(builder)"

append("""
/**GRADLE DSL INSERTION**/
public void ${fieldNameCamelCase.decapitalize()}(){
${protoMessageForField.builderClassName.canonicalName} builder = ${protoMessageForField.className.canonicalName}.newBuilder();
$addStatement;
}
public void ${fieldNameCamelCase.decapitalize()}( org.gradle.api.Action<${protoMessageForField.builderClassName.canonicalName}> action){
${protoMessageForField.builderClassName.canonicalName} builder = ${protoMessageForField.className.canonicalName}.newBuilder();
action.execute(builder);
$addStatement;
}
public void ${fieldNameCamelCase.decapitalize()}( groovy.lang.Closure<${protoMessageForField.builderClassName.canonicalName}> closure){
${protoMessageForField.builderClassName.canonicalName} builder = ${protoMessageForField.className.canonicalName}.newBuilder();
org.gradle.util.ConfigureUtil.configure(closure,builder);
$addStatement;
}
""".trimIndent()
)
appendln()
}
}




val camelCaseFieldName = { it: String ->
// We cant use CaseFormat.UPPER_CAMEL since
// protoc is lenient with malformed field names
if (it.contains("_"))
it.split("_").joinToString(separator = "") { it.capitalize() } else
it.capitalize()

}.memoize()
10 changes: 10 additions & 0 deletions kroto-plus-gradle-plugin/krotoPlusConfig.asciipb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
insertions {
filter {
include_path: "krotoplus/*"
}
entry {
point: BUILDER_SCOPE
script_path: "gradleDsl.kts"
script_bundle: "gen-config-dsl/build/libs/bundle.jar"
}
}
Loading

0 comments on commit 198b8b9

Please sign in to comment.