Skip to content

Commit

Permalink
feat: 可以探测原生adb的设备
Browse files Browse the repository at this point in the history
  • Loading branch information
ChingCdesu committed Sep 22, 2023
1 parent fcdc6e5 commit cd2e2fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
12 changes: 11 additions & 1 deletion packages/main/deviceDetector/adapters/linuxAdapter.ts
Original file line number Diff line number Diff line change
@@ -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<void> {}
Expand All @@ -13,7 +16,14 @@ class LinuxEmulator implements EmulatorAdapter {
protected async getLd(): Promise<void> {}

async getAdbDevices(): Promise<Device[]> {
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<Emulator[]> {
Expand Down
12 changes: 11 additions & 1 deletion packages/main/deviceDetector/adapters/macAdapter.ts
Original file line number Diff line number Diff line change
@@ -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<void> {}
Expand All @@ -13,7 +16,14 @@ class MacEmulator implements EmulatorAdapter {
protected async getLd(): Promise<void> {}

async getAdbDevices(): Promise<Device[]> {
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<Emulator[]> {
Expand Down
12 changes: 9 additions & 3 deletions packages/main/deviceDetector/adapters/winAdapter.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -370,8 +370,14 @@ class WindowsAdapter implements EmulatorAdapter {
}

async getAdbDevices(): Promise<Device[]> {
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<Emulator[]> {
Expand Down
25 changes: 25 additions & 0 deletions packages/main/deviceDetector/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
}))
}

0 comments on commit cd2e2fe

Please sign in to comment.