GenerateProtoBuf: support java_multiple_files mode
This commit is contained in:
@@ -66,7 +66,7 @@ val PROTO_PATHS: List<ProtoPath> = listOf(
|
|||||||
private val EXT_OPTIONS_PROTO_PATH = ProtoPath("core/metadata/src/ext_options.proto")
|
private val EXT_OPTIONS_PROTO_PATH = ProtoPath("core/metadata/src/ext_options.proto")
|
||||||
private val PROTOBUF_PROTO_PATHS = listOf("./", "core/metadata/src")
|
private val PROTOBUF_PROTO_PATHS = listOf("./", "core/metadata/src")
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main() {
|
||||||
try {
|
try {
|
||||||
checkVersion()
|
checkVersion()
|
||||||
|
|
||||||
@@ -80,12 +80,10 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
println()
|
println()
|
||||||
println("Do not forget to run GenerateProtoBufCompare")
|
println("Do not forget to run GenerateProtoBufCompare")
|
||||||
}
|
} catch (e: Throwable) {
|
||||||
catch (e: Throwable) {
|
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
throw e
|
throw e
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
// Workaround for JVM hanging: IDEA's process handler creates thread pool
|
// Workaround for JVM hanging: IDEA's process handler creates thread pool
|
||||||
exitProcess(0)
|
exitProcess(0)
|
||||||
}
|
}
|
||||||
@@ -128,17 +126,34 @@ private fun renamePackages(protoPath: String, outPath: String) {
|
|||||||
val className = protoFileContents.findValue("option java_outer_classname = \"(\\w+)\";".toRegex())
|
val className = protoFileContents.findValue("option java_outer_classname = \"(\\w+)\";".toRegex())
|
||||||
?: error("No java_outer_classname option found in $protoPath")
|
?: error("No java_outer_classname option found in $protoPath")
|
||||||
|
|
||||||
val javaFile = File(outPath, "${packageName.replace('.', '/')}/$className.java")
|
val javaMultipleFiles = protoFileContents.findValue("option java_multiple_files = (\\w+);".toRegex()) == "true"
|
||||||
|
|
||||||
|
if (javaMultipleFiles) {
|
||||||
|
val packageDirectory = File(outPath, packageName.replace('.', '/'))
|
||||||
|
if (!packageDirectory.exists() || !packageDirectory.isDirectory) {
|
||||||
|
throw AssertionError("$protoPath, java_multiple_files mode: '$packageDirectory' doesn't exist or is not a directory")
|
||||||
|
}
|
||||||
|
val javaFiles = packageDirectory.listFiles { f: File -> f.extension == "java" }
|
||||||
|
?: throw AssertionError("$protoPath, java_multiple_files mode: Can't list directory contents of '$packageDirectory'")
|
||||||
|
for (javaFile in javaFiles) {
|
||||||
|
renamePackagesInSingleFile(javaFile)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
renamePackagesInSingleFile(File(outPath, "${packageName.replace('.', '/')}/$className.java"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun renamePackagesInSingleFile(javaFile: File) {
|
||||||
if (!javaFile.exists()) {
|
if (!javaFile.exists()) {
|
||||||
throw AssertionError("File does not exist: $javaFile")
|
throw AssertionError("File does not exist: $javaFile")
|
||||||
}
|
}
|
||||||
|
|
||||||
javaFile.writeText(
|
javaFile.writeText(
|
||||||
javaFile.readLines().map { line ->
|
javaFile.readLines().joinToString(LineSeparator.getSystemLineSeparator().separatorString) { line ->
|
||||||
line.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf")
|
line.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf")
|
||||||
// Memory footprint optimizations: do not allocate too big bytes buffers that effectively remain unused
|
// Memory footprint optimizations: do not allocate too big bytes buffers that effectively remain unused
|
||||||
.replace(" unknownFieldsOutput);", " unknownFieldsOutput, 1);")
|
.replace(" unknownFieldsOutput);", " unknownFieldsOutput, 1);")
|
||||||
}.joinToString(LineSeparator.getSystemLineSeparator().separatorString)
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,8 +171,10 @@ private fun modifyAndExecProtoc(protoPath: ProtoPath) {
|
|||||||
|
|
||||||
private fun modifyForDebug(protoPath: ProtoPath): String {
|
private fun modifyForDebug(protoPath: ProtoPath): String {
|
||||||
var text = File(protoPath.file).readText()
|
var text = File(protoPath.file).readText()
|
||||||
.replace("option java_outer_classname = \"${protoPath.className}\"",
|
.replace(
|
||||||
"option java_outer_classname = \"${protoPath.debugClassName}\"") // give different name for class
|
"option java_outer_classname = \"${protoPath.className}\"",
|
||||||
|
"option java_outer_classname = \"${protoPath.debugClassName}\""
|
||||||
|
) // give different name for class
|
||||||
.replace("option optimize_for = LITE_RUNTIME;", "") // using default instead
|
.replace("option optimize_for = LITE_RUNTIME;", "") // using default instead
|
||||||
(listOf(EXT_OPTIONS_PROTO_PATH) + PROTO_PATHS).forEach {
|
(listOf(EXT_OPTIONS_PROTO_PATH) + PROTO_PATHS).forEach {
|
||||||
val file = it.file
|
val file = it.file
|
||||||
|
|||||||
Reference in New Issue
Block a user