Skip to content

Commit

Permalink
fix windows detection (#92)
Browse files Browse the repository at this point in the history
* fix windows detection

Co-authored-by: Anton Malinskiy <anton@malinskiy.com>
  • Loading branch information
matsudamper and Malinskiy committed Nov 27, 2022
1 parent d1a222d commit 27e509b
Showing 1 changed file with 24 additions and 11 deletions.
Expand Up @@ -18,8 +18,10 @@ package com.malinskiy.adam.interactor

import kotlinx.coroutines.delay
import java.io.File
import java.lang.ProcessBuilder.Redirect
import java.util.*

@Suppress("NewApi")
open class AdbBinaryInteractor {
suspend fun execute(
adbBinary: File?,
Expand All @@ -33,23 +35,24 @@ open class AdbBinaryInteractor {
}?.let { File(it) }

val os = System.getProperty("os.name").lowercase(Locale.ENGLISH)
val isWindows = os.contains("win")
val adbBinaryName = when {
os.contains("win") -> {
isWindows -> {
"adb.exe"
}

else -> "adb"
}

val adb = when {
adbBinary != null -> adbBinary
androidHome != null -> File(androidHome, "platform-tools" + File.separator + adbBinaryName)
androidEnvHome != null -> File(androidEnvHome, "platform-tools" + File.separator + adbBinaryName)
else -> discoverAdbBinary(os)
else -> discoverAdbBinary(isWindows)
}
if (adb?.isFile != true) return false

val builder = ProcessBuilder(adb.absolutePath, *cmd).inheritIO()

val process = builder.start()
do {
delay(16)
Expand All @@ -61,16 +64,26 @@ open class AdbBinaryInteractor {
}
}

private fun discoverAdbBinary(os: String): File? {
val discoverCommand = if (os == "win") "where" else "which"
val builder = ProcessBuilder(discoverCommand, "adb").inheritIO()
private fun discoverAdbBinary(isWindows: Boolean): File? {
val discoverCommand = if (isWindows) "where" else "which"

val builder = ProcessBuilder(discoverCommand, "adb")
.redirectInput(Redirect.INHERIT)
.redirectOutput(Redirect.PIPE)
.redirectError(Redirect.PIPE)

val process = builder.start()

process.outputStream.close()
val stdout = process.inputStream.bufferedReader().use { it.readText() }
val stderr = process.errorStream.bufferedReader().use { it.readText() }

process.waitFor()

return process.takeIf { it.exitValue() == 0 }
?.inputStream
?.bufferedReader()
?.readLine()
?.let(::File)
return if (process.exitValue() == 0) {
stdout.lines().firstOrNull()?.let(::File)
} else {
null
}
}
}

0 comments on commit 27e509b

Please sign in to comment.