Repository navigation
Expand file tree
/
Copy pathplatform-runtime-android-adb-host.ts
More file actions
161 lines (158 loc) · 5.64 KB
/
Copy pathplatform-runtime-android-adb-host.ts
File metadata and controls
161 lines (158 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { bindAndroidAdbHost } from '@agent-device/platform-android/adb-host';
import {
androidAdbOwnedServerPort,
androidManagedAdbEnvironment,
applyManagedAndroidAdbServer,
serializeAndroidAdbInvocation,
type AndroidAdbExecutorOptions,
type AndroidAdbInvocation,
} from '@agent-device/platform-android/mechanics';
import { createHash, randomUUID } from 'node:crypto';
import {
coerceExecResult,
execFailureDetails,
runCmd,
runCmdBackground,
withCommandExecutorOverride,
withoutCommandExecutorOverride,
} from '@agent-device/host-kit/command';
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
import { constants as fsConstants } from 'node:fs';
import {
access,
mkdir,
mkdtemp,
open,
readFile,
readdir,
rename,
rm,
stat,
writeFile,
} from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
const environment = process.env;
bindAndroidAdbHost({
environment,
files: {
access: async (candidate) => await access(candidate),
ensureDirectory: async (directory) => {
await mkdir(directory, { recursive: true });
},
isExecutable: async (candidate) => {
try {
await access(candidate, fsConstants.X_OK);
return true;
} catch {
return false;
}
},
makeTempDirectory: async (prefix) => await mkdtemp(path.join(os.tmpdir(), prefix)),
readBytes: async (filePath) => await readFile(filePath),
readDirectory: async (directory) => await readdir(directory),
readText: async (filePath) => await readFile(filePath, 'utf8'),
remove: async (target, options) => await rm(target, options),
stat: async (filePath) => {
const result = await stat(filePath);
return { isFile: result.isFile(), size: result.size };
},
sha256: (value) => createHash('sha256').update(value).digest('hex'),
writeAtomicText: async (filePath, value, mode = 0o600) => {
const directory = path.dirname(filePath);
const temporaryPath = path.join(directory, `.${path.basename(filePath)}.${randomUUID()}.tmp`);
let handle: Awaited<ReturnType<typeof open>> | undefined;
let published = false;
try {
handle = await open(temporaryPath, 'wx', mode);
await handle.writeFile(value, 'utf8');
await handle.sync();
await handle.close();
handle = undefined;
await rename(temporaryPath, filePath);
published = true;
let directoryHandle: Awaited<ReturnType<typeof open>> | undefined;
try {
directoryHandle = await open(directory, 'r');
await directoryHandle.sync();
} catch {
} finally {
if (directoryHandle) await directoryHandle.close().catch(() => {});
}
} finally {
if (handle) await handle.close().catch(() => {});
if (!published) await rm(temporaryPath, { force: true }).catch(() => {});
}
},
writeBytes: async (filePath, value) => await writeFile(filePath, value),
},
execAdb: async (invocation, options) => {
const lowered = lowerAndroidAdbInvocation(invocation, options);
return await runCmd('adb', lowered.args, lowered.options);
},
spawnAdb: (invocation, options) => {
const lowered = lowerAndroidAdbInvocation(invocation, options);
const background = runCmdBackground('adb', lowered.args, {
...lowered.options,
allowFailure: true,
captureOutput: false,
});
void background.wait.catch(() => {});
return background.child;
},
withAdbCommandExecutorOverride: withCommandExecutorOverride,
withoutAdbCommandExecutorOverride: withoutCommandExecutorOverride,
coerceAdbResult: coerceExecResult,
execFailureDetails,
emitDiagnostic,
imeRecoveryMarkers: {
write: async (stateDir, serial) => {
const { writeAndroidTestImeRecoveryMarker } =
await import('@agent-device/platform-android/mechanics');
return await writeAndroidTestImeRecoveryMarker(stateDir, serial);
},
clear: async (stateDir, serial) => {
const { clearAndroidTestImeRecoveryMarker } =
await import('@agent-device/platform-android/mechanics');
await clearAndroidTestImeRecoveryMarker(stateDir, serial);
},
read: async (stateDir) => {
const { readAndroidTestImeRecoveryMarkers } =
await import('@agent-device/platform-android/mechanics');
return await readAndroidTestImeRecoveryMarkers(stateDir);
},
},
resolveHelperArtifact: async (options) => {
const { resolveAndroidHelperArtifact } =
await import('@agent-device/platform-android/mechanics');
return await resolveAndroidHelperArtifact(options);
},
ensureHelperInstalled: async (config, request) => {
const { makeEnsureAndroidHelperInstalled } =
await import('@agent-device/platform-android/mechanics');
return await makeEnsureAndroidHelperInstalled(config)(request);
},
});
function lowerAndroidAdbInvocation(
invocation: AndroidAdbInvocation,
options?: AndroidAdbExecutorOptions,
): { args: string[]; options: AndroidAdbExecutorOptions | undefined } {
const { serverPort, detached, ...execOptions } = options ?? {};
const managedPort = serverPort ?? androidAdbOwnedServerPort(invocation);
const resolved =
managedPort === undefined
? invocation
: applyManagedAndroidAdbServer(invocation, { port: managedPort });
const env =
managedPort === undefined
? undefined
: androidManagedAdbEnvironment(resolved.target, environment, execOptions.env);
return {
args: serializeAndroidAdbInvocation(resolved),
options: {
...execOptions,
...(env === undefined ? {} : { env }),
...(detached === true && process.platform !== 'win32' ? { detached: true } : {}),
},
};
}