Skip to content

Commit a3609e1

Browse files
authored
Merge pull request #1 from cem2ran/feat/init-emulator-with-config
Implement merging of local config.ini into AVD config
2 parents 61458be + ab6cb2e commit a3609e1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

maestro-client/src/main/java/maestro/device/DeviceService.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,46 @@ object DeviceService {
439439
throw IllegalStateException("Failed to start android emulator: $processOutput")
440440
}
441441

442+
// Merge local config.ini if present
443+
val localConfig = File(System.getProperty("user.dir"), "config.ini")
444+
if (localConfig.exists()) {
445+
val androidHome = when (val env = System.getenv("ANDROID_USER_HOME")) {
446+
null -> "${System.getProperty("user.home")}/.android"
447+
else -> env
448+
}
449+
val avdConfig = File(androidHome, "avd/$name.avd/config.ini")
450+
451+
if (avdConfig.exists()) {
452+
try {
453+
val localEntries = localConfig.readLines()
454+
.filter { it.contains("=") && !it.trimStart().startsWith("#") }
455+
.associate { it.substringBefore("=").trim() to it.substringAfter("=").trim() }
456+
457+
if (localEntries.isNotEmpty()) {
458+
val existingLines = avdConfig.readLines().toMutableList()
459+
val keyToIndex = existingLines
460+
.mapIndexedNotNull { i, line ->
461+
if (line.contains("=")) line.substringBefore("=").trim() to i else null
462+
}
463+
.toMap()
464+
465+
localEntries.forEach { (key, value) ->
466+
keyToIndex[key]?.let { index ->
467+
existingLines[index] = "$key=$value"
468+
} ?: run {
469+
existingLines.add("$key=$value")
470+
}
471+
}
472+
473+
avdConfig.writeText(existingLines.joinToString("\n") + "\n")
474+
logger.info("Merged ${localEntries.size} entries from local config.ini into AVD $name")
475+
}
476+
} catch (e: Exception) {
477+
logger.warn("Failed to merge local config.ini: ${e.message}")
478+
}
479+
}
480+
}
481+
442482
return name
443483
}
444484

0 commit comments

Comments
 (0)