Build tw: extract line processor from build output reader

This commit is contained in:
Vladislav.Soroka
2019-04-16 15:20:58 +03:00
parent 0b472c1e90
commit 1ba2a153f0
2 changed files with 63 additions and 36 deletions
@@ -23,7 +23,7 @@ class BuildOutputInstantReaderImpl(private val buildId: Any,
private val receivedLinesBuffer = LinkedList<String>()
private var currentIndex = -1
private var lineBuilder: StringBuilder? = null
private val lineProcessor: LineProcessor
init {
val thisReader = this
@@ -48,6 +48,19 @@ class BuildOutputInstantReaderImpl(private val buildId: Any,
}
}
}
lineProcessor = object : LineProcessor() {
override fun process(line: String) {
if (job.isCompleted) {
LOG.warn("Build output reader closed")
return
}
if (!job.isActive) {
job.start()
}
runBlocking { channel.send(line) }
}
}
}
override fun getBuildId(): Any {
@@ -55,59 +68,28 @@ class BuildOutputInstantReaderImpl(private val buildId: Any,
}
override fun append(csq: CharSequence): BuildOutputInstantReaderImpl {
for (i in 0 until csq.length) {
append(csq[i])
}
lineProcessor.append(csq)
return this
}
override fun append(csq: CharSequence, start: Int, end: Int): BuildOutputInstantReaderImpl {
append(csq.subSequence(start, end))
lineProcessor.append(csq, start, end)
return this
}
override fun append(c: Char): BuildOutputInstantReaderImpl {
if (lineBuilder == null) {
lineBuilder = StringBuilder()
}
if (c == '\n') {
runBlocking {
doFlush()
}
}
else {
lineBuilder!!.append(c)
}
lineProcessor.append(c)
return this
}
override fun close() {
runBlocking {
doFlush()
lineProcessor.close()
channel.close()
job.cancelAndJoin()
}
}
private suspend fun doFlush() {
if (lineBuilder == null) {
return
}
if (job.isCompleted) {
LOG.warn("Build output reader closed")
lineBuilder!!.setLength(0)
return
}
val line = lineBuilder!!.toString()
lineBuilder!!.setLength(0)
if (!job.isActive) {
job.start()
}
channel.send(line)
}
override fun readLine(): String? {
if (currentIndex < -1) {
LOG.error("Wrong buffered output lines index")
@@ -0,0 +1,45 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.build.output
import java.io.Closeable
abstract class LineProcessor : Appendable, Closeable {
private var lineBuilder: StringBuilder? = null
abstract fun process(line: String)
override fun append(csq: CharSequence): LineProcessor {
for (i in 0 until csq.length) {
append(csq[i])
}
return this
}
override fun append(csq: CharSequence, start: Int, end: Int): LineProcessor {
append(csq.subSequence(start, end))
return this
}
override fun append(c: Char): LineProcessor {
if (lineBuilder == null) {
lineBuilder = StringBuilder()
}
if (c == '\n') {
flushBuffer()
}
else {
lineBuilder!!.append(c)
}
return this
}
override fun close() {
flushBuffer()
}
private fun flushBuffer() {
val line = lineBuilder?.toString() ?: return
lineBuilder!!.setLength(0)
process(line)
}
}