Use protobuf with renamed packages, pack to IDEA plugin

Update GenerateProtoBuf.kt to also regexp-replace com.google.protobuf with
org.jetbrains.kotlin.protobuf in generated Java sources

 #KT-12581 Fixed
This commit is contained in:
Alexander Udalov
2016-06-07 19:41:51 +03:00
parent 952a85414b
commit 7e38b93d80
44 changed files with 3356 additions and 3317 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.generators.protobuf
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.util.ExecUtil
import com.intellij.util.LineSeparator
import java.io.File
import java.util.regex.Pattern
@@ -104,6 +105,32 @@ private fun execProtoc(protoPath: String, outPath: String) {
if (processOutput.stderr.isNotEmpty()) {
throw AssertionError(processOutput.stderr)
}
renamePackages(protoPath, outPath)
}
private fun renamePackages(protoPath: String, outPath: String) {
fun List<String>.findValue(regex: Regex): String? =
mapNotNull { line ->
regex.find(line)?.groupValues?.get(1)
}.singleOrNull()
val protoFileContents = File(protoPath).readLines()
val packageName = protoFileContents.findValue("package ([\\w.]+);".toRegex())
?: error("No package directive found in $protoPath")
val className = protoFileContents.findValue("option java_outer_classname = \"(\\w+)\";".toRegex())
?: error("No java_outer_classname option found in $protoPath")
val javaFile = File(outPath, "${packageName.replace('.', '/')}/$className.java")
if (!javaFile.exists()) {
throw AssertionError("File does not exist: $javaFile")
}
javaFile.writeText(
javaFile.readLines().map { line ->
line.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf")
}.joinToString(LineSeparator.getSystemLineSeparator().separatorString)
)
}
private fun modifyAndExecProtoc(protoPath: ProtoPath) {
@@ -16,8 +16,8 @@
package org.jetbrains.kotlin.generators.protobuf
import com.google.protobuf.Descriptors
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil
import org.jetbrains.kotlin.protobuf.Descriptors
import org.jetbrains.kotlin.serialization.DebugExtOptionsProtoBuf
import org.jetbrains.kotlin.serialization.DebugProtoBuf
import org.jetbrains.kotlin.serialization.jvm.DebugJvmProtoBuf
@@ -39,7 +39,7 @@ class GenerateProtoBufCompare {
}
}
private val JAVA_TYPES_WITH_INLINED_EQUALS: EnumSet<com.google.protobuf.Descriptors.FieldDescriptor.JavaType> = EnumSet.of(
private val JAVA_TYPES_WITH_INLINED_EQUALS: EnumSet<Descriptors.FieldDescriptor.JavaType> = EnumSet.of(
Descriptors.FieldDescriptor.JavaType.INT,
Descriptors.FieldDescriptor.JavaType.LONG,
Descriptors.FieldDescriptor.JavaType.FLOAT,
@@ -16,13 +16,12 @@
package org.jetbrains.kotlin.generators.protobuf
import junit.framework.TestCase
import com.google.protobuf.GeneratedMessage.GeneratedExtension
import java.lang.reflect.ParameterizedType
import com.google.protobuf.Descriptors
import kotlin.test.fail
import com.google.common.collect.LinkedHashMultimap
import junit.framework.TestCase
import org.jetbrains.kotlin.protobuf.Descriptors
import org.jetbrains.kotlin.protobuf.GeneratedMessage.GeneratedExtension
import java.lang.reflect.Modifier
import java.lang.reflect.ParameterizedType
class ProtoBufConsistencyTest : TestCase() {
fun testExtensionNumbersDoNotIntersect() {