feat: 支持安装前后ADB命令与安装后启动应用
按工程保存前后置命令和自动启动选项,全程绑定选中设备,区分安装与后续动作失败。通过实际APK及目标设备解析Launcher入口,兼容activity-alias;增加模拟进程回归测试及0.8.0三套服务部署记录。
This commit is contained in:
1 parent
6f9cc7e7ea
commit
b46ebd1070
18 files changed
+472
-35
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var shellWord = regexp.MustCompile(`^[A-Za-z0-9_./:@%+=,-]+$`)
|
||||
var apkPackage = regexp.MustCompile(`(?m)^package: name='([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)+)'`)
|
||||
var launcherComponent = regexp.MustCompile(`^[A-Za-z0-9_.]+/[A-Za-z0-9_.$]+$`)
|
||||
|
||||
func parseInstallCommands(text string) ([][]string, error) {
|
||||
if len(text) > 16<<10 {
|
||||
return nil, fmt.Errorf("安装命令最多16KiB")
|
||||
}
|
||||
result := [][]string{}
|
||||
for i, line := range strings.Split(text, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
words := strings.Fields(line)
|
||||
if strings.EqualFold(words[0], "adb") || strings.EqualFold(words[0], "adb.exe") {
|
||||
words = words[1:]
|
||||
}
|
||||
if len(words) < 2 || words[0] != "shell" {
|
||||
return nil, fmt.Errorf("第%d行仅支持adb shell命令;设备由插件指定,请勿填写-s/-d/-e", i+1)
|
||||
}
|
||||
for _, word := range words {
|
||||
if !shellWord.MatchString(word) {
|
||||
return nil, fmt.Errorf("第%d行参数包含不支持的字符;请勿使用引号、管道、重定向或命令拼接", i+1)
|
||||
}
|
||||
}
|
||||
result = append(result, words)
|
||||
if len(result) > 10 {
|
||||
return nil, fmt.Errorf("每个安装阶段最多10条命令")
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Server) runInstallCommands(ctx context.Context, serial, stage string, commands [][]string, log *strings.Builder) error {
|
||||
for i, args := range commands {
|
||||
fmt.Fprintf(log, "[%s %d/%d] adb -s %s %s\n", stage, i+1, len(commands), serial, strings.Join(args, " "))
|
||||
child, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
out, e := s.adbCommand(child, append([]string{"-s", serial}, args...)...)
|
||||
cancel()
|
||||
log.WriteString(out)
|
||||
if e != nil {
|
||||
return fmt.Errorf("%s命令失败,已停止该阶段:%w", stage, e)
|
||||
}
|
||||
log.WriteString("\n退出码:0\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) apkApplicationID(ctx context.Context, path string) (string, error) {
|
||||
cfg, e := s.buildEnvironment()
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
exe := "aapt"
|
||||
if runtime.GOOS == "windows" {
|
||||
exe += ".exe"
|
||||
}
|
||||
candidates, _ := filepath.Glob(filepath.Join(cfg.AndroidHome, "build-tools", "*", exe))
|
||||
sort.Sort(sort.Reverse(sort.StringSlice(candidates)))
|
||||
if binary, e := exec.LookPath(exe); e == nil {
|
||||
candidates = append(candidates, binary)
|
||||
}
|
||||
for _, binary := range candidates {
|
||||
if st, e := os.Stat(binary); e != nil || st.IsDir() {
|
||||
continue
|
||||
}
|
||||
child, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
cmd := exec.CommandContext(child, binary, "dump", "badging", path)
|
||||
var out adbOutput
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
e := cmd.Run()
|
||||
cancel()
|
||||
if e == nil {
|
||||
if match := apkPackage.FindStringSubmatch(out.String()); match != nil {
|
||||
return match[1], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("无法从APK读取包名,请检查服务端Android SDK build-tools中的aapt;应用已安装")
|
||||
}
|
||||
|
||||
func (s *Server) launchInstalledAPK(ctx context.Context, serial, path string, log *strings.Builder) error {
|
||||
pkg, e := s.apkApplicationID(ctx, path)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
child, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
out, e := s.adbCommand(child, "-s", serial, "shell", "cmd", "package", "resolve-activity", "--brief", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER", pkg)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
component := ""
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if launcherComponent.MatchString(line) && strings.HasPrefix(line, pkg+"/") {
|
||||
component = line
|
||||
}
|
||||
}
|
||||
if component == "" {
|
||||
return fmt.Errorf("%s没有可解析的默认Launcher入口;保持安装成功", pkg)
|
||||
}
|
||||
fmt.Fprintf(log, "[打开应用] adb -s %s:%s\n", serial, component)
|
||||
out, e = s.adbCommand(child, "-s", serial, "shell", "am", "start", "-W", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER", "-n", "'"+component+"'")
|
||||
log.WriteString(out)
|
||||
if e == nil && (strings.Contains(out, "Error:") || strings.Contains(out, "Exception")) {
|
||||
e = fmt.Errorf("启动失败:%s", out)
|
||||
}
|
||||
return e
|
||||
}
|
||||
Reference in new issue
Block a user