Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Add windows compatibility #45

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions swarmer/src/main/kotlin/com/gojuno/swarmer/Args.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sealed class Commands {
val ALIASES_START = listOf("start")
val ALIASES_STOP = listOf("stop")

fun fromStringAlias(alias: String): Commands? = when {
fun fromStringAlias(alias: String?): Commands? = when {
ALIASES_HELP.contains(alias) -> Help
ALIASES_STOP.contains(alias) -> Stop()
ALIASES_START.contains(alias) -> Start()
Expand Down Expand Up @@ -140,7 +140,7 @@ sealed class Commands {
) : Commands()
}

fun parseCommand(rawArgs: List<String>) = Commands.fromStringAlias(rawArgs[0])
fun parseCommand(rawArgs: List<String>) = Commands.fromStringAlias(rawArgs.getOrNull(0))

fun parseStartArguments(rawArgs: List<String>): List<Commands.Start> =
rawArgs
Expand Down
47 changes: 37 additions & 10 deletions swarmer/src/main/kotlin/com/gojuno/swarmer/Emulators.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package com.gojuno.swarmer

import com.gojuno.commander.android.*
import com.gojuno.commander.os.Notification
import com.gojuno.commander.os.home
import com.gojuno.commander.os.log
import com.gojuno.commander.os.process
import com.gojuno.commander.os.*
import rx.Completable
import rx.Observable
import rx.Single
import rx.schedulers.Schedulers
import rx.schedulers.Schedulers.io
import java.io.File
import java.lang.System.nanoTime
import java.util.*
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.*
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicLong

val sh: String = "/bin/sh"
val avdManager: String = "$androidHome/tools/bin/avdmanager"
val emulator = "$androidHome/emulator/emulator"
val emulatorCompat = "$androidHome/tools/emulator"
val sh: List<String> = when (os()) {
Os.Linux, Os.Mac -> listOf("/bin/sh", "-c")
Os.Windows -> listOf("cmd", "/C")
}
val runInBackground: String = when (os()) {
Os.Linux, Os.Mac -> "&"
Os.Windows -> ""
}
val avdManager: String = when (os()) {
Os.Linux, Os.Mac -> "$androidHome/tools/bin/avdmanager"
Os.Windows -> "$androidHome/tools/bin/avdmanager.bat"
}
val emulator = when (os()) {
Os.Linux, Os.Mac -> "$androidHome/emulator/emulator"
Os.Windows -> "$androidHome/emulator/emulator.exe"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.exe! RUN DON'T LOOK BACK

}
val emulatorCompat = when (os()) {
Os.Linux, Os.Mac -> "$androidHome/tools/emulator"
Os.Windows -> "$androidHome/tools/emulator.exe"
}

data class Emulator(
val id: String,
Expand Down Expand Up @@ -102,8 +116,7 @@ private fun startEmulator(
.doOnNext { log("Ports for emulator ${args.emulatorName}: ${it.first}, ${it.second}.") }
.flatMap { ports ->
startEmulatorProcess(
// Unix only, PR welcome.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew someone will delete this line haha

listOf(sh, "-c", "${emulator(args)} ${if (args.verbose) "-verbose" else ""} -avd ${args.emulatorName} -ports ${ports.first},${ports.second} ${args.emulatorStartOptions.joinToString(" ")} &"),
sh + "${emulator(args)} ${if (args.verbose) "-verbose " else ""}-avd ${args.emulatorName} -ports ${ports.first},${ports.second} ${args.emulatorStartOptions.joinToString(" ")} $runInBackground".trim(),
args
).let { process ->
waitForEmulatorToStart(args, connectedAdbDevices, process, ports)
Expand Down Expand Up @@ -334,3 +347,17 @@ private fun outputDirectory(args: Commands.Start) =

private fun connectedEmulators(): Single<Set<AdbDevice>> =
connectedAdbDevices().take(1).toSingle().map { it.filter { it.isEmulator }.toSet() }

private fun os(): Os {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do it, sounds good

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val os = System.getProperty("os.name", "unknown").toLowerCase(Locale.ENGLISH)

return if (os.contains("mac") || os.contains("darwin")) {
Os.Mac
} else if (os.contains("linux")) {
Os.Linux
} else if (os.contains("windows")) {
Os.Windows
} else {
throw IllegalStateException("Unsupported os $os, only ${Os.values()} are supported.")
}
}
4 changes: 2 additions & 2 deletions swarmer/src/test/kotlin/com/gojuno/swarmer/EmulatorsSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ class EmulatorsSpec : Spek({
START_COMMANDS.forEach { command ->
it("should start emulators") {
verify(startEmulatorsProcess).invoke(
sh +
listOf(
"/bin/sh", "-c",
"${emulator(command)} ${if (command.verbose) "-verbose" else ""} -avd ${command.emulatorName} -ports ${EMULATOR_PORTS.first},${EMULATOR_PORTS.second} ${command.emulatorStartOptions.joinToString(" ")} &"
"${emulator(command)} ${if (command.verbose) "-verbose" else ""} -avd ${command.emulatorName} -ports ${EMULATOR_PORTS.first},${EMULATOR_PORTS.second} ${command.emulatorStartOptions.joinToString(" ")} $runInBackground".trim()
),
command
)
Expand Down