Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
0.7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyitao committed Oct 2, 2020
1 parent 85c7908 commit 3262643
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 157 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.daxiang</groupId>
<artifactId>agent</artifactId>
<version>0.7.6</version>
<version>0.7.8</version>
<packaging>jar</packaging>

<properties>
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/daxiang/action/AndroidAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.daxiang.core.mobile.android.AndroidUtil;
import com.daxiang.core.mobile.android.IDeviceExecuteShellCommandException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

/**
* Created by jiangyitao.
Expand All @@ -25,22 +24,16 @@ public AndroidAction(AndroidDevice androidDevice) {

@Action(id = 2000, name = "清除apk数据", platforms = 1)
public void clearApkData(@Param(description = "包名") String packageName) throws IDeviceExecuteShellCommandException {
Assert.hasText(packageName, "包名不能为空");

AndroidUtil.clearApkData(androidDevice.getIDevice(), packageName);
}

@Action(id = 2001, name = "启动/重启apk", platforms = 1)
public void restartApk(@Param(description = "包名") String packageName, @Param(description = "启动Activity名") String launchActivity) throws IDeviceExecuteShellCommandException {
Assert.hasText(packageName, "包名不能为空");
Assert.hasText(launchActivity, "启动Activity不能为空");

AndroidUtil.restartApk(androidDevice.getIDevice(), packageName, launchActivity);
}

@Action(id = 2002, name = "执行adb shell命令", returnValueDesc = "命令返回信息", platforms = 1)
public String executeAdbShellCommand(@Param(description = "命令") String cmd) throws IDeviceExecuteShellCommandException {
Assert.hasText(cmd, "命令不能为空");
return AndroidUtil.executeShellCommand(androidDevice.getIDevice(), cmd);
}
}
11 changes: 0 additions & 11 deletions src/main/java/com/daxiang/controller/ActionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,4 @@ public class ActionController {
public Response debug(@Valid @RequestBody ActionDebugRequest request) {
return actionService.debug(request);
}

/**
* 给开发者调试专用
*
* @param code
* @return
*/
@PostMapping("/developer/debug")
public Response developerDebug(String className, String code) {
return actionService.compileAndDebug(className, code);
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/daxiang/controller/AndroidController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.daxiang.model.Response;
import com.daxiang.service.AndroidService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

Expand All @@ -17,26 +18,30 @@ public class AndroidController {

@GetMapping("/{mobileId}/adbkit/start")
public Response startAdbKit(@PathVariable String mobileId) {
return androidService.startAdbKit(mobileId);
int port = androidService.startAdbKit(mobileId);
return Response.success(ImmutableMap.of("port", port));
}

@GetMapping("/{mobileId}/adbkit/stop")
public Response stop(@PathVariable String mobileId) {
return androidService.stopAdbKit(mobileId);
androidService.stopAdbKit(mobileId);
return Response.success("停止完成");
}

@PostMapping("/aaptDumpBadging")
public Response aaptDumpBadging(@RequestBody String apkDownloadUrl) {
return androidService.aaptDumpBadging(apkDownloadUrl);
String res = androidService.aaptDumpBadging(apkDownloadUrl);
return Response.success("ok", res);
}

@GetMapping("{mobileId}/imeList")
public Response getImeList(@PathVariable String mobileId) {
return androidService.getImeList(mobileId);
return Response.success(androidService.getImeList(mobileId));
}

@PostMapping("{mobileId}/ime")
public Response setIme(@PathVariable String mobileId, String ime) {
return androidService.setIme(mobileId, ime);
androidService.setIme(mobileId, ime);
return Response.success("设置输入法成功");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class BrowserController {

@GetMapping("/{browserId}")
public Response getBrowser(@PathVariable String browserId) {
return browserService.getBrowser(browserId);
return Response.success(browserService.getBrowser(browserId));
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/daxiang/controller/DeviceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class DeviceController {

@GetMapping("/{deviceId}/dump")
public Response dump(@PathVariable String deviceId) {
return deviceService.dump(deviceId);
return Response.success(deviceService.dump(deviceId));
}

@GetMapping("/{deviceId}/screenshot")
public Response screenshot(@PathVariable String deviceId) {
return deviceService.screenshot(deviceId);
return Response.success(deviceService.screenshot(deviceId));
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/daxiang/controller/MobileController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ public class MobileController {

@PostMapping("/{mobileId}/installApp")
public Response installApp(MultipartFile app, @PathVariable String mobileId) {
return mobileService.installApp(app, mobileId);
mobileService.installApp(app, mobileId);
return Response.success("安装成功");
}

@GetMapping("/{mobileId}")
public Response getMobile(@PathVariable String mobileId) {
return mobileService.getMobile(mobileId);
return Response.success(mobileService.getMobile(mobileId));
}
}
24 changes: 11 additions & 13 deletions src/main/java/com/daxiang/core/mobile/android/ADB.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Created by jiangyitao.
Expand All @@ -28,26 +30,22 @@ public static void startServer() throws IOException {
Terminal.execute("adb start-server");
}

/**
* 获取adb路径
*
* @return
*/
private static String getPath() {
String androidHome = System.getenv("ANDROID_HOME");
log.info("环境变量ANDROID_HOME: {}", androidHome);
log.info("ANDROID_HOME: {}", androidHome);

if (StringUtils.isEmpty(androidHome)) {
throw new RuntimeException("未获取到ANDROID_HOME,请配置ANDROID_HOME环境变量");
throw new IllegalStateException("环境变量缺少ANDROID_HOME");
}

String adbPath = androidHome + File.separator + "platform-tools" + File.separator;
if (Terminal.IS_WINDOWS) {
adbPath = adbPath + "adb.exe";
} else {
adbPath = adbPath + "adb";
}
String adbPrefixPath = androidHome + File.separator + "platform-tools" + File.separator;
String adbPath = Terminal.IS_WINDOWS ? adbPrefixPath + "adb.exe" : adbPrefixPath + "adb";
log.info("adb路径: {}", adbPath);

if (!Files.exists(Paths.get(adbPath))) {
throw new IllegalStateException(adbPath + "文件不存在");
}

return adbPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public static String getMemSize(IDevice iDevice) throws IDeviceExecuteShellComma
}

public static String getDeviceName(IDevice iDevice) {
return String.format("[%s] %s", iDevice.getProperty("ro.product.brand"), iDevice.getProperty("ro.product.model"));
String brand = iDevice.getProperty("ro.product.brand");
String model = iDevice.getProperty("ro.product.model");
return String.format("[%s] %s", brand, model);
}

public static String getAndroidVersion(Integer sdkVerison) {
Expand Down Expand Up @@ -134,10 +136,14 @@ public static String aaptDumpBadging(String apkPath) throws IOException {
}

public static void clearApkData(IDevice iDevice, String packageName) throws IDeviceExecuteShellCommandException {
Assert.hasText(packageName, "packageName must has text");
executeShellCommand(iDevice, "pm clear " + packageName);
}

public static void restartApk(IDevice iDevice, String packageName, String launchActivity) throws IDeviceExecuteShellCommandException {
Assert.hasText(packageName, "packageName must has text");
Assert.hasText(launchActivity, "launchActivity must has text");

executeShellCommand(iDevice, "am start -S -n " + packageName + "/" + launchActivity);
}

Expand Down Expand Up @@ -168,6 +174,7 @@ public static List<String> getImeList(IDevice iDevice) throws IDeviceExecuteShel
}

public static void setIme(IDevice iDevice, String ime) throws IDeviceExecuteShellCommandException {
Assert.hasText(ime, "ime must has text");
executeShellCommand(iDevice, "ime set " + ime);
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/daxiang/core/testng/TestNGRunner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.daxiang.core.testng;

import com.daxiang.model.Response;
import com.daxiang.exception.ActionDebugException;
import org.springframework.util.CollectionUtils;
import org.testng.ITestNGListener;
import org.testng.TestNG;
Expand All @@ -21,18 +21,18 @@ public static void runTestCases(Class[] classes, Integer failRetryCount) {
}
}

public static Response debugAction(Class clazz) {
public static String debugAction(Class clazz) throws ActionDebugException {
TestNG testNG = run(new Class[]{clazz}, Arrays.asList(DebugActionTestListener.class));
if (testNG.getStatus() != 0) { // 运行有错误
return Response.fail(DebugActionTestListener.getFailMsg());
} else { // 运行成功
List<String> printMsgList = DebugActionTestListener.getPrintMsgList();
if (CollectionUtils.isEmpty(printMsgList)) {
printMsgList = Arrays.asList("执行成功");
}

return Response.success(String.join("\n", printMsgList));
throw new ActionDebugException(DebugActionTestListener.getFailMsg());
}

List<String> printMsgList = DebugActionTestListener.getPrintMsgList();
if (CollectionUtils.isEmpty(printMsgList)) {
printMsgList = Arrays.asList("执行成功");
}

return String.join("\n", printMsgList);
}

private static TestNG run(Class[] testClasses, List<Class<? extends ITestNGListener>> listenerClasses) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/daxiang/exception/ActionDebugException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.daxiang.exception;

/**
* Created by jiangyitao.
*/
public class ActionDebugException extends Exception {
public ActionDebugException(String msg) {
super(msg);
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/daxiang/model/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Response<T> {
private String msg;
private T data;

private static <T> Response<T> buildResponse(Integer status, String msg, T data) {
Response response = new Response();
private static <T> Response<T> createResponse(Integer status, String msg, T data) {
Response<T> response = new Response<>();
response.setStatus(status);
response.setMsg(msg);
response.setData(data);
Expand All @@ -27,35 +27,35 @@ private static <T> Response<T> buildResponse(Integer status, String msg, T data)

@JsonIgnore
public boolean isSuccess() {
return status == SUCCESS;
return SUCCESS.equals(status);
}

public static Response success() {
return buildResponse(SUCCESS, "success", null);
return createResponse(SUCCESS, "success", null);
}

public static <T> Response<T> success(T data) {
return buildResponse(SUCCESS, "success", data);
return createResponse(SUCCESS, "success", data);
}

public static Response success(String msg) {
return buildResponse(SUCCESS, msg, null);
return createResponse(SUCCESS, msg, null);
}

public static <T> Response<T> success(String msg, T data) {
return buildResponse(SUCCESS, msg, data);
return createResponse(SUCCESS, msg, data);
}

public static Response fail(String msg) {
return buildResponse(FAIL, msg, null);
return createResponse(FAIL, msg, null);
}

public static <T> Response<T> fail(String msg, T data) {
return buildResponse(FAIL, msg, data);
return createResponse(FAIL, msg, data);
}

public static Response error(String msg) {
return buildResponse(ERROR, msg, null);
return createResponse(ERROR, msg, null);
}

}
25 changes: 5 additions & 20 deletions src/main/java/com/daxiang/service/ActionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.daxiang.utils.UUIDUtil;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.dvare.dynamic.exceptions.DynamicCompilerException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

Expand All @@ -24,12 +23,6 @@
@Service
public class ActionService {

/**
* 调试action
*
* @param request
* @return
*/
public Response debug(ActionDebugRequest request) {
DeviceTestTask deviceTestTask = new DeviceTestTask();
BeanUtils.copyProperties(request, deviceTestTask);
Expand All @@ -48,21 +41,13 @@ public Response debug(ActionDebugRequest request) {
return Response.fail(e.getMessage());
}

Response response = compileAndDebug(className, code);
response.setData(ImmutableMap.of("code", code));
return response;
}

/**
* 编译调试运行
*/
public Response compileAndDebug(String className, String code) {
try {
Class clazz = JavaCompiler.compile(className, code);
return TestNGRunner.debugAction(clazz);
} catch (DynamicCompilerException e) {
log.error("编译{}失败: {}", className, e.getMessage());
return Response.fail(e.getMessage());
String printInfo = TestNGRunner.debugAction(clazz);
return Response.success(printInfo, ImmutableMap.of("code", code));
} catch (Exception e) {
log.error("[{}]err msg: {}, code: {} ", request.getDeviceId(), e.getMessage(), code);
return Response.fail(e.getMessage(), ImmutableMap.of("code", code));
}
}

Expand Down
Loading

0 comments on commit 3262643

Please sign in to comment.