From 912304893ee8fd539932c8bd74a1cc55141103f5 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Mon, 30 Jul 2012 15:30:39 +0400 Subject: [PATCH] Android module: fix stopRedundantEmulators method --- .../jetbrains/jet/compiler/OutputUtils.java | 37 +------------ .../jet/compiler/emulator/Emulator.java | 54 +++++++++++++++++-- .../jetbrains/jet/compiler/run/RunUtils.java | 2 +- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java b/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java index 761f9d9d9ce..c2157add3e3 100644 --- a/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java +++ b/compiler/android-tests/src/org/jetbrains/jet/compiler/OutputUtils.java @@ -31,11 +31,10 @@ import java.util.regex.Pattern; public class OutputUtils { - private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*"); private final static Pattern EMULATOR_PROCESS_PATTERN = Pattern.compile("\\w*[\\s]+([0-9]*) .* java .* emulator .*"); - public static boolean isResultOk(String output) { - return !(output.contains("BUILD FAILED") || output.contains("Build failed")); + public static boolean isBuildFailed(String output) { + return output.contains("BUILD FAILED") || output.contains("Build failed"); } public static void checkResult(RunResult result) { @@ -44,38 +43,6 @@ public class OutputUtils { } } - public static void stopRedundantEmulators(PathManager pathManager) { - GeneralCommandLine commandLineForListOfDevices = new GeneralCommandLine(); - String adbCmdName = SystemInfo.isWindows ? "adb.exe" : "adb"; - commandLineForListOfDevices.setExePath(pathManager.getPlatformToolsFolderInAndroidSdk() + "/" + adbCmdName); - commandLineForListOfDevices.addParameter("devices"); - RunResult runResult = RunUtils.execute(commandLineForListOfDevices); - checkResult(runResult); - - Matcher matcher = EMULATOR_PATTERN.matcher(runResult.getOutput()); - while (matcher.find()) { - System.out.println("Stopping redundant emulator..."); - GeneralCommandLine commandLineForStoppingEmulators = new GeneralCommandLine(); - if (SystemInfo.isWindows) { - commandLineForStoppingEmulators.setExePath("taskkill"); - commandLineForStoppingEmulators.addParameter("/F"); - commandLineForStoppingEmulators.addParameter("/IM"); - commandLineForStoppingEmulators.addParameter("emulator-arm.exe"); - checkResult(RunUtils.execute(commandLineForStoppingEmulators)); - break; - } - else { - commandLineForStoppingEmulators.setExePath(pathManager.getPlatformToolsFolderInAndroidSdk() + "/adb"); - commandLineForStoppingEmulators.addParameter("-s"); - commandLineForStoppingEmulators.addParameter(matcher.group()); - commandLineForStoppingEmulators.addParameter("emu"); - commandLineForStoppingEmulators.addParameter("kill"); - checkResult(RunUtils.execute(commandLineForStoppingEmulators)); - } - } - checkResult(RunUtils.execute(commandLineForListOfDevices)); - } - @Nullable public static String getPidFromPsCommand(String output) { if (!output.isEmpty()) { diff --git a/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java b/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java index 4119d06928f..27a16fe16e3 100644 --- a/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java +++ b/compiler/android-tests/src/org/jetbrains/jet/compiler/emulator/Emulator.java @@ -18,17 +18,25 @@ package org.jetbrains.jet.compiler.emulator; import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.jet.compiler.OutputUtils; import org.jetbrains.jet.compiler.PathManager; import org.jetbrains.jet.compiler.run.RunUtils; import org.jetbrains.jet.compiler.run.result.RunResult; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * @author Natalia.Ukhorskaya */ public class Emulator { + private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*"); + + private final PathManager pathManager; public Emulator(PathManager pathManager) { @@ -102,7 +110,7 @@ public class Emulator { public void startEmulator() { System.out.println("Starting emulator..."); - OutputUtils.stopRedundantEmulators(pathManager); + stopRedundantEmulators(pathManager); OutputUtils.checkResult(RunUtils.executeOnSeparateThread(getStartCommand(), false)); } @@ -147,12 +155,52 @@ public class Emulator { pidOfProcess.setExePath("pidof"); pidOfProcess.addParameter(processName); RunResult runResult = RunUtils.execute(pidOfProcess); - String pid = runResult.getOutput().substring(("pidof " + processName).length()); - if (pid.length() > 1) { + String processIdsStr = runResult.getOutput().substring(("pidof " + processName).length()); + List processIds = StringUtil.getWordsIn(processIdsStr); + for (String pid : processIds) { GeneralCommandLine killCommand = new GeneralCommandLine(); killCommand.setExePath("kill"); killCommand.addParameter(pid); OutputUtils.checkResult(RunUtils.execute(killCommand)); } } + + private void stopRedundantEmulators(PathManager pathManager) { + GeneralCommandLine commandLineForListOfDevices = new GeneralCommandLine(); + String adbCmdName = SystemInfo.isWindows ? "adb.exe" : "adb"; + commandLineForListOfDevices.setExePath(pathManager.getPlatformToolsFolderInAndroidSdk() + "/" + adbCmdName); + commandLineForListOfDevices.addParameter("devices"); + RunResult runResult = RunUtils.execute(commandLineForListOfDevices); + OutputUtils.checkResult(runResult); + + Matcher matcher = EMULATOR_PATTERN.matcher(runResult.getOutput()); + boolean isDdmsStopped = false; + while (matcher.find()) { + System.out.println("Stopping redundant emulator..."); + GeneralCommandLine commandLineForStoppingEmulators = new GeneralCommandLine(); + if (SystemInfo.isWindows) { + commandLineForStoppingEmulators.setExePath("taskkill"); + commandLineForStoppingEmulators.addParameter("/F"); + commandLineForStoppingEmulators.addParameter("/IM"); + commandLineForStoppingEmulators.addParameter("emulator-arm.exe"); + OutputUtils.checkResult(RunUtils.execute(commandLineForStoppingEmulators)); + break; + } + else { + if (!isDdmsStopped && SystemInfo.isUnix) { + finishProcess("emulator-arm"); + stopDdmsProcess(); + isDdmsStopped = true; + } + commandLineForStoppingEmulators.setExePath(pathManager.getPlatformToolsFolderInAndroidSdk() + "/adb"); + commandLineForStoppingEmulators.addParameter("-s"); + commandLineForStoppingEmulators.addParameter(matcher.group()); + commandLineForStoppingEmulators.addParameter("emu"); + commandLineForStoppingEmulators.addParameter("kill"); + OutputUtils.checkResult(RunUtils.execute(commandLineForStoppingEmulators)); + } + } + OutputUtils.checkResult(RunUtils.execute(commandLineForListOfDevices)); + } + } diff --git a/compiler/android-tests/src/org/jetbrains/jet/compiler/run/RunUtils.java b/compiler/android-tests/src/org/jetbrains/jet/compiler/run/RunUtils.java index 555e66208d5..d2628651912 100644 --- a/compiler/android-tests/src/org/jetbrains/jet/compiler/run/RunUtils.java +++ b/compiler/android-tests/src/org/jetbrains/jet/compiler/run/RunUtils.java @@ -139,7 +139,7 @@ public class RunUtils { } else { String output = builderToString(stdOut) + builderToString(stdErr); - if (!OutputUtils.isResultOk(output)) { + if (OutputUtils.isBuildFailed(output)) { return new RunResult(false, output); } return new RunResult(true, output);