Skip to content

Commit 65b85a8

Browse files
fix(mobile): resolve SDK tools and recheck native inputs
1 parent 0014633 commit 65b85a8

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎scripts/mobile-native-client.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { assert, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
34
import * as Effect from "effect/Effect";
45
import * as FileSystem from "effect/FileSystem";
56
import * as Path from "effect/Path";
67
import {
8+
resolveAdb,
79
NativeClientError,
810
clientStatus,
911
ensureClient,
@@ -134,3 +136,39 @@ it.effect(
134136
);
135137
}).pipe(Effect.provide(NodeServices.layer)),
136138
);
139+
140+
it.effect("rejects native edits while reading a previously compatible binary", () =>
141+
Effect.gen(function* () {
142+
let fingerprint = "native-a";
143+
const error = yield* ensureClient({
144+
fingerprint: Effect.sync(() => fingerprint),
145+
installedBinary: Effect.sync(() => {
146+
fingerprint = "native-b";
147+
return "binary-a";
148+
}),
149+
readRecord: Effect.succeed({ fingerprint: "native-a", binary: "binary-a" }),
150+
build: Effect.die("A changed checkout must be rechecked before building"),
151+
saveRecord: () => Effect.die("Must not record changed inputs"),
152+
}).pipe(Effect.flip);
153+
assert.match(error.message, /inputs changed/);
154+
}),
155+
);
156+
157+
it.effect("finds adb in the Android SDK when PATH does not include platform-tools", () =>
158+
Effect.gen(function* () {
159+
const fs = yield* FileSystem.FileSystem;
160+
const path = yield* Path.Path;
161+
const sdk = yield* fs.makeTempDirectoryScoped({ prefix: "native-client-sdk-" });
162+
yield* fs.makeDirectory(path.join(sdk, "platform-tools"));
163+
const executable = (yield* HostProcessPlatform) === "win32" ? "adb.exe" : "adb";
164+
const adb = path.join(sdk, "platform-tools", executable);
165+
yield* fs.writeFileString(adb, "#!/bin/sh\nexit 0\n");
166+
yield* fs.chmod(adb, 0o755);
167+
assert.equal(
168+
yield* resolveAdb.pipe(
169+
Effect.provideService(HostProcessEnvironment, { PATH: "", ANDROID_HOME: sdk }),
170+
),
171+
adb,
172+
);
173+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
174+
);

‎scripts/mobile-native-client.ts‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HostProcessExecutablePath,
66
HostProcessPlatform,
77
} from "@t3tools/shared/hostProcess";
8+
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
89
import * as Console from "effect/Console";
910
import * as Crypto from "effect/Crypto";
1011
import * as Effect from "effect/Effect";
@@ -48,7 +49,7 @@ export function clientStatus(
4849
return record.fingerprint === fingerprint ? "compatible" : "stale";
4950
}
5051

51-
/** Record only a successful installation built from unchanged native inputs. */
52+
/** Keep native sources stable during ensure, as with a normal build; endpoint checks reject detected edits. */
5253
export const ensureClient = Effect.fn("ensureClient")(function* <E, R, E2, R2>(operations: {
5354
fingerprint: Effect.Effect<string, E, R>;
5455
installedBinary: Effect.Effect<string | null, E, R>;
@@ -62,19 +63,25 @@ export const ensureClient = Effect.fn("ensureClient")(function* <E, R, E2, R2>(o
6263
yield* operations.installedBinary,
6364
yield* operations.readRecord,
6465
);
65-
if (status === "compatible") return { status, rebuilt: false, fingerprint };
66-
yield* operations.build;
67-
if ((yield* operations.fingerprint) !== fingerprint) {
68-
return yield* new NativeClientError({
69-
message:
70-
"Native inputs changed during the build. Run ensure again; this build was not recorded.",
71-
});
66+
const verifyInputs = Effect.gen(function* () {
67+
if ((yield* operations.fingerprint) !== fingerprint) {
68+
return yield* new NativeClientError({
69+
message:
70+
"Native inputs changed during verification. Run ensure again; this build was not recorded.",
71+
});
72+
}
73+
});
74+
if (status === "compatible") {
75+
yield* verifyInputs;
76+
return { status, rebuilt: false, fingerprint };
7277
}
78+
yield* operations.build;
7379
const binary = yield* operations.installedBinary;
7480
if (binary === null)
7581
return yield* new NativeClientError({
7682
message: "Build finished but the development client is not installed.",
7783
});
84+
yield* verifyInputs;
7885
yield* operations.saveRecord({ fingerprint, binary });
7986
return { status: "compatible" as const, rebuilt: true, fingerprint };
8087
});
@@ -146,6 +153,22 @@ const collect = <E>(stream: Stream.Stream<Uint8Array, E>) =>
146153
(a, b) => a + b,
147154
),
148155
);
156+
export const resolveAdb = Effect.gen(function* () {
157+
if (yield* isCommandAvailable("adb")) return "adb";
158+
const environment = yield* HostProcessEnvironment;
159+
const path = yield* Path.Path;
160+
const executable = (yield* HostProcessPlatform) === "win32" ? "adb.exe" : "adb";
161+
for (const sdk of [environment.ANDROID_SDK_ROOT, environment.ANDROID_HOME]) {
162+
if (!sdk) continue;
163+
const candidate = path.join(sdk, "platform-tools", executable);
164+
if (yield* isCommandAvailable(candidate)) return candidate;
165+
}
166+
return yield* new NativeClientError({
167+
message:
168+
"adb was not found on PATH or in ANDROID_SDK_ROOT/ANDROID_HOME. Install Android SDK platform-tools.",
169+
});
170+
});
171+
149172
const command = Effect.fn("nativeClient.command")(function* (
150173
program: string,
151174
args: string[],
@@ -154,8 +177,10 @@ const command = Effect.fn("nativeClient.command")(function* (
154177
) {
155178
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
156179
const environment = yield* HostProcessEnvironment;
180+
const spawn = yield* resolveSpawnCommand(program === "adb" ? yield* resolveAdb : program, args);
157181
const child = yield* spawner.spawn(
158-
ChildProcess.make(program, args, {
182+
ChildProcess.make(spawn.command, spawn.args, {
183+
shell: spawn.shell,
159184
cwd: cwd ?? (yield* roots).mobile,
160185
env: {
161186
...environment,

0 commit comments

Comments
 (0)