Minor, cleanup GenerateProtoBuf.kt

This commit is contained in:
Alexander Udalov
2016-06-07 17:53:12 +03:00
parent 0c0a451d20
commit aba1cc3d83
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.generators.protobuf package org.jetbrains.kotlin.generators.protobuf
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.util.ExecUtil import com.intellij.execution.util.ExecUtil
import java.io.File import java.io.File
import java.util.regex.Pattern import java.util.regex.Pattern
@@ -29,7 +30,7 @@ import java.util.regex.Pattern
// * You can also download source and build it yourself (https://code.google.com/p/protobuf/downloads/list) // * You can also download source and build it yourself (https://code.google.com/p/protobuf/downloads/list)
// //
// You may need to provide custom path to protoc executable, just modify this constant: // You may need to provide custom path to protoc executable, just modify this constant:
val PROTOC_EXE = "protoc" private const val PROTOC_EXE = "protoc"
class ProtoPath(val file: String) { class ProtoPath(val file: String) {
val outPath: String = File(file).parent val outPath: String = File(file).parent
@@ -80,8 +81,8 @@ fun main(args: Array<String>) {
} }
} }
fun checkVersion() { private fun checkVersion() {
val processOutput = ExecUtil.execAndGetOutput(listOf(PROTOC_EXE, "--version"), null) val processOutput = ExecUtil.execAndGetOutput(GeneralCommandLine(PROTOC_EXE, "--version"))
val version = processOutput.stdout.trim() val version = processOutput.stdout.trim()
if (version.isEmpty()) { if (version.isEmpty()) {
@@ -92,15 +93,20 @@ fun checkVersion() {
} }
} }
fun execProtoc(protoPath: String, outPath: String) { private fun execProtoc(protoPath: String, outPath: String) {
val processOutput = ExecUtil.execAndGetOutput(listOf(PROTOC_EXE, protoPath, "--java_out=$outPath") + PROTOBUF_PROTO_PATHS.map { "--proto_path=$it" }, null) val commandLine = GeneralCommandLine(
listOf(PROTOC_EXE, protoPath, "--java_out=$outPath") +
PROTOBUF_PROTO_PATHS.map { "--proto_path=$it" }
)
println("running $commandLine")
val processOutput = ExecUtil.execAndGetOutput(commandLine)
print(processOutput.stdout) print(processOutput.stdout)
if (processOutput.stderr.isNotEmpty()) { if (processOutput.stderr.isNotEmpty()) {
throw AssertionError(processOutput.stderr) throw AssertionError(processOutput.stderr)
} }
} }
fun modifyAndExecProtoc(protoPath: ProtoPath) { private fun modifyAndExecProtoc(protoPath: ProtoPath) {
val debugProtoFile = File(protoPath.file.replace(".proto", ".debug.proto")) val debugProtoFile = File(protoPath.file.replace(".proto", ".debug.proto"))
debugProtoFile.writeText(modifyForDebug(protoPath)) debugProtoFile.writeText(modifyForDebug(protoPath))
debugProtoFile.deleteOnExit() debugProtoFile.deleteOnExit()
@@ -108,7 +114,7 @@ fun modifyAndExecProtoc(protoPath: ProtoPath) {
execProtoc(debugProtoFile.path, "build-common/test") execProtoc(debugProtoFile.path, "build-common/test")
} }
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.className}\"",
"option java_outer_classname = \"${protoPath.debugClassName}\"") // give different name for class "option java_outer_classname = \"${protoPath.debugClassName}\"") // give different name for class