Android module: fix stopRedundantEmulators method

This commit is contained in:
Natalia.Ukhorskaya
2012-07-30 15:30:39 +04:00
parent 4685679eaf
commit 912304893e
3 changed files with 54 additions and 39 deletions
@@ -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()) {
@@ -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<String> 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));
}
}
@@ -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);