Reading input stream from ProcessBuilder pipeline in build.gradle.kts hangs

48 Views Asked by At

I've read through a ton of questions here and none of the solution code works.

I'm in android studio writing a build.gradle.kts script to get a previous version number and increment it by 1.

import kotlin.collections.List
import org.gradle.nativeplatform.platform.OperatingSystem
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

...

val os: OperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
var version: String
if (os.isWindows()) {
    println("Windows")
    val versionProcesses = ProcessBuilder
        .startPipeline(
            List<ProcessBuilder>(2) {
                ProcessBuilder("git", "log", "--pretty=format:'%D'")
                    .directory(project.rootProject.projectDir);
                ProcessBuilder("findstr", "'tag: v\\d{4}\\.\\d{1,2}\\.\\d{1,2}'");
            }
        )
    val lastProcess = versionProcesses.get(1)
    val inputstream = lastProcess.inputStream
    val bufferedreader = inputstream.bufferedReader()
    // I've tried readText, Scanner, outputstream.flush/close, a while loop, etc. nothing works.
    val line = bufferedreader.readLine()
    version = line.split('\n').first().split('v')[1].dropLast(1)
} else  {
    ...
}

val versions = version.split('.')
val majorVersion = versions[0]
val minorVersion = versions[1].toInt() + 1
val patchVersion = "0"
val versionName = "$majorVersion.$minorVersion.$patchVersion"
println(versionName)
return versionName

The command I'm trying to run is this:

git log --pretty=format:'%D' | findstr "tag: v\\d{4}\\.\\d{1,}\\.\\d{1,}"

The output looks like this:

tag: v2023.11.1
tag: v2023.11.0
tag: v2023.10.3
tag: v2023.10.2
tag: v2023.10.1
tag: v2023.10.0
tag: v2023.9.4
tag: v2023.9.3
tag: v2023.9.2
tag: v2023.9.1
tag: v2023.9.0
tag: v2023.8.9
tag: v2023.8.8
...

I just want to read the first line and strip out the 2023.11.1.

The code always hangs on the readLine(). I've tried readText(). Doesn't work. I've tried using a Scanner, but I can't import it. import java.util.Scanner doesn't work. I've tried a while loop to read through all the lines in the buffer. Doesn't work.

EDIT1: Adding the --no-pager argument for git does not fix the problem.

EDIT2: Simplifying the code to just this:

val versionProcess = ProcessBuilder()
    .command("git", "log", "--pretty=format:'%D'")
    .directory(project.rootProject.projectDir)
    .start()
versionProcess.waitFor()
val output = versionProcess.inputStream.bufferedReader().readText()
println(output)

also hangs

0

There are 0 best solutions below