Android module: add check for another running devices

This commit is contained in:
Natalia.Ukhorskaya
2012-07-06 12:52:53 +04:00
parent 96a7128a36
commit 0ba9ddd6a9
4 changed files with 98 additions and 40 deletions
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.compiler;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.jet.compiler.run.RunUtils;
import org.jetbrains.jet.compiler.run.result.RunResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Natalia.Ukhorskaya
*/
public class OutputUtils {
private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*");
public static boolean isResultOk(String output) {
return !(output.contains("BUILD FAILED") || output.contains("Build failed"));
}
public static void checkResult(RunResult result) {
if (!result.getStatus()) {
throw new RuntimeException(result.getOutput());
}
}
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("emu");
commandLineForStoppingEmulators.addParameter("kill");
commandLineForStoppingEmulators.addParameter("-s");
commandLineForStoppingEmulators.addParameter(matcher.group());
checkResult(RunUtils.execute(commandLineForStoppingEmulators));
}
}
}
private OutputUtils() {
}
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.compiler.ant;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
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;
@@ -40,31 +41,25 @@ public class AntRunner {
listOfAntCommands.add("-buildfile");
listOfAntCommands.add(pathManager.getTmpFolder() + "/build.xml");
}
/* Pack compiled sources on first step to one jar file */
public void packLibraries() {
System.out.println("Pack libraries...");
RunResult result = RunUtils.execute(generateCommandLine("pack_libraries"));
if (!result.getStatus()) {
throw new RuntimeException(result.getOutput());
}
OutputUtils.checkResult(result);
}
/* Clean output directory */
public void cleanOutput() {
System.out.println("Clearing output directory...");
RunResult result = RunUtils.execute(generateCommandLine("clean"));
if (!result.getStatus()) {
throw new RuntimeException(result.getOutput());
}
OutputUtils.checkResult(result);
}
public void compileSources() {
System.out.println("Compiling sources...");
RunResult result = RunUtils.execute(generateCommandLine("debug"));
if (!result.getStatus()) {
throw new RuntimeException(result.getOutput());
}
OutputUtils.checkResult(result);
}
public void installApplicationOnEmulator() {
@@ -74,24 +69,19 @@ public class AntRunner {
if (!isInstallSuccessful(resultOutput)) {
installApplicationOnEmulator();
return;
} else {
}
else {
System.out.println(resultOutput);
}
if (!result.getStatus()) {
throw new RuntimeException(resultOutput);
}
OutputUtils.checkResult(result);
}
public String runTestsOnEmulator() {
System.out.println("Running tests...");
RunResult result = RunUtils.execute(generateCommandLine("test"));
String resultOutput = result.getOutput();
if (!result.getStatus()) {
throw new RuntimeException(resultOutput);
}
else {
return resultOutput;
}
OutputUtils.checkResult(result);
return resultOutput;
}
private static boolean isInstallSuccessful(String output) {
@@ -18,6 +18,7 @@ package org.jetbrains.jet.compiler.emulator;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
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;
@@ -95,31 +96,26 @@ public class Emulator {
public void createEmulator() {
System.out.println("Creating emulator...");
checkResult(RunUtils.execute(getCreateCommand(), "no"));
OutputUtils.checkResult(RunUtils.execute(getCreateCommand(), "no"));
}
public void startEmulator() {
System.out.println("Starting emulator...");
checkResult(RunUtils.executeOnSeparateThread(getStartCommand(), false));
OutputUtils.stopRedundantEmulators(pathManager);
OutputUtils.checkResult(RunUtils.executeOnSeparateThread(getStartCommand(), false));
}
public void waitEmulatorStart() {
System.out.println("Waiting for emulator start...");
checkResult(RunUtils.execute(getWaitCommand()));
OutputUtils.checkResult(RunUtils.execute(getWaitCommand()));
}
public void stopEmulator() {
System.out.println("Stopping emulator...");
checkResult(RunUtils.execute(getStopCommand()));
OutputUtils.checkResult(RunUtils.execute(getStopCommand()));
System.out.println("Stopping adb...");
checkResult(RunUtils.execute(getStopCommandForAdb()));
}
private static void checkResult(RunResult result) {
if (!result.getStatus()) {
throw new RuntimeException(result.getOutput());
}
OutputUtils.checkResult(RunUtils.execute(getStopCommandForAdb()));
}
}
@@ -27,6 +27,7 @@ import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.OutputUtils;
import org.jetbrains.jet.compiler.run.result.RunResult;
import java.io.Closeable;
@@ -138,7 +139,7 @@ public class RunUtils {
}
else {
String output = builderToString(stdOut) + builderToString(stdErr);
if (!isResultOk(output)) {
if (!OutputUtils.isResultOk(output)) {
return new RunResult(false, output);
}
return new RunResult(true, output);
@@ -173,12 +174,5 @@ public class RunUtils {
}
return writer.toString();
}
public static boolean isResultOk(String output) {
if (output.contains("BUILD FAILED")
|| output.contains("Build failed")) {
return false;
}
return true;
}
}