Skip to content

Commit

Permalink
add adb path resolution && logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Sumin committed Nov 15, 2022
1 parent 8ed449d commit 2a86a0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.kaspersky.adbserver.common.api.CommandResult
import java.nio.file.Path

/**
* @param adbPath - path to adb binary if null when used "adb" command
* @param adbPath - path to adb binary
*/
class AdbCommandPerformer(
private val adbPath: Path?,
private val adbPath: Path,
private val cmdCommandPerformer: CmdCommandPerformer,
) {

Expand All @@ -16,7 +16,6 @@ class AdbCommandPerformer(
* @param command - adb command without path to adb binaries
*/
fun perform(command: String): CommandResult {
val adbPath = adbPath?.toString() ?: "adb"
return cmdCommandPerformer.perform("$adbPath $command")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import kotlinx.cli.delimiter
import java.io.File
import java.lang.management.ManagementFactory
import java.nio.file.Path
import kotlin.system.exitProcess

private const val DESKTOP = "Desktop-"
private const val ERROR_EXIT_CODE = -1

internal fun main(args: Array<String>) {
val parser = ArgParser("Adb Server")
Expand All @@ -35,7 +38,7 @@ internal fun main(args: Array<String>) {
description = "Logs Level"
).default(LogLevel.INFO)

val adbPath by parser.option(
val adbPathFromArguments by parser.option(
type = ArgType.String,
fullName = "adbPath",
description = "Path to adb binary, if not set when use 'adb' from process environment"
Expand All @@ -46,10 +49,17 @@ internal fun main(args: Array<String>) {
val desktopName = getDesktopName()
val desktopLogger = LoggerFactory.getDesktopLogger(logLevel, desktopName)

desktopLogger.i("Desktop started with arguments: emulators=$emulators, adbServerPort=$port")
val adbPath = adbPathFromArguments?.let { Path.of(it) } ?: findAdbAtPath()

if (adbPath == null) {
desktopLogger.e("Adb path not passed via arguments. Adb not found at path variable. Aborting...")
exitProcess(ERROR_EXIT_CODE)
}

desktopLogger.i("Desktop started with arguments: emulators=$emulators, adbServerPort=$port, adbPath=$adbPath")

val cmdCommandPerformer = CmdCommandPerformer(desktopName)
val adbCommandPerformer = AdbCommandPerformer(adbPath?.let { Path.of(it) }, cmdCommandPerformer)
val adbCommandPerformer = AdbCommandPerformer(adbPath, cmdCommandPerformer)
val desktop = Desktop(
cmdCommandPerformer = cmdCommandPerformer,
adbCommandPerformer = adbCommandPerformer,
Expand All @@ -65,3 +75,15 @@ private fun getDesktopName(): String {
val pid = processName.split("@".toRegex()).toTypedArray()[0].toLong()
return DESKTOP + pid
}

/**
* Search for adb executable at environment PATH variable
*/
private fun findAdbAtPath(): Path? {
return System.getenv("PATH")
.split(":")
.asSequence()
.map { File(it) }
.flatMap { it.listFiles()?.toList() ?: emptyList() }
.firstOrNull { it.nameWithoutExtension == "adb" }?.toPath()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal class DesktopServerHolder(private val logger: Logger) {
@Synchronized
fun start(workingDirectory: Path, adbPath: Path) {
check(desktop == null) { "Desktop already started" }

logger.debug("Starting Desktop server. workingDir=$workingDirectory, adbPath=$adbPath")

val cmdCommandPerformer = CmdCommandPerformer(DESKTOP_NAME, workingDirectory)
val adbCommandPerformer = AdbCommandPerformer(adbPath, cmdCommandPerformer)
val logger = LoggerFactory.getDesktopLogger(LogLevel.VERBOSE, DESKTOP_NAME, GradleFullLogger(logger))
Expand Down

0 comments on commit 2a86a0c

Please sign in to comment.