Skip to content

Commit 4d65aff

Browse files
committed
refactor(android): allow providers to install prepared app artifacts
1 parent a2c169f commit 4d65aff

4 files changed

Lines changed: 196 additions & 29 deletions

File tree

‎packages/contracts/src/app-deployment-runtime.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,9 @@ export type AndroidAppDeploymentExecutor = Readonly<{
115115
options?: Readonly<{ installedOnly?: boolean }>,
116116
): Promise<string>;
117117
}>;
118+
119+
/** Transfers an already materialized app to the selected target. */
120+
export type AppDeploymentInstaller = (
121+
artifact: MaterializedAppSource,
122+
signal: AbortSignal,
123+
) => Promise<AppDeploymentResult>;

‎packages/platform-android/src/deployment/runtime.test.ts‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,144 @@ test('uninstall resolves, removes and invalidates on the device readiness return
260260
signal,
261261
);
262262
});
263+
264+
test.each([false, true])(
265+
'reinstall removes data before the replacement is materialized with a hosted installer: %s',
266+
async (hosted) => {
267+
const events: string[] = [];
268+
const signal = new AbortController().signal;
269+
const artifact = {
270+
installablePath: '/tmp/new.apk',
271+
cleanup: async () => {
272+
events.push('cleanup');
273+
},
274+
};
275+
const install = vi.fn(async () => {
276+
events.push('install');
277+
return { packageName: 'com.example.app' };
278+
});
279+
native.uninstallAndroidPackage.mockImplementation(async () => {
280+
events.push('uninstall');
281+
});
282+
native.installAndroidArtifact.mockImplementation(async () => {
283+
events.push('install');
284+
return 'com.example.app';
285+
});
286+
const host = hostFixture({
287+
withInvalidatedAppResolutionCache: async (_device, operation) => {
288+
events.push('invalidate-before');
289+
try {
290+
return await operation();
291+
} finally {
292+
events.push('invalidate-after');
293+
}
294+
},
295+
prepareArtifact: async () => {
296+
events.push('materialize');
297+
return artifact;
298+
},
299+
});
300+
const operations = createAndroidAppDeploymentOperations({
301+
host,
302+
device,
303+
signal,
304+
...(hosted ? { install } : {}),
305+
});
306+
expect(
307+
await operations.deployApp!({
308+
app: 'com.example.app',
309+
appPath: '/tmp/new.apk',
310+
replaceExisting: true,
311+
}),
312+
).toEqual({ packageName: 'com.example.app', launchTarget: 'com.example.app' });
313+
expect(events).toEqual([
314+
'invalidate-before',
315+
'uninstall',
316+
'materialize',
317+
'install',
318+
'cleanup',
319+
'invalidate-after',
320+
]);
321+
if (hosted) {
322+
expect(install).toHaveBeenCalledExactlyOnceWith(artifact, signal);
323+
expect(native.installAndroidArtifact).not.toHaveBeenCalled();
324+
} else {
325+
expect(install).not.toHaveBeenCalled();
326+
expect(native.installAndroidArtifact).toHaveBeenCalledExactlyOnceWith(
327+
host,
328+
device,
329+
artifact.installablePath,
330+
undefined,
331+
signal,
332+
);
333+
}
334+
},
335+
);
336+
337+
test.each(['path', 'prepared'] as const)(
338+
'a hosted %s install uses the provider identity and leaves artifact cleanup with its owner',
339+
async (source) => {
340+
const cleanup = vi.fn(async () => {});
341+
const artifact = { installablePath: '/tmp/app.apk', cleanup };
342+
const prepareArtifact = vi.fn(async () => artifact);
343+
const install = vi.fn(async () => ({ packageName: 'com.example.hosted' }));
344+
const signal = new AbortController().signal;
345+
const operations = createAndroidAppDeploymentOperations({
346+
host: hostFixture({ prepareArtifact }),
347+
device,
348+
signal,
349+
install,
350+
});
351+
352+
const result =
353+
source === 'path'
354+
? await operations.deployApp!({
355+
app: '',
356+
appPath: artifact.installablePath,
357+
replaceExisting: false,
358+
})
359+
: await operations.deployMaterializedApp!({ artifact });
360+
361+
expect(result).toEqual({
362+
packageName: 'com.example.hosted',
363+
appName: 'hosted',
364+
launchTarget: 'com.example.hosted',
365+
});
366+
expect(install).toHaveBeenCalledExactlyOnceWith(artifact, signal);
367+
expect(native.installAndroidArtifact).not.toHaveBeenCalled();
368+
expect(prepareArtifact).toHaveBeenCalledTimes(source === 'path' ? 1 : 0);
369+
expect(cleanup).toHaveBeenCalledTimes(source === 'path' ? 1 : 0);
370+
},
371+
);
372+
373+
test.each(['path', 'prepared'] as const)(
374+
'a hosted %s install preserves provider metadata and falls back to materialized identity',
375+
async (source) => {
376+
const artifact = {
377+
installablePath: '/tmp/app.apk',
378+
packageName: 'com.example.materialized',
379+
appName: 'Materialized app',
380+
cleanup: vi.fn(async () => {}),
381+
};
382+
const providerResult = {
383+
appName: 'Provider app',
384+
launchTarget: 'com.example.materialized/.MainActivity',
385+
};
386+
const install = vi.fn(async () => providerResult);
387+
const operations = createAndroidAppDeploymentOperations({
388+
host: hostFixture({ prepareArtifact: async () => artifact }),
389+
device,
390+
signal: new AbortController().signal,
391+
install,
392+
});
393+
const result =
394+
source === 'path'
395+
? await operations.deployApp!({
396+
app: '',
397+
appPath: artifact.installablePath,
398+
replaceExisting: false,
399+
})
400+
: await operations.deployMaterializedApp!({ artifact });
401+
expect(result).toEqual({ ...providerResult, packageName: artifact.packageName });
402+
},
403+
);

‎packages/platform-android/src/deployment/runtime.ts‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
AppDeploymentInput,
3+
AppDeploymentInstaller,
34
AppDeploymentResult,
45
AppDeploymentRuntimeOperations,
56
AppUninstallInput,
@@ -45,17 +46,19 @@ export function createAndroidAppDeploymentOperations(params: {
4546
host: PlatformRuntimeHost;
4647
device: DeviceInfo;
4748
signal: AbortSignal;
49+
install?: AppDeploymentInstaller;
4850
}): Partial<AppDeploymentRuntimeOperations> {
4951
const { host, device, signal } = params;
52+
const install = params.install;
5053
const facts = androidAppDeploymentFacts(device);
5154
if (!facts.deployApp.available) return Object.freeze({});
5255
return Object.freeze({
5356
deployApp: async (input: AppDeploymentInput) =>
54-
await deployAndroidApp(host, device, input, signal),
57+
await deployAndroidApp(host, device, input, signal, install),
5558
materializeAppSource: async (input: MaterializeAppSourceInput) =>
5659
await host.androidDeployment.prepareArtifact(input, { signal }),
5760
deployMaterializedApp: async (input: DeployMaterializedAppInput) =>
58-
await deployPreparedAndroidApp(host, device, input, signal),
61+
await deployPreparedAndroidApp(host, device, input, signal, install),
5962
sendPushNotification: async (input: PushNotificationInput) =>
6063
await pushAndroidNotification(host, device, input, signal),
6164
uninstallApp: async (input: AppUninstallInput) =>
@@ -96,6 +99,7 @@ async function deployAndroidApp(
9699
device: DeviceInfo,
97100
input: AppDeploymentInput,
98101
signal: AbortSignal,
102+
install?: AppDeploymentInstaller,
99103
): Promise<AppDeploymentResult> {
100104
if (input.replaceExisting) {
101105
// Reinstall resolves its fuzzy target during uninstall. Keep the old whole-operation
@@ -113,7 +117,9 @@ async function deployAndroidApp(
113117
{ source: { kind: 'path', path: input.appPath } },
114118
{ resolveIdentity: false, signal },
115119
);
116-
await installAndroidArtifact(host, device, artifact.installablePath, undefined, signal);
120+
if (install) await install(artifact, signal);
121+
else
122+
await installAndroidArtifact(host, device, artifact.installablePath, undefined, signal);
117123
return { packageName, launchTarget: packageName };
118124
} finally {
119125
await artifact?.cleanup();
@@ -133,22 +139,8 @@ async function deployAndroidApp(
133139
{ source: { kind: 'path', path: input.appPath } },
134140
{ resolveIdentity: true, signal },
135141
);
136-
const packageName = await installAndroidArtifact(
137-
host,
138-
device,
139-
artifact.installablePath,
140-
artifact.packageName,
141-
signal,
142-
);
143-
// Plain install has historically succeeded even when the device cannot report the
144-
// installed package identity. Source installation is stricter because its response
145-
// contract promises an identity after materialization.
146-
if (!packageName) return {};
147-
return {
148-
packageName,
149-
appName: inferAndroidAppName(packageName),
150-
launchTarget: packageName,
151-
};
142+
// Plain install permits an unknown identity; source install promises one.
143+
return await installAndroidApp(host, device, artifact, signal, install);
152144
} finally {
153145
await artifact?.cleanup();
154146
}
@@ -159,24 +151,43 @@ async function deployPreparedAndroidApp(
159151
device: DeviceInfo,
160152
input: DeployMaterializedAppInput,
161153
signal: AbortSignal,
154+
install?: AppDeploymentInstaller,
162155
): Promise<AppDeploymentResult> {
163-
const packageName = await installAndroidArtifact(
164-
host,
165-
device,
166-
input.artifact.installablePath,
167-
input.artifact.packageName,
168-
signal,
169-
);
170-
if (!packageName) {
156+
const result = await installAndroidApp(host, device, input.artifact, signal, install);
157+
if (!result.packageName) {
171158
throw new AppError(
172159
'COMMAND_FAILED',
173160
'Installed Android app identity could not be resolved from the artifact or device state',
174161
);
175162
}
163+
return result;
164+
}
165+
166+
async function installAndroidApp(
167+
host: PlatformRuntimeHost,
168+
device: DeviceInfo,
169+
artifact: MaterializedAppSource,
170+
signal: AbortSignal,
171+
install?: AppDeploymentInstaller,
172+
): Promise<AppDeploymentResult> {
173+
const result = install
174+
? await install(artifact, signal)
175+
: {
176+
packageName: await installAndroidArtifact(
177+
host,
178+
device,
179+
artifact.installablePath,
180+
artifact.packageName,
181+
signal,
182+
),
183+
};
184+
const packageName = result.packageName ?? artifact.packageName;
185+
if (!packageName) return result;
176186
return {
187+
...result,
177188
packageName,
178-
appName: inferAndroidAppName(packageName),
179-
launchTarget: packageName,
189+
appName: result.appName ?? artifact.appName ?? inferAndroidAppName(packageName),
190+
launchTarget: result.launchTarget ?? packageName,
180191
};
181192
}
182193

‎packages/platform-android/src/mechanics.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,12 @@ export {
372372
type AndroidBlockingDialogObservation,
373373
type AndroidWindowDumpReader,
374374
} from './window-state.ts';
375+
376+
export async function createAndroidAppDeploymentOperations(
377+
params: Parameters<
378+
typeof import('./deployment/runtime.ts').createAndroidAppDeploymentOperations
379+
>[0],
380+
) {
381+
const { createAndroidAppDeploymentOperations: bind } = await import('./deployment/runtime.ts');
382+
return bind(params);
383+
}

0 commit comments

Comments
 (0)