Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

addWidgetAssetCatalog.cjs

70 lines2.4 KB

1"use strict";
2​
3// expo-widgets generates the widget target without a Resources build phase, and
4// hand-adding a PBXResourcesBuildPhase does NOT get picked up — xcodebuild's
5// planner never schedules `actool` for it (verified: even a full re-plan skips
6// it). So instead we add a shell-script phase that runs `actool` directly and
7// drops the compiled Assets.car into the extension bundle.
8​
9const PHASE_NAME = "Compile Widget Assets";
10​
11const ACTOOL_SCRIPT = [
12 "set -e",
13 'CATALOG="${SRCROOT}/ExpoWidgetsTarget/Assets.xcassets"',
14 'if [ ! -d "$CATALOG" ]; then',
15 ' echo "error: widget asset catalog not found at $CATALOG (expo-widgets wiped it? check plugin ordering in app.config.ts)"',
16 " exit 1",
17 "fi",
18 'DEST="${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"',
19 'mkdir -p "$DEST"',
20 'xcrun actool "$CATALOG" --compile "$DEST" --platform "${PLATFORM_NAME}" --minimum-deployment-target "${IPHONEOS_DEPLOYMENT_TARGET:-16.0}" --output-format human-readable-text',
21].join("\n");
22​
23function stripComments(map) {
24 const out = {};
25 for (const key of Object.keys(map || {})) {
26 if (key.endsWith("_comment")) continue;
27 out[key] = map[key];
28 }
29 return out;
31​
32function findByName(map, name) {
33 for (const [uuid, value] of Object.entries(stripComments(map))) {
34 if (value && value.name === name) return { uuid, value };
35 }
36 return null;
38​
39/**
40 * @param {import('xcode').XcodeProject} proj
41 * @param {{ targetName: string }} opts
42 */
43function addWidgetAssetCatalog(proj, opts) {
44 const objects = proj.hash.project.objects;
45 const target = findByName(objects.PBXNativeTarget, opts.targetName);
46 if (!target) {
47 throw new Error(
48 `addWidgetAssetCatalog: target "${opts.targetName}" not found — ` +
49 "withWidgetLogoAsset must be registered before expo-widgets so its " +
50 "xcodeproj mod runs after the widget target is created.",
51 );
52 }
53​
54 const phases = target.value.buildPhases || [];
55 const existing = objects.PBXShellScriptBuildPhase || {};
56 const already = Object.entries(stripComments(existing)).some(
57 ([uuid, value]) =>
58 value && value.name === `"${PHASE_NAME}"` && phases.some((p) => p.value === uuid),
59 );
60 if (already) return false;
61​
62 const { uuid } = proj.addBuildPhase([], "PBXShellScriptBuildPhase", PHASE_NAME, target.uuid, {
63 shellPath: "/bin/sh",
64 shellScript: ACTOOL_SCRIPT,
65 });
66 objects.PBXShellScriptBuildPhase[uuid].alwaysOutOfDate = 1;
67 return true;
69​
70module.exports = { addWidgetAssetCatalog };