From cd2e2fe4196fcdda671bb980f20d4ff3cb3e36b5 Mon Sep 17 00:00:00 2001 From: _ChingC Date: Fri, 22 Sep 2023 14:14:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=AF=E4=BB=A5=E6=8E=A2=E6=B5=8B?= =?UTF-8?q?=E5=8E=9F=E7=94=9Fadb=E7=9A=84=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../deviceDetector/adapters/linuxAdapter.ts | 12 ++++++++- .../deviceDetector/adapters/macAdapter.ts | 12 ++++++++- .../deviceDetector/adapters/winAdapter.ts | 12 ++++++--- packages/main/deviceDetector/utils.ts | 25 +++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/packages/main/deviceDetector/adapters/linuxAdapter.ts b/packages/main/deviceDetector/adapters/linuxAdapter.ts index 4c3b4c49..250f751f 100644 --- a/packages/main/deviceDetector/adapters/linuxAdapter.ts +++ b/packages/main/deviceDetector/adapters/linuxAdapter.ts @@ -1,7 +1,10 @@ import { Singleton } from '@common/function/singletonDecorator' +import { $ } from '@main/utils/shell' import type { Device, Emulator, EmulatorAdapter } from '@type/device' import psList from 'ps-list' +import { defaultAdbPath, getDeviceUuid, parseAdbDevice } from '../utils' + @Singleton class LinuxEmulator implements EmulatorAdapter { protected async getBluestack(): Promise {} @@ -13,7 +16,14 @@ class LinuxEmulator implements EmulatorAdapter { protected async getLd(): Promise {} async getAdbDevices(): Promise { - return [] + const { stdout } = await $`${defaultAdbPath} devices` + const devices = parseAdbDevice(stdout) + return Promise.all( + devices.map(async d => { + const uuid = await getDeviceUuid(d.address, defaultAdbPath) + return { ...d, uuid: uuid || '' } + }) + ) } async getEmulators(): Promise { diff --git a/packages/main/deviceDetector/adapters/macAdapter.ts b/packages/main/deviceDetector/adapters/macAdapter.ts index be84a0cc..5bd2d4b4 100644 --- a/packages/main/deviceDetector/adapters/macAdapter.ts +++ b/packages/main/deviceDetector/adapters/macAdapter.ts @@ -1,7 +1,10 @@ import { Singleton } from '@common/function/singletonDecorator' +import { $ } from '@main/utils/shell' import type { Device, Emulator, EmulatorAdapter } from '@type/device' import psList from 'ps-list' +import { defaultAdbPath, getDeviceUuid, parseAdbDevice } from '../utils' + @Singleton class MacEmulator implements EmulatorAdapter { protected async getBluestack(): Promise {} @@ -13,7 +16,14 @@ class MacEmulator implements EmulatorAdapter { protected async getLd(): Promise {} async getAdbDevices(): Promise { - return [] + const { stdout } = await $`${defaultAdbPath} devices` + const devices = parseAdbDevice(stdout) + return Promise.all( + devices.map(async d => { + const uuid = await getDeviceUuid(d.address, defaultAdbPath) + return { ...d, uuid: uuid || '' } + }) + ) } async getEmulators(): Promise { diff --git a/packages/main/deviceDetector/adapters/winAdapter.ts b/packages/main/deviceDetector/adapters/winAdapter.ts index c45f218b..6aaddba9 100644 --- a/packages/main/deviceDetector/adapters/winAdapter.ts +++ b/packages/main/deviceDetector/adapters/winAdapter.ts @@ -1,6 +1,6 @@ /* eslint-disable vue/max-len */ import { Singleton } from '@common/function/singletonDecorator' -import { defaultAdbPath, getDeviceUuid } from '@main/deviceDetector/utils' +import { defaultAdbPath, getDeviceUuid, parseAdbDevice } from '@main/deviceDetector/utils' import logger from '@main/utils/logger' import { $ } from '@main/utils/shell' import type { Device, Emulator, EmulatorAdapter } from '@type/device' @@ -370,8 +370,14 @@ class WindowsAdapter implements EmulatorAdapter { } async getAdbDevices(): Promise { - const emulators: Device[] = [] - return emulators + const { stdout } = await $`${defaultAdbPath} devices` + const devices = parseAdbDevice(stdout) + return Promise.all( + devices.map(async d => { + const uuid = await getDeviceUuid(d.address, defaultAdbPath) + return { ...d, uuid: uuid || '' } + }) + ) } async getEmulators(): Promise { diff --git a/packages/main/deviceDetector/utils.ts b/packages/main/deviceDetector/utils.ts index bfbd2f1f..aaa9b78e 100644 --- a/packages/main/deviceDetector/utils.ts +++ b/packages/main/deviceDetector/utils.ts @@ -2,6 +2,7 @@ import { getComponentBaseDir } from '@main/componentManager/utils/path' import logger from '@main/utils/logger' import { getPlatform } from '@main/utils/os' import { $, $$ } from '@main/utils/shell' +import type { Device } from '@type/device' import { app } from 'electron' import fs from 'fs' import _ from 'lodash' @@ -38,3 +39,27 @@ export async function getDeviceUuid( } return false } + +export function parseAdbDevice(stdout: string): Device[] { + const lines = stdout.trim().split('\n') + lines.shift() + + if (lines.length === 0) { + return [] + } + + const connected = lines + .map(line => { + const [address, status] = line.split('\t') + return { address, status } + }) + .filter(({ status }) => status === 'device') + + return connected.map(({ address }) => ({ + uuid: '', + address, + status: 'available', + adbPath: defaultAdbPath, + config: 'General', + })) +}