-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial prototype for kroto config gradle dsl
- Loading branch information
1 parent
8c4af77
commit 89ef739
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
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' | ||
} |
50 changes: 50 additions & 0 deletions
50
kroto-plus-gradle-plugin/gen-config-dsl/src/main/kotlin/builderInsertion.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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(""" | ||
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() |
81 changes: 81 additions & 0 deletions
81
kroto-plus-gradle-plugin/gen-config-dsl/src/main/kotlin/varArgExtensionGenerator.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import com.github.marcoferrer.krotoplus.config.CompilerConfig | ||
import com.github.marcoferrer.krotoplus.generators.Generator | ||
import com.github.marcoferrer.krotoplus.generators.ProtoBuildersGenerator | ||
import com.github.marcoferrer.krotoplus.proto.ProtoEnum | ||
import com.github.marcoferrer.krotoplus.proto.ProtoMessage | ||
import com.github.marcoferrer.krotoplus.utils.addFile | ||
import com.github.marcoferrer.krotoplus.utils.addFunctions | ||
import com.github.marcoferrer.krotoplus.utils.memoize | ||
import com.google.protobuf.ByteString | ||
import com.google.protobuf.DescriptorProtos | ||
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED | ||
import com.google.protobuf.compiler.PluginProtos | ||
import com.squareup.kotlinpoet.* | ||
|
||
object VarArgExtensionGenerator : Generator { | ||
|
||
private val gradleActionClassName = ClassName("org.gradle.api", "Action") | ||
|
||
override val isEnabled: Boolean | ||
get() = true | ||
|
||
override fun invoke(): PluginProtos.CodeGeneratorResponse { | ||
val configMessage = context.schema.protoTypes.values | ||
.find { it.name == "CompilerConfig" } as ProtoMessage | ||
|
||
val funSpecs = configMessage.descriptorProto.fieldList.asSequence() | ||
.filter { it.type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE } | ||
.map { | ||
it to requireNotNull(context.schema.protoTypes[it.typeName] as? ProtoMessage) { | ||
"${it.typeName} was not found in schema type map." | ||
} | ||
} | ||
.filterNot { it.second.isMapEntry } | ||
.map { (fieldDescriptorProto, protoMessageForField) -> | ||
|
||
val fieldNameCamelCase = camelCaseFieldName(fieldDescriptorProto.name) | ||
val statementTemplate = "builder.add%N(%T.newBuilder().also{ block.execute(it) }.build())" | ||
|
||
val funSpecBuilder = FunSpec.builder(fieldNameCamelCase.decapitalize()) | ||
.addStatement(statementTemplate, fieldNameCamelCase, protoMessageForField.className) | ||
|
||
funSpecBuilder | ||
.addParameter( | ||
"block", ParameterizedTypeName | ||
.get(gradleActionClassName, protoMessageForField.builderClassName) | ||
|
||
) | ||
.returns(UNIT) | ||
.build() | ||
}.toList() | ||
|
||
val fileSpec = FileSpec.builder(configMessage.javaPackage.orEmpty(), "CompilerConfigDsl") | ||
.addType( | ||
TypeSpec.classBuilder("KrotoPlusConfigurator") | ||
.addProperty( | ||
PropertySpec.builder("builder", configMessage.builderClassName) | ||
.addModifiers(KModifier.PRIVATE) | ||
.initializer("%T.newBuilder()", configMessage.className) | ||
.build() | ||
) | ||
.addFunctions(funSpecs) | ||
.build() | ||
) | ||
|
||
// val typeSpec = | ||
|
||
return PluginProtos.CodeGeneratorResponse.newBuilder() | ||
.addFile(fileSpec.build().toResponseFileProto()) | ||
.build() | ||
} | ||
|
||
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() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
generator_scripts { | ||
script_path: "varArgExtensionGenerator.kts" | ||
script_bundle: "gen-config-dsl/build/libs/bundle.jar" | ||
} | ||
insertions { | ||
filter { | ||
include_path: "krotoplus/*" | ||
} | ||
entry { point: BUILDER_SCOPE | ||
script_path: "builderInsertion.kts" | ||
script_bundle: "gen-config-dsl/build/libs/bundle.jar" | ||
} | ||
} |