GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — t3code/scripts/mobile-native-client.ts at main · AndryHTC/t3code Expand file tree/ Copy pathmobile-native-client.tsMore file actions 1393 lines (1351 loc) · 54 KB / Copy pathmobile-native-client.tsFile metadata and controls1393 lines (1351 loc) · 54 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell"; import * as Clock from "effect/Clock"; import * as Console from "effect/Console"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Digest from "effect/crypto/Digest"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Random from "effect/Random"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import { Argument, Command, Flag } from "effect/cli"; import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; export type NativePlatform = "ios" | "android"; const NativeClientRecord = Schema.Struct({ fingerprint: Schema.String, binary: Schema.String }); const encodeRecord = Schema.encodeEffect(Schema.fromJsonString(NativeClientRecord)); const decodeRecord = Schema.decodeUnknownEffect(Schema.fromJsonString(NativeClientRecord)); const encodeOutput = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const isNotLink = Schema.is(Schema.Struct({ code: Schema.Literal("EINVAL") })); const decodeSimulators = Schema.decodeUnknownEffect( Schema.fromJsonString( Schema.Struct({ devices: Schema.Record( Schema.String, Schema.Array( Schema.Struct({ udid: Schema.String, name: Schema.String, state: Schema.String }), ), ), }), ), ); export type NativeClientRecord = typeof NativeClientRecord.Type; export type NativeClientStatus = "compatible" | "missing" | "unknown" | "stale"; export class NativeClientError extends Schema.TaggedError<NativeClientError>()( "NativeClientError", { message: Schema.String }, ) {} export function clientStatus( fingerprint: string, binary: string | null, record: NativeClientRecord | null, ): NativeClientStatus { if (binary === null) return "missing"; if (!record || record.binary !== binary) return "unknown"; return record.fingerprint === fingerprint ? "compatible" : "stale"; } export type ArtifactSource = "local" | "remote" | "build"; /** Keep native sources stable during ensure, as with a normal build; endpoint checks reject detected edits. */ export const ensureClient = Effect.fn("ensureClient")(function* <E, R, E2, R2>(operations: { fingerprint: Effect.Effect<string, E, R>; installedBinary: Effect.Effect<string | null, E, R>; readRecord: Effect.Effect<NativeClientRecord | null, E, R>; install: (fingerprint: string) => Effect.Effect<ArtifactSource, E, R>; saveRecord: (record: NativeClientRecord) => Effect.Effect<void, E2, R2>; }) { const fingerprint = yield* operations.fingerprint; const status = clientStatus( fingerprint, yield* operations.installedBinary, yield* operations.readRecord, ); if (status === "compatible") { return { status, installed: false, fingerprint }; } const source = yield* operations.install(fingerprint); const binary = yield* operations.installedBinary; if (binary === null) return yield* new NativeClientError({ message: "Install finished but the development client is not on the device.", }); yield* operations.saveRecord({ fingerprint, binary }); return { status: "compatible" as const, installed: true, source, fingerprint }; }); const digest = Effect.fn("nativeClient.digest")(function* (value: string | Uint8Array) { const crypto = yield* Digest.Digest; const bytes = yield* crypto.digest( Digest.sha256, typeof value === "string" ? new TextEncoder().encode(value) : value, ); return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); }); /** Hash fixed-size chunks to bound memory, including resources and symlink targets. */ export const hashBundle = Effect.fn("hashBundle")(function* (root: string) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const entries: string[] = []; const visit = Effect.fn("mobile-native-client.visit")(function* ( relative: string, ): Effect.fn.Return<void, FileSystemError, FileSystem.FileSystem | Digest.Digest> { const names = (yield* fs.readDirectory(path.join(root, relative))).sort((a, b) => a.localeCompare(b, "en"), ); for (const name of names) { const key = path.join(relative, name); const absolute = path.join(root, key); // FileSystem.stat follows links; readLink distinguishes them without following a cycle. const link = yield* fs.readLink(absolute).pipe( Effect.catchIf( (error) => isNotLink(error.reason.cause), () => Effect.succeed(null), ), ); if (link !== null) { entries.push(`link:${key}:${link}`); continue; } const info = yield* fs.stat(absolute); if (info.type === "Directory") { entries.push(`directory:${key}`); yield* visit(key); } else { const chunks = yield* fs.stream(absolute, { chunkSize: 65536 }).pipe( Stream.mapEffect((chunk) => digest(chunk)), Stream.runCollect, ); entries.push(`file:${key}:${yield* digest(chunks.join("\n"))}`); } } }); yield* visit(""); return yield* digest(entries.map((entry) => `${entry.length}:${entry}`).join("")); }); type FileSystemError = import("effect/PlatformError").PlatformError; const bundleId = "com.t3tools.t3code.dev"; const roots = Effect.gen(function* () { const path = yield* Path.Path; const repo = yield* path.fromFileUrl(new URL("../", import.meta.url)); return { repo, mobile: path.join(repo, "apps/mobile") }; }); const collect = <E>(stream: Stream.Stream<Uint8Array, E>) => stream.pipe( Stream.decodeText(), Stream.runFold( () => "", (a, b) => a + b, ), ); export const resolveAdb = Effect.gen(function* () { if (yield* isCommandAvailable("adb")) return "adb"; const environment = yield* HostProcess.Environment; const path = yield* Path.Path; const executable = (yield* HostProcess.Platform) === "win32" ? "adb.exe" : "adb"; for (const sdk of [environment.ANDROID_SDK_ROOT, environment.ANDROID_HOME]) { if (!sdk) continue; const candidate = path.join(sdk, "platform-tools", executable); if (yield* isCommandAvailable(candidate)) return candidate; } return yield* new NativeClientError({ message: "adb was not found on PATH or in ANDROID_SDK_ROOT/ANDROID_HOME. Install Android SDK platform-tools.", }); }); /** Build tool output goes to this file instead of the caller's terminal; null streams it. */ const BuildLog = Context.Reference<string | null>("nativeClient/BuildLog", { defaultValue: () => null, }); const command = Effect.fn("nativeClient.command")(function* ( program: string, args: string[], inherit = false, cwd?: string, ) { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const environment = yield* HostProcess.Environment; const log = inherit ? yield* BuildLog : null; const spawn = yield* resolveSpawnCommand(program === "adb" ? yield* resolveAdb : program, args); const child = yield* spawner.spawn( ChildProcess.make(spawn.command, spawn.args, { shell: spawn.shell, cwd: cwd ?? (yield* roots).mobile, env: { ...environment, APP_VARIANT: "development", MOBILE_VERSION_POLICY: "appVersion", T3CODE_IOS_PERSONAL_TEAM: "0", CI: "1", EXPO_NO_GIT_STATUS: "1", // macOS tar otherwise adds AppleDouble entries that change the extracted bundle. COPYFILE_DISABLE: "1", }, stdin: "ignore", stdout: inherit && !log ? "inherit" : "pipe", stderr: inherit && !log ? "inherit" : "pipe", }), ); if (log) { const fs = yield* FileSystem.FileSystem; const append = <E>(stream: Stream.Stream<Uint8Array, E>) => stream.pipe(Stream.run(fs.sink(log, { flag: "a" }))); const [, , code] = yield* Effect.all( [append(child.stdout), append(child.stderr), child.exitCode], { concurrency: "unbounded" }, ); if (code === 0) return ""; const tail = (yield* fs.readFileString(log)).trimEnd().split("\n").slice(-40).join("\n"); return yield* new NativeClientError({ message: `${program} ${args[0]} failed (${code}). Last lines:\n${tail}\nFull log: ${log}`, }); } const [stdout, stderr, code] = yield* Effect.all( [collect(child.stdout), collect(child.stderr), child.exitCode], { concurrency: "unbounded" }, ); if (code !== 0) return yield* new NativeClientError({ message: `${program} ${args[0]} failed (${code}): ${stderr || "see build output"}`, }); return stdout.trim(); }, Effect.scoped); const fingerprint = Effect.fn("nativeClient.fingerprint")(function* (platform: NativePlatform) { const output = yield* command(yield* HostProcess.ExecutablePath, [ "--eval", // Same hash on every machine: generated native projects and the config's `extra` (env-specific // values a dev client reads from Metro) stay out, so a Mac and a Linux VM share builds. `const f = require('expo/fingerprint'); f.createFingerprintAsync(process.cwd(), { platforms: [process.argv[1]], silent: true, ignorePaths: ['android/**', 'ios/**'], sourceSkips: f.DEFAULT_SOURCE_SKIPS | f.SourceSkips.ExpoConfigExtraSection }).then(fp => console.log('T3_NATIVE_FINGERPRINT=' + fp.hash)).catch(e => { console.error(e); process.exitCode = 1; });`, platform, ]); const hash = output .split("\n") .find((line) => line.startsWith("T3_NATIVE_FINGERPRINT=")) ?.split("=")[1]; if (!hash || !/^[a-f0-9]{40,64}$/.test(hash)) return yield* new NativeClientError({ message: "Expo did not return a native fingerprint." }); return hash; }); const runtimeName = (runtime: string) => runtime .replace(/^com\.apple\.CoreSimulator\.SimRuntime\./, "") .replace(/-(\d+)-(\d+)$/, " $1.$2"); /** Validate the named device, or pick the only booted one; otherwise list exact commands to choose from. */ const resolveDevice = Effect.fn("nativeClient.resolveDevice")(function* ( platform: NativePlatform, requested: string | undefined, ) { const choose = (candidates: ReadonlyArray<{ id: string; label: string }>, problem: string) => new NativeClientError({ message: [ problem, ...candidates.map( ({ id, label }) => ` vp run mobile:install ${platform} ${id} # ${label}`, ), ].join("\n"), }); if (platform === "ios") { if ((yield* HostProcess.Platform) !== "darwin") return yield* new NativeClientError({ message: "Run iOS installs on the Mac that hosts the simulator.", }); // The shared iOS client is an arm64 simulator build; Intel simulators cannot run it. if ((yield* HostProcess.Architecture) !== "arm64") return yield* new NativeClientError({ message: "mobile:install needs an Apple silicon Mac. Use vp run ios:dev on Intel Macs.", }); const listing = yield* decodeSimulators( yield* command("xcrun", ["simctl", "list", "devices", "available", "--json"]), ); const simulators = Object.entries(listing.devices).flatMap(([runtime, devices]) => devices.map((device) => ({ ...device, label: `${device.name} (${runtimeName(runtime)})` })), ); const booted = simulators .filter((simulator) => simulator.state === "Booted") .map(({ udid, label }) => ({ id: udid, label })); if (requested === undefined) { if (booted.length === 1) return booted[0]!; return yield* booted.length === 0 ? new NativeClientError({ message: "No iOS simulator is booted. Open one with device_open, or boot one: xcrun simctl boot <udid>", }) : choose(booted, "Several iOS simulators are booted. Pick one:"); } const simulator = simulators.find((entry) => entry.udid === requested); if (!simulator) return yield* choose(booted, `No available iOS simulator with UDID ${requested}. Booted:`); if (simulator.state !== "Booted") return yield* new NativeClientError({ message: `Boot the selected simulator first: xcrun simctl boot ${requested}`, }); return { id: simulator.udid, label: simulator.label }; } const attached = (yield* command("adb", ["devices", "-l"])) .split("\n") .slice(1) .map((line) => line.trim().split(/\s+/)) .filter((parts) => parts.length >= 2) .map(([id, state, ...details]) => ({ id: id!, state: state!, label: details.find((detail) => detail.startsWith("model:"))?.slice(6) ?? id!, })); const ready = attached.filter((device) => device.state === "device"); const device = requested === undefined ? ready : attached.filter((entry) => entry.id === requested); if (requested === undefined && ready.length !== 1) return yield* ready.length === 0 ? new NativeClientError({ message: "No Android emulator or phone is connected. Open one with device_open, or start an emulator.", }) : choose(ready, "Several Android devices are connected. Pick one:"); const selected = device[0]; if (!selected) return yield* choose(ready, `Android device ${requested} is not connected. Ready:`); if (selected.state === "unauthorized") return yield* new NativeClientError({ message: `Unlock ${selected.label} and accept the USB debugging prompt, then run this again.`, }); if (selected.state !== "device") return yield* new NativeClientError({ message: `Android device ${selected.id} is ${selected.state}, not ready.`, }); // Emulators report a generic model; their AVD name is what users recognize. const avd = selected.id.startsWith("emulator-") ? yield* command("adb", ["-s", selected.id, "emu", "avd", "name"]).pipe( Effect.map((output) => output.split("\n")[0]?.trim()), Effect.orElseSucceed(() => undefined), ) : undefined; return { id: selected.id, label: avd || selected.label }; }); export const installedBinary = Effect.fn("installedBinary")(function* ( platform: NativePlatform, device: string, run: typeof command = command, ) { if (platform === "ios") { const apps = yield* run("xcrun", ["simctl", "listapps", device]); if (!apps.includes(`"${bundleId}"`)) return null; return yield* hashBundle( yield* run("xcrun", ["simctl", "get_app_container", device, bundleId, "app"]), ); } const installed = yield* run("adb", ["-s", device, "shell", "pm", "list", "packages", bundleId]); if (!installed.split("\n").some((line) => line.trim() === `package:${bundleId}`)) return null; const packages = yield* run("adb", ["-s", device, "shell", "pm", "path", bundleId]); const apks = packages .split("\n") .filter((line) => line.startsWith("package:")) .map((line) => line.slice(8).trim()) .sort(); if (apks.length === 0) return null; const hashes = yield* Effect.forEach(apks, (apk) => Effect.gen(function* () { if (!/^\/[\w/+=.~-]+\.apk$/.test(apk)) return yield* new NativeClientError({ message: "Unexpected installed APK path." }); const hash = (yield* run("adb", ["-s", device, "shell", "sha256sum", apk])).split(/\s/)[0]; if (!hash || !/^[a-f0-9]{64}$/.test(hash)) return yield* new NativeClientError({ message: "Could not hash installed APK." }); return hash; }), ); return yield* digest(hashes.sort().join("\n")); }); /** Preserve input-invalidated Gradle/.cxx caches; exclude app/build because its tasks omit inputs. Ignore cache failures. */ export const prebuildAndroid = Effect.fn("nativeClient.prebuildAndroid")(function* <E, R>( mobile: string, prebuild: Effect.Effect<void, E, R>, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const android = path.join(mobile, "android"); // Sibling renames stay on one filesystem; interrupted runs leave this gitignored cache for next-run cleanup. const saved = path.join(mobile, ".android-cache"); const caches = [".gradle", "app/.cxx"]; const clear = fs.remove(saved, { recursive: true, force: true }).pipe(Effect.ignore); const move = (from: string, to: string) => Effect.forEach( caches, (cache) => Effect.gen(function* () { const source = path.join(from, cache); const target = path.join(to, cache); if (!(yield* fs.exists(source)) || (yield* fs.exists(target))) return; yield* fs.makeDirectory(path.dirname(target), { recursive: true }); yield* fs.rename(source, target); }).pipe(Effect.ignore), { discard: true }, ); yield* Effect.acquireUseRelease( clear.pipe(Effect.andThen(move(android, saved))), () => prebuild, () => move(saved, android).pipe(Effect.andThen(clear)), ); }); const artifactNames = { ios: "T3CodeDev.app", android: "app-debug.apk" } as const; // R2 requires equal-sized parts and Workers cap request bodies at 100 MB. const partSize = 64 * 1024 * 1024; const RemoteCredentials = Schema.fromJsonString( Schema.Record(Schema.String, Schema.Struct({ name: Schema.String, token: Schema.String })), ); const decodeCredentials = Schema.decodeUnknownEffect(RemoteCredentials); const decodeEndpoint = Schema.decodeUnknownEffect( Schema.fromJsonString(Schema.Struct({ endpoint: Schema.String })), ); const decodeUpload = Schema.decodeUnknownEffect( Schema.Union([ Schema.Struct({ exists: Schema.Literal(true) }), Schema.Struct({ uploadId: Schema.String }), ]), ); const decodePart = Schema.decodeUnknownEffect(Schema.Struct({ etag: Schema.String })); const decodeEasBuilds = Schema.decodeUnknownEffect( Schema.fromJsonString( Schema.Array(Schema.Struct({ artifacts: Schema.Struct({ buildUrl: Schema.String }) })), ), ); interface RemoteCache { readonly url: string; readonly telemetry: string; readonly name: string; readonly token: string; } /** The shared task-cache login (infra/task-cache/install.sh) also authorizes native clients. */ const remoteCache = Effect.fn("nativeClient.remoteCache")( function* (home: string) { const environment = yield* HostProcess.Environment; if (environment.T3_NATIVE_CLIENT_CACHE === "off") return null; const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const { endpoint } = yield* decodeEndpoint( yield* fs.readFileString(path.join((yield* roots).repo, ".task-cache.json")), ); const credentials = yield* decodeCredentials( yield* fs.readFileString(path.join(home, ".config/t3-task-cache/credentials.json")), ); const login = credentials[endpoint]; if (!login) return null; const base = endpoint.replace(/\/$/, ""); return { url: `${base}/native-clients`, telemetry: `${base}/telemetry`, ...login, } satisfies RemoteCache; }, Effect.orElseSucceed(() => null), ); const ReaderLogin = Schema.fromJsonString( Schema.Struct({ name: Schema.String, token: Schema.String }), ); /** * A URL another machine can download `key` from. The header goes to that machine, so only a * read-only login (`ci-reader`) is ever handed out, never a developer's writer token. */ const sharedSource = Effect.fn("nativeClient.sharedSource")( function* (home: string, key: string) { const remote = yield* remoteCache(home); if (!remote) return null; const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const reader = remote.name === "ci-reader" ? remote : yield* fs .readFileString(path.join(home, ".config/t3-task-cache/ci-reader.json")) .pipe(Effect.flatMap(Schema.decodeUnknownEffect(ReaderLogin))); const basic = Buffer.from(`${reader.name}:${reader.token}`).toString("base64"); return { url: `${remote.url}/${key}`, header: `authorization:Basic ${basic}` }; }, Effect.orElseSucceed(() => null), ); const authorized = (remote: RemoteCache, request: HttpClientRequest.HttpClientRequest) => request.pipe(HttpClientRequest.basicAuth(remote.name, remote.token)); // Build duration lives beside the artifact so hits can report the time they saved. const sidecar = (directory: string) => `${directory}.json`; const decodeSidecar = Schema.decodeUnknownEffect( Schema.fromJsonString(Schema.Struct({ buildMs: Schema.Number })), ); const readBuildMs = (directory: string) => FileSystem.FileSystem.pipe( Effect.flatMap((fs) => fs.readFileString(sidecar(directory))), Effect.flatMap(decodeSidecar), Effect.map(({ buildMs }) => buildMs), Effect.orElseSucceed(() => 0), ); const writeBuildMs = (directory: string, buildMs: number) => FileSystem.FileSystem.pipe( Effect.flatMap((fs) => fs.writeFileString(sidecar(directory), JSON.stringify({ buildMs }))), Effect.ignore, ); /** Download into a sibling staging directory and publish only after the digest matches. */ const download = Effect.fn("nativeClient.download")(function* ( remote: RemoteCache, platform: NativePlatform, key: string, target: string, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const client = yield* HttpClient.HttpClient; const response = yield* client.execute( authorized(remote, HttpClientRequest.get(`${remote.url}/${key}`)), ); if (response.status === 404) return false; yield* HttpClientResponse.filterStatusOk(response); const expected = response.headers["x-t3-digest"] ?? ""; if (!/^[a-f0-9]{64}$/.test(expected)) return yield* new NativeClientError({ message: "Cached native client has no digest." }); yield* Console.error(`Downloading native client ${key} from the shared cache...`); const staging = yield* fs.makeTempDirectory({ directory: path.dirname(target), prefix: ".download-", }); return yield* Effect.gen(function* () { if (platform === "ios") { const archive = `${staging}.tgz`; yield* response.stream.pipe( Stream.run(fs.sink(archive)), Effect.andThen(command("tar", ["-xzf", archive, "-C", staging])), Effect.ensuring(fs.remove(archive, { force: true }).pipe(Effect.ignore)), ); } else { yield* response.stream.pipe(Stream.run(fs.sink(path.join(staging, artifactNames.android)))); } if ((yield* hashBundle(staging)) !== expected) return yield* new NativeClientError({ message: "Cached native client failed verification." }); yield* publish(staging, target); const buildMs = Number(response.headers["x-t3-build-ms"] ?? 0); yield* writeBuildMs(target, Number.isSafeInteger(buildMs) ? buildMs : 0); return true; }).pipe( Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)), ); }); /** Upload in fixed-size parts; any failure aborts the upload and leaves the cache unchanged. */ const upload = Effect.fn("nativeClient.upload")(function* ( remote: RemoteCache, platform: NativePlatform, key: string, artifact: string, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk); const created = yield* client .execute( authorized(remote, HttpClientRequest.post(`${remote.url}/${key}/uploads`)).pipe( HttpClientRequest.setHeader("x-t3-digest", yield* hashBundle(artifact)), HttpClientRequest.setHeader("x-t3-build-ms", String(yield* readBuildMs(artifact))), ), ) .pipe( Effect.flatMap((response) => response.json), Effect.flatMap(decodeUpload), ); if ("exists" in created) return; const uploadUrl = `${remote.url}/${key}/uploads/${encodeURIComponent(created.uploadId)}`; yield* Console.error(`Uploading native client ${key} to the shared cache...`); yield* Effect.gen(function* () { const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-upload-" }); const file = platform === "ios" ? path.join(directory, "client.tgz") : path.join(artifact, artifactNames.android); if (platform === "ios") yield* command("tar", ["-czf", file, "-C", artifact, artifactNames.ios]); const handle = yield* fs.open(file); const parts: Array<{ partNumber: number; etag: string }> = []; for (let partNumber = 1; ; partNumber++) { const buffer = new Uint8Array(partSize); let filled = 0; while (filled < partSize) { const read = Number(yield* handle.read(buffer.subarray(filled))); if (read === 0) break; filled += read; } if (filled === 0) break; const { etag } = yield* client .execute( authorized(remote, HttpClientRequest.put(`${uploadUrl}/${partNumber}`)).pipe( HttpClientRequest.bodyUint8Array(buffer.subarray(0, filled)), ), ) .pipe( Effect.flatMap((response) => response.json), Effect.flatMap(decodePart), ); parts.push({ partNumber, etag }); if (filled < partSize) break; } yield* client.execute( authorized(remote, HttpClientRequest.post(`${uploadUrl}/complete`)).pipe( HttpClientRequest.bodyJsonUnsafe({ parts }), ), ); }).pipe( Effect.scoped, Effect.onError(() => client.execute(authorized(remote, HttpClientRequest.delete(uploadUrl))).pipe(Effect.ignore), ), ); }); const publish = Effect.fn("nativeClient.publish")(function* (staging: string, target: string) { const fs = yield* FileSystem.FileSystem; // A concurrent ensure may have published the same fingerprint first; either copy is valid. if (yield* fs.exists(target)) return; yield* fs.rename(staging, target); }); /** Xcode builds iOS locally; without it (Linux, or a Mac with no Xcode) EAS builds in the cloud. */ const hasXcode = Effect.gen(function* () { if ((yield* HostProcess.Platform) !== "darwin") return false; return yield* command("xcrun", ["--find", "xcodebuild"]).pipe( Effect.as(true), Effect.orElseSucceed(() => false), ); }); /** Without Xcode, EAS builds the same simulator client from this checkout. */ const buildIosOnEas = Effect.fn("nativeClient.buildIosOnEas")(function* (staging: string) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; // Fail before any work when neither builder is available. yield* command("npx", ["-y", easCli, "whoami"]).pipe( Effect.mapError( () => new NativeClientError({ message: "iOS builds need Xcode on macOS, or an EAS login (`npx eas-cli login`, or EXPO_TOKEN) to build in the cloud.", }), ), ); yield* Console.error("No Xcode here; building the iOS simulator client on EAS (10-30 min)."); const [build] = yield* decodeEasBuilds( yield* command("npx", [ "-y", easCli, "build", "--platform", "ios", "--profile", "development-simulator", "--non-interactive", "--wait", "--json", ]), ); if (!build) return yield* new NativeClientError({ message: "EAS returned no build." }); const archive = path.join(staging, "eas-build.tar.gz"); yield* command("curl", ["-fsSL", "--retry", "3", "-o", archive, build.artifacts.buildUrl]); yield* command("tar", ["-xzf", archive, "-C", staging]); yield* fs.remove(archive); const app = (yield* fs.readDirectory(staging)).find((name) => name.endsWith(".app")); if (!app) return yield* new NativeClientError({ message: "The EAS build archive has no .app." }); if (app !== artifactNames.ios) yield* fs.rename(path.join(staging, app), path.join(staging, artifactNames.ios)); }); const build = Effect.fn("nativeClient.build")(function* ( platform: NativePlatform, abi: string | null, target: string, expected: string, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const { repo, mobile } = yield* roots; const tracked = yield* command("git", ["ls-files", `apps/mobile/${platform}`], false, repo); if (tracked) return yield* new NativeClientError({ message: "Native directory contains tracked files; clean prebuild would overwrite them.", }); const prebuild = command( "vp", ["exec", "expo", "prebuild", "--clean", "--platform", platform, "--no-install"], true, ).pipe(Effect.asVoid); const onEas = platform === "ios" && !(yield* hasXcode); if (platform === "android") yield* prebuildAndroid(mobile, prebuild); else if (!onEas) yield* prebuild; const staging = yield* fs.makeTempDirectory({ directory: path.dirname(target), prefix: ".build-", }); yield* Effect.gen(function* () { if (onEas) { yield* buildIosOnEas(staging); } else if (platform === "ios") { const output = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-client-" }); yield* command("pod", ["install"], true, path.join(mobile, "ios")); // One device-independent arm64 simulator build installs on every iPhone and iPad simulator. yield* command( "xcrun", [ "xcodebuild", "-workspace", path.join(mobile, "ios/T3CodeDev.xcworkspace"), "-scheme", "T3CodeDev", "-configuration", "Debug", "-destination", "generic/platform=iOS Simulator", "-derivedDataPath", output, "ARCHS=arm64", "ONLY_ACTIVE_ARCH=NO", "build", ], true, ); yield* command("ditto", [ path.join(output, "Build/Products/Debug-iphonesimulator", artifactNames.ios), path.join(staging, artifactNames.ios), ]); } else { const android = path.join(mobile, "android"); yield* command( path.join(android, (yield* HostProcess.Platform) === "win32" ? "gradlew.bat" : "gradlew"), ["app:assembleDebug", `-PreactNativeArchitectures=${abi}`], true, android, ); yield* fs.copyFile( path.join(android, "app/build/outputs/apk/debug", artifactNames.android), path.join(staging, artifactNames.android), ); } // Native edits during the build would publish this binary under a fingerprint it doesn't match. if ((yield* fingerprint(platform)) !== expected) return yield* new NativeClientError({ message: "Native inputs changed during the build, so it was discarded. Run this again.", }); yield* publish(staging, target); }).pipe( Effect.scoped, Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)), ); }); /** Keep the newest few fingerprints per platform; each artifact is a few hundred megabytes. */ const prune = Effect.fn("nativeClient.prune")( function* (parent: string, keep: string) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const entries = yield* Effect.forEach( (yield* fs.readDirectory(parent)).filter( (name) => !name.startsWith(".") && !name.endsWith(".json"), ), (name) => fs.stat(path.join(parent, name)).pipe( Effect.map((info) => ({ name, modified: Option.getOrUndefined(info.mtime)?.getTime() ?? 0, })), ), ); const stale = entries .filter(({ name }) => name !== keep) .sort((a, b) => b.modified - a.modified) .slice(2); yield* Effect.forEach( stale, ({ name }) => fs .remove(path.join(parent, name), { recursive: true, force: true }) .pipe(Effect.andThen(fs.remove(sidecar(path.join(parent, name)), { force: true }))), { discard: true }, ); }, (effect) => Effect.ignore(effect), ); const buildWorkflow = "mobile-dev-client.yml"; const WorkflowRun = Schema.Struct({ displayTitle: Schema.String, status: Schema.String, conclusion: Schema.NullOr(Schema.String), createdAt: Schema.String, }); const decodeWorkflowRuns = Schema.decodeUnknownEffect( Schema.fromJsonString(Schema.Array(WorkflowRun)), ); /** The workflow names each run after the client it builds, which is how a waiting machine finds it. */ export const buildRunTitle = (platform: NativePlatform, abi: string | null, fingerprint: string) => `Dev client ${platform}${abi ? ` ${abi}` : ""} ${fingerprint}`; export type RemoteBuildState = "none" | "running" | "failed" | "succeeded"; /** The newest run with this title that was created at or after `sinceMs`. */ export const remoteBuildState = ( runs: ReadonlyArray<typeof WorkflowRun.Type>, title: string, sinceMs: number, ): RemoteBuildState => { const run = runs .filter((run) => run.displayTitle === title && Date.parse(run.createdAt) >= sinceMs) .sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))[0]; if (!run) return "none"; if (run.status !== "completed") return "running"; return run.conclusion === "success" ? "succeeded" : "failed"; }; const remoteBuildPollMs = 20_000; const remoteBuildTimeoutMs = 25 * 60_000; /** A run this recent for the same client is joined instead of dispatching a second one. */ const remoteBuildJoinMs = 30 * 60_000; /** * Build the client for HEAD in the `mobile-dev-client.yml` workflow (Blacksmith runners with warm * Gradle and ccache state) and download it from the shared cache. False means "build here * instead": no `gh`, no permission to dispatch, HEAD not on GitHub, native edits that HEAD does * not have (the workflow rejects a fingerprint it does not compute), or a failed run. */ const buildOnBlacksmith = Effect.fn("nativeClient.buildOnBlacksmith")( function* ( remote: RemoteCache, platform: NativePlatform, abi: string | null, fingerprint: string, key: string, directory: string, ) { const environment = yield* HostProcess.Environment; // The workflow itself runs this script: it must build, never dispatch. if (environment.GITHUB_ACTIONS === "true" || environment.T3_NATIVE_CLIENT_BUILD === "local") return false; if (!(yield* isCommandAvailable("gh"))) return false; const repository = environment.T3_NATIVE_CLIENT_REPOSITORY ?? "pingdotgg/t3code"; const { repo } = yield* roots; const sha = yield* command("git", ["rev-parse", "HEAD"], false, repo); const pushed = yield* command( "gh", ["api", `repos/${repository}/commits/${sha}`, "--jq", ".sha"], false, repo, ).pipe( Effect.map((found) => found === sha), Effect.orElseSucceed(() => false), ); if (!pushed) { yield* Console.error(`HEAD ${sha.slice(0, 10)} is not on GitHub yet; building here.`); return false; } const title = buildRunTitle(platform, abi, fingerprint); const state = (sinceMs: number) => command( "gh", [ "run", "list", "--repo", repository, "--workflow", buildWorkflow, "--event", "workflow_dispatch", "--limit", "30", "--json", "displayTitle,status,conclusion,createdAt", ], false, repo, ).pipe( Effect.flatMap(decodeWorkflowRuns), Effect.map((runs) => remoteBuildState(runs, title, sinceMs)), // A failed listing says nothing about the build; keep waiting on the cache. Effect.orElseSucceed((): RemoteBuildState => "none"), ); const started = yield* Clock.currentTimeMillis; const joined = (yield* state(started - remoteBuildJoinMs)) === "running"; if (!joined) { const dispatched = yield* command( "gh", [ "workflow", "run", buildWorkflow, "--repo", repository, "--ref", "main", "-f", `ref=${sha}`, "-f", `platform=${platform}`, "-f", `abi=${abi ?? "x86_64"}`, "-f", `fingerprint=${fingerprint}`, ], false, repo, ).pipe( Effect.as(true), Effect.orElseSucceed(() => false), ); if (!dispatched) return false; } yield* Console.error( `${joined ? "A Blacksmith build of" : "Building"} ${key} ${joined ? "is already running" : "on Blacksmith"}; waiting for it in the shared cache...`, ); const since = joined ? started - remoteBuildJoinMs : started - 60_000; const fetched = download(remote, platform, key, directory).pipe( Effect.orElseSucceed(() => false), ); while ((yield* Clock.currentTimeMillis) - started < remoteBuildTimeoutMs) { yield* Effect.sleep(remoteBuildPollMs); if (yield* fetched) return true; const now = yield* state(since); if (now === "failed" || (now === "succeeded" && !(yield* fetched))) { yield* Console.error( now === "failed" ? "The Blacksmith build failed (or HEAD lacks this checkout's native edits); building here." : "The Blacksmith run finished without sharing this client; building here.", ); return false; } } yield* Console.error("The Blacksmith build did not finish in time; building here."); return false; }, Effect.orElseSucceed(() => false), ); /** Local artifact, then the shared cache, then a Blacksmith build when this user can start one, then a build here that is shared back when credentials allow. */ export const resolveArtifact = Effect.fn("nativeClient.resolveArtifact")(function* ( home: string, platform: NativePlatform, abi: string | null, fingerprint: string, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const key = `${platform}/${abi ? `development-${abi}` : "development"}/${fingerprint}`; const directory = path.join(home, ".cache/t3code/native-clients/artifacts", key); const artifact = { directory, file: path.join(directory, artifactNames[platform]) }; if (yield* fs.exists(artifact.file)) { yield* Console.error(`Using cached client ${key}.`); // Pruning keeps the most recently used, so a client another checkout is installing stays. const now = yield* Clock.currentTimeMillis; yield* fs.utimes(directory, now / 1000, now / 1000).pipe(Effect.ignore); return { ...artifact, source: "local" as const, buildMs: yield* readBuildMs(directory) }; } yield* fs.makeDirectory(path.dirname(directory), { recursive: true }); const remote = yield* remoteCache(home); if (remote) { const downloaded = yield* download(remote, platform, key, directory).pipe( Effect.catch((error) => Console.error(`Shared native client cache unavailable (${describe(error)}).`).pipe( Effect.as(false), ), ), ); if (downloaded) { yield* prune(path.dirname(directory), fingerprint); return { ...artifact, source: "remote" as const, buildMs: yield* readBuildMs(directory) }; } } if (remote && (yield* buildOnBlacksmith(remote, platform, abi, fingerprint, key, directory))) { yield* prune(path.dirname(directory), fingerprint); return { ...artifact, source: "remote" as const, buildMs: yield* readBuildMs(directory) }; } const log = yield* BuildLog; yield* Console.error( `No cached client for ${key}. Building it here once for every machine (5-15 min)${log ? `. Log: ${log}` : ""}`, ); const started = yield* Clock.currentTimeMillis; yield* build(platform, abi, directory, fingerprint); yield* writeBuildMs(directory, (yield* Clock.currentTimeMillis) - started); yield* prune(path.dirname(directory), fingerprint); if (remote && (yield* HostProcess.Environment).T3_NATIVE_CLIENT_CACHE !== "read") { // The Blacksmith workflow exists to share the client: there a failed upload fails the run. const required = (yield* HostProcess.Environment).T3_NATIVE_CLIENT_SHARE === "required"; yield* upload(remote, platform, key, directory).pipe( Effect.catch((error) => |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |