xcodebug 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GUIDE.md +213 -0
- package/LICENSE +21 -0
- package/README.md +449 -2
- package/archive.ts +164 -0
- package/capture.ts +327 -0
- package/changes.ts +368 -0
- package/devices.ts +278 -0
- package/exec.ts +122 -0
- package/index.ts +2274 -0
- package/package.json +52 -4
- package/progress.ts +171 -0
- package/xcode.ts +220 -0
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xcodebug",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Build, install and launch an iOS app on a physical device and read its console inside the agent session (pi / omp extension)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi-extension",
|
|
8
|
+
"ios",
|
|
9
|
+
"xcode",
|
|
10
|
+
"xcodebuild",
|
|
11
|
+
"devicectl",
|
|
12
|
+
"ios-device",
|
|
13
|
+
"device-debugging",
|
|
14
|
+
"log-capture",
|
|
15
|
+
"oslog"
|
|
16
|
+
],
|
|
17
|
+
"author": "codebaby <liuxingsky555@gmail.com>",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/sublimter/xcodebug.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/sublimter/xcodebug#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/sublimter/xcodebug/issues"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"*.ts",
|
|
29
|
+
"README.md",
|
|
30
|
+
"GUIDE.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20"
|
|
35
|
+
},
|
|
36
|
+
"omp": {
|
|
37
|
+
"extensions": [
|
|
38
|
+
"./index.ts"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"pi": {
|
|
42
|
+
"extensions": [
|
|
43
|
+
"./index.ts"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
48
|
+
"typebox": "*"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22",
|
|
52
|
+
"typescript": "^5"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/progress.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build progress for xcodebuild (stage-based).
|
|
3
|
+
*
|
|
4
|
+
* xcodebuild exposes no percentage and no total task count: the "compiled N of
|
|
5
|
+
* M" bar lives in the Xcode IDE over a private IPC channel, and there is no
|
|
6
|
+
* -dry-run to precompute M (measured: the same scheme reports 32,042 tasks on a
|
|
7
|
+
* clean build and 3,217 on an interrupted one).
|
|
8
|
+
*
|
|
9
|
+
* What the log DOES expose reliably is the build phase. Markers are ordered by
|
|
10
|
+
* where they actually appear in a real 322k-line log, not by intuition:
|
|
11
|
+
* ProcessInfoPlistFile at line 5,133 - a pod plist, long before compiling
|
|
12
|
+
* CompileSwift at line 55,406
|
|
13
|
+
* Ld at line 63,826
|
|
14
|
+
* CodeSign at line 65,102
|
|
15
|
+
* ** BUILD SUCCEEDED ** at line 322,614
|
|
16
|
+
* Only markers that genuinely come last advance the stage.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type BuildStage = "starting" | "compiling" | "linking" | "signing" | "done" | "failed";
|
|
20
|
+
|
|
21
|
+
const STAGE_LABEL: Record<BuildStage, string> = {
|
|
22
|
+
starting: "preparing",
|
|
23
|
+
compiling: "compiling",
|
|
24
|
+
linking: "linking",
|
|
25
|
+
signing: "signing",
|
|
26
|
+
done: "done",
|
|
27
|
+
failed: "failed",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Coarse completion fraction by stage. */
|
|
31
|
+
const STAGE_FLOOR: Record<BuildStage, number> = {
|
|
32
|
+
starting: 0,
|
|
33
|
+
compiling: 0.05,
|
|
34
|
+
linking: 0.85,
|
|
35
|
+
signing: 0.93,
|
|
36
|
+
done: 1,
|
|
37
|
+
failed: 1,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* How far a stage may creep past its floor while it lasts.
|
|
42
|
+
*
|
|
43
|
+
* A flat floor is what made the bar sit at exactly 93% through a long signing
|
|
44
|
+
* phase: nothing distinguished "still working" from "hung". Creeping makes the
|
|
45
|
+
* bar move during a quiet stage, and the cap keeps it from reaching the next
|
|
46
|
+
* stage's floor, so a real transition still shows as a jump rather than a slide.
|
|
47
|
+
*/
|
|
48
|
+
const STAGE_CREEP: Record<BuildStage, number> = {
|
|
49
|
+
starting: 0.04,
|
|
50
|
+
compiling: 0.75,
|
|
51
|
+
linking: 0.07,
|
|
52
|
+
signing: 0.06,
|
|
53
|
+
done: 0,
|
|
54
|
+
failed: 0,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** A stage's creep reaches its cap after this long without a phase change. */
|
|
58
|
+
const CREEP_MS = 120_000;
|
|
59
|
+
|
|
60
|
+
export interface BuildProgress {
|
|
61
|
+
stage: BuildStage;
|
|
62
|
+
stageLabel: string;
|
|
63
|
+
/** Completed task lines seen so far - a fact, not an estimate. */
|
|
64
|
+
tasks: number;
|
|
65
|
+
elapsedMs: number;
|
|
66
|
+
/** Coarse completion fraction from the stage model (0..1). */
|
|
67
|
+
fraction: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function stageFromLine(line: string): BuildStage | undefined {
|
|
71
|
+
// Order matters: the success marker also contains "** BUILD".
|
|
72
|
+
if (line.startsWith("** BUILD SUCCEEDED")) return "done";
|
|
73
|
+
if (line.startsWith("** BUILD FAILED") || line.startsWith("** BUILD INTERRUPTED")) return "failed";
|
|
74
|
+
if (line.startsWith("CodeSign ")) return "signing";
|
|
75
|
+
if (line.startsWith("ComputeTargetDependencyGraph") || line.startsWith("SendProjectDescription") ||
|
|
76
|
+
line.startsWith("CreateBuildOperation") || line.startsWith("GatherProvisioningInputs") ||
|
|
77
|
+
line.startsWith("CreateBuildDescription") || line.startsWith("ExecuteExternalTool")) return "starting";
|
|
78
|
+
if (line.startsWith("Ld ") || line.startsWith("Libtool ") || line.startsWith("GenerateDSYMFile")) return "linking";
|
|
79
|
+
// Any per-target work line means the build has left "preparing". An
|
|
80
|
+
// incremental build emits no CompileSwift at all - it is all
|
|
81
|
+
// PhaseScriptExecution / ProcessInfoPlistFile - so those count too.
|
|
82
|
+
if (
|
|
83
|
+
line.startsWith("CompileSwift") ||
|
|
84
|
+
line.startsWith("SwiftCompile") ||
|
|
85
|
+
line.startsWith("SwiftDriver") ||
|
|
86
|
+
line.startsWith("EmitSwiftModule") ||
|
|
87
|
+
line.startsWith("CompileC") ||
|
|
88
|
+
line.startsWith("CompileMetal") ||
|
|
89
|
+
line.startsWith("CompileAssetCatalog") ||
|
|
90
|
+
line.startsWith("CompileXIB") ||
|
|
91
|
+
line.startsWith("CompileStoryboard") ||
|
|
92
|
+
line.startsWith("PhaseScriptExecution") ||
|
|
93
|
+
line.startsWith("ProcessInfoPlistFile") ||
|
|
94
|
+
line.startsWith("CpResource") ||
|
|
95
|
+
line.startsWith("CpHeader") ||
|
|
96
|
+
line.startsWith("CopySwiftLibs") ||
|
|
97
|
+
line.startsWith("WriteAuxiliaryFile")
|
|
98
|
+
) {
|
|
99
|
+
return "compiling";
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Tracks one build's stage from its output stream. */
|
|
105
|
+
export class BuildProgressTracker {
|
|
106
|
+
private readonly startedAt = Date.now();
|
|
107
|
+
private stage: BuildStage = "starting";
|
|
108
|
+
/** When the current stage began; creep is measured from here. */
|
|
109
|
+
private stageStartedAt = Date.now();
|
|
110
|
+
private tasks = 0;
|
|
111
|
+
private remainder = "";
|
|
112
|
+
|
|
113
|
+
/** Feed a raw output chunk; returns the current progress snapshot. */
|
|
114
|
+
update(chunk: string): BuildProgress {
|
|
115
|
+
const pending = this.remainder + chunk;
|
|
116
|
+
const lines = pending.split("\n");
|
|
117
|
+
this.remainder = lines.pop() ?? "";
|
|
118
|
+
for (const line of lines) {
|
|
119
|
+
const trimmed = line.trimEnd();
|
|
120
|
+
if (trimmed.includes("(in target '")) this.tasks += 1;
|
|
121
|
+
const next = stageFromLine(trimmed.trimStart());
|
|
122
|
+
// Stages only move forward; a stray earlier-phase line must not rewind.
|
|
123
|
+
if (next && STAGE_FLOOR[next] >= STAGE_FLOOR[this.stage]) {
|
|
124
|
+
if (next !== this.stage) this.stageStartedAt = Date.now();
|
|
125
|
+
this.stage = next;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return this.snapshot();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
snapshot(): BuildProgress {
|
|
132
|
+
return {
|
|
133
|
+
stage: this.stage,
|
|
134
|
+
stageLabel: STAGE_LABEL[this.stage],
|
|
135
|
+
tasks: this.tasks,
|
|
136
|
+
elapsedMs: Date.now() - this.startedAt,
|
|
137
|
+
fraction: this.fraction(),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Stage floor plus however much this stage has crept toward its cap.
|
|
143
|
+
*
|
|
144
|
+
* Signing a large app can take minutes with no output at all, and a frozen
|
|
145
|
+
* 93% is indistinguishable from a wedged build. Creep keeps the bar honest
|
|
146
|
+
* about "still going" without ever claiming the next stage was reached.
|
|
147
|
+
*/
|
|
148
|
+
private fraction(): number {
|
|
149
|
+
const floor = STAGE_FLOOR[this.stage];
|
|
150
|
+
const creep = STAGE_CREEP[this.stage];
|
|
151
|
+
if (creep <= 0) return floor;
|
|
152
|
+
const inStage = Date.now() - this.stageStartedAt;
|
|
153
|
+
const progress = Math.min(1, inStage / CREEP_MS);
|
|
154
|
+
return floor + creep * progress;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
finish(stage: "done" | "failed"): BuildProgress {
|
|
158
|
+
this.stage = stage;
|
|
159
|
+
return this.snapshot();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** One-line rendering for tool updates. */
|
|
164
|
+
export function formatProgress(progress: BuildProgress): string {
|
|
165
|
+
const barWidth = 20;
|
|
166
|
+
const filled = Math.round(progress.fraction * barWidth);
|
|
167
|
+
const bar = `${"█".repeat(filled)}${"░".repeat(barWidth - filled)}`;
|
|
168
|
+
const elapsed = `${(progress.elapsedMs / 1000).toFixed(0)}s`;
|
|
169
|
+
const tasks = progress.tasks > 0 ? ` · ${progress.tasks.toLocaleString()} tasks` : "";
|
|
170
|
+
return `${bar} ${Math.round(progress.fraction * 100)}% · ${progress.stageLabel}${tasks} · ${elapsed}`;
|
|
171
|
+
}
|
package/xcode.ts
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Xcode build / install / launch plumbing for a physical iOS device.
|
|
3
|
+
*
|
|
4
|
+
* The console mode mirrors what Xcode shows in its debug console: devicectl
|
|
5
|
+
* bridges the app's stdout/stderr and, with OS_ACTIVITY_DT_MODE=YES, its
|
|
6
|
+
* os_log output as well. No jailbreak and no debugger attach are involved.
|
|
7
|
+
*/
|
|
8
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { asRecord, asString, lastLines, runCommand, timestamp } from "./exec";
|
|
11
|
+
import { BuildProgressTracker, formatProgress } from "./progress";
|
|
12
|
+
import type { DeviceInfo } from "./devices";
|
|
13
|
+
|
|
14
|
+
export interface BuildTarget {
|
|
15
|
+
workspace?: string;
|
|
16
|
+
project?: string;
|
|
17
|
+
scheme: string;
|
|
18
|
+
configuration: string;
|
|
19
|
+
derivedDataPath?: string;
|
|
20
|
+
extraArgs?: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BuildOutcome {
|
|
24
|
+
ok: boolean;
|
|
25
|
+
appPath?: string;
|
|
26
|
+
bundleId?: string;
|
|
27
|
+
executable?: string;
|
|
28
|
+
logFile: string;
|
|
29
|
+
summary: string;
|
|
30
|
+
durationMs: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function destination(device: DeviceInfo): string {
|
|
34
|
+
const id = device.udid ?? device.id;
|
|
35
|
+
return `platform=iOS,id=${id}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function targetFlags(target: BuildTarget): string[] {
|
|
39
|
+
const flags: string[] = [];
|
|
40
|
+
if (target.workspace) flags.push("-workspace", target.workspace);
|
|
41
|
+
else if (target.project) flags.push("-project", target.project);
|
|
42
|
+
|
|
43
|
+
flags.push("-scheme", target.scheme, "-configuration", target.configuration);
|
|
44
|
+
if (target.derivedDataPath) flags.push("-derivedDataPath", target.derivedDataPath);
|
|
45
|
+
if (target.extraArgs?.length) flags.push(...target.extraArgs);
|
|
46
|
+
return flags;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function readPlistValue(appPath: string, key: string): Promise<string | undefined> {
|
|
50
|
+
const result = await runCommand(
|
|
51
|
+
"/usr/libexec/PlistBuddy",
|
|
52
|
+
["-c", `Print :${key}`, join(appPath, "Info.plist")],
|
|
53
|
+
{ timeoutMs: 15_000 },
|
|
54
|
+
);
|
|
55
|
+
if (result.code !== 0) return undefined;
|
|
56
|
+
return asString(result.stdout.trim());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function buildForDevice(
|
|
60
|
+
target: BuildTarget,
|
|
61
|
+
device: DeviceInfo,
|
|
62
|
+
options: {
|
|
63
|
+
logDir: string;
|
|
64
|
+
timeoutMs: number;
|
|
65
|
+
cwd: string;
|
|
66
|
+
signal?: AbortSignal;
|
|
67
|
+
/** Called at most a few times a second while the build runs. */
|
|
68
|
+
onProgress?: (text: string) => void;
|
|
69
|
+
},
|
|
70
|
+
): Promise<BuildOutcome> {
|
|
71
|
+
const started = Date.now();
|
|
72
|
+
const logFile = join(options.logDir, `build-${target.scheme}-${timestamp()}.log`);
|
|
73
|
+
await mkdir(dirname(logFile), { recursive: true });
|
|
74
|
+
|
|
75
|
+
const args = [...targetFlags(target), "-destination", destination(device), "build"];
|
|
76
|
+
const tracker = new BuildProgressTracker();
|
|
77
|
+
let lastEmit = 0;
|
|
78
|
+
let lastStage = "";
|
|
79
|
+
// xcodebuild can go quiet for minutes (signing, copying a large bundle). A
|
|
80
|
+
// heartbeat keeps the bar and its clock moving so a slow stage never looks
|
|
81
|
+
// like a wedged one; the stage logic itself still comes from the output.
|
|
82
|
+
const heartbeat = setInterval(() => {
|
|
83
|
+
if (!options.onProgress) return;
|
|
84
|
+
lastEmit = Date.now();
|
|
85
|
+
options.onProgress(formatProgress(tracker.snapshot()));
|
|
86
|
+
}, 5_000);
|
|
87
|
+
heartbeat.unref?.();
|
|
88
|
+
const result = await runCommand("xcodebuild", args, {
|
|
89
|
+
cwd: options.cwd,
|
|
90
|
+
timeoutMs: options.timeoutMs,
|
|
91
|
+
maxBytes: 12_000_000,
|
|
92
|
+
signal: options.signal,
|
|
93
|
+
onChunk: (_stream, chunk) => {
|
|
94
|
+
const progress = tracker.update(chunk);
|
|
95
|
+
// Throttle: a build emits tens of thousands of lines.
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
// A stage change is always worth a frame; otherwise throttle, because a
|
|
98
|
+
// build emits tens of thousands of lines and every frame is a tool update.
|
|
99
|
+
const stageChanged = progress.stage !== lastStage;
|
|
100
|
+
if (options.onProgress && (stageChanged || now - lastEmit >= 2_000)) {
|
|
101
|
+
lastEmit = now;
|
|
102
|
+
lastStage = progress.stage;
|
|
103
|
+
options.onProgress(formatProgress(progress));
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
clearInterval(heartbeat);
|
|
108
|
+
await writeFile(logFile, `$ xcodebuild ${args.join(" ")}\n\n${result.stdout}\n${result.stderr}`);
|
|
109
|
+
|
|
110
|
+
const durationMs = Date.now() - started;
|
|
111
|
+
if (result.code !== 0) {
|
|
112
|
+
const final = tracker.finish("failed");
|
|
113
|
+
options.onProgress?.(formatProgress(final));
|
|
114
|
+
const reason = result.timedOut ? "build timed out" : `build failed (exit ${result.code})`;
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
logFile,
|
|
118
|
+
durationMs,
|
|
119
|
+
summary: `${reason}\n${lastLines(`${result.stderr}\n${result.stdout}`, 25)}`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
options.onProgress?.(formatProgress(tracker.finish("done")));
|
|
123
|
+
|
|
124
|
+
// Resolve the product path from the SAME destination the build used. Without
|
|
125
|
+
// -destination, xcodebuild reports the default platform (maccatalyst for a
|
|
126
|
+
// framework scheme), and appPath would point at the wrong product.
|
|
127
|
+
const settings = await runCommand(
|
|
128
|
+
"xcodebuild",
|
|
129
|
+
[...targetFlags(target), "-destination", destination(device), "-showBuildSettings", "-json"],
|
|
130
|
+
{
|
|
131
|
+
cwd: options.cwd,
|
|
132
|
+
timeoutMs: 120_000,
|
|
133
|
+
maxBytes: 4_000_000,
|
|
134
|
+
signal: options.signal,
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
let buildDir: string | undefined;
|
|
138
|
+
let wrapper: string | undefined;
|
|
139
|
+
let bundleId: string | undefined;
|
|
140
|
+
if (settings.code === 0) {
|
|
141
|
+
try {
|
|
142
|
+
const parsed = JSON.parse(settings.stdout);
|
|
143
|
+
const entries = Array.isArray(parsed) ? parsed : [parsed];
|
|
144
|
+
for (const entry of entries) {
|
|
145
|
+
const values = asRecord(asRecord(entry).buildSettings);
|
|
146
|
+
if (asString(values.TARGET_BUILD_DIR) && asString(values.WRAPPER_NAME)) {
|
|
147
|
+
buildDir = asString(values.TARGET_BUILD_DIR);
|
|
148
|
+
wrapper = asString(values.WRAPPER_NAME);
|
|
149
|
+
bundleId = asString(values.PRODUCT_BUNDLE_IDENTIFIER);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
// Fall through to the log-only success report.
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const appPath = buildDir && wrapper ? join(buildDir, wrapper) : undefined;
|
|
159
|
+
const executable = appPath ? await readPlistValue(appPath, "CFBundleExecutable") : undefined;
|
|
160
|
+
const plistBundleId = appPath ? await readPlistValue(appPath, "CFBundleIdentifier") : undefined;
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
ok: true,
|
|
164
|
+
appPath,
|
|
165
|
+
bundleId: plistBundleId ?? bundleId,
|
|
166
|
+
executable,
|
|
167
|
+
logFile,
|
|
168
|
+
durationMs,
|
|
169
|
+
summary: `build succeeded in ${(durationMs / 1000).toFixed(1)}s${appPath ? `\napp: ${appPath}` : ""}`,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function installApp(
|
|
174
|
+
device: DeviceInfo,
|
|
175
|
+
appPath: string,
|
|
176
|
+
options: { timeoutMs: number; signal?: AbortSignal },
|
|
177
|
+
): Promise<{ ok: boolean; message: string }> {
|
|
178
|
+
const result = await runCommand(
|
|
179
|
+
"xcrun",
|
|
180
|
+
["devicectl", "device", "install", "app", "--device", device.id, appPath],
|
|
181
|
+
{ timeoutMs: options.timeoutMs, maxBytes: 2_000_000, signal: options.signal },
|
|
182
|
+
);
|
|
183
|
+
if (result.code === 0) return { ok: true, message: "installed" };
|
|
184
|
+
return {
|
|
185
|
+
ok: false,
|
|
186
|
+
message: `install failed (exit ${result.code}):\n${lastLines(`${result.stderr}\n${result.stdout}`, 15)}`,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function consoleLaunchArgs(
|
|
191
|
+
device: DeviceInfo,
|
|
192
|
+
bundleId: string,
|
|
193
|
+
options: { args?: string[]; jsonOutput?: string; terminateExisting: boolean; environment?: Record<string, string> },
|
|
194
|
+
): string[] {
|
|
195
|
+
const args = ["devicectl", "device", "process", "launch", "--device", device.id, "--console"];
|
|
196
|
+
if (options.terminateExisting) args.push("--terminate-existing");
|
|
197
|
+
if (options.jsonOutput) args.push("--json-output", options.jsonOutput);
|
|
198
|
+
const environment = { OS_ACTIVITY_DT_MODE: "YES", ...options.environment };
|
|
199
|
+
args.push("-e", JSON.stringify(environment));
|
|
200
|
+
args.push(bundleId);
|
|
201
|
+
if (options.args?.length) args.push("--", ...options.args);
|
|
202
|
+
return args;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function terminateApp(
|
|
206
|
+
device: DeviceInfo,
|
|
207
|
+
pid: number,
|
|
208
|
+
options: { timeoutMs: number },
|
|
209
|
+
): Promise<{ ok: boolean; message: string }> {
|
|
210
|
+
const result = await runCommand(
|
|
211
|
+
"xcrun",
|
|
212
|
+
["devicectl", "device", "process", "terminate", "--device", device.id, "--pid", String(pid)],
|
|
213
|
+
{ timeoutMs: options.timeoutMs, maxBytes: 200_000 },
|
|
214
|
+
);
|
|
215
|
+
if (result.code === 0) return { ok: true, message: `terminated pid ${pid}` };
|
|
216
|
+
return {
|
|
217
|
+
ok: false,
|
|
218
|
+
message: `terminate failed (exit ${result.code}): ${(result.stderr || result.stdout).trim().slice(0, 400)}`,
|
|
219
|
+
};
|
|
220
|
+
}
|