// Build-setting expansion the way Xcode does it, for guards that read// `apps/cotton/project.yml` and must assert what the BUILT bundle says rather// than what the template looks like.//// What Xcode does, and so what this does://// * `$(NAME)` and `${NAME}` name a setting; the innermost reference expands// first, so `$(COTTON_APP_ID_$(COTTON_FLAVOR))` reads `COTTON_FLAVOR`,// then the setting that name spells. That nesting is the whole flavor// switch (`COTTON_FLAVOR` in `project.yml`), which is why a guard that// only grepped the YAML could not tell what Cotton Dev resolves to.// * Layers stack low → high: project base, project configuration, target// base, target configuration, then the command line (`xcodebuild …// COTTON_FLAVOR=real`) over everything.// * A value is itself expanded, recursively.//// Where it is STRICTER than Xcode, on purpose://// * A string operator (`$(VAR:identifier)`, `$(VAR:lower)`, …) THROWS.// Xcode applies them silently — `:identifier` once turned the URL scheme// into `tinmen-cotton_dev` with no build error — and a guard that// modelled them would be agreeing with the thing it is meant to doubt.// * A cycle THROWS rather than expanding to empty.// * A setting no layer defines is KEPT VERBATIM (`$(CONFIGURATION)`,// `$(PRODUCT_MODULE_NAME)` are Xcode's own and not in the spec). A guard// that needs a value fully resolved asserts no `$(` survived./** One layer of settings: name → raw value, as YAML hands them over. */exporttypeSettingsLayer = Record<string, unknown> | undefined;const REFERENCE = /\$[({]([A-Za-z_][A-Za-z0-9_]*)(:[^)}]*)?[)}]/;const MAX_DEPTH = 32;/** Merge layers low → high into one flat table of strings. */exportfunction mergeLayers(...layers: SettingsLayer[]): Record<string, string> {const out: Record<string, string> = {};for (const layer of layers) {if (!layer) continue;for (const [name, value] ofObject.entries(layer)) {if (value === undefined || value === null) continue; out[name] = String(value); } }return out;}/** * Expand every reference in `template` against `settings`, innermost first. * * Throws on a string operator and on a cycle; keeps an undefined reference * verbatim (see the file header). */exportfunction expandSettings(template: string, settings: Record<string, string>, depth = 0): string {if (depth > MAX_DEPTH) {thrownewError(`build setting expansion did not settle within ${MAX_DEPTH} levels — a cycle? (${template})`); }let out = template;const kept = newMap<string, string>();for (let guard = 0; guard < 1000; guard += 1) {// The innermost reference: one whose body holds no further `$(`.const match = innermost(out);if (!match) break;const [whole, name, operator] = match;if (operator) {thrownewError(`${whole}: a string operator rewrites the value invisibly — not allowed here`); }if (!(name in settings)) {// Kept verbatim. A placeholder stands in so the loop moves past it,// and is put back at the end.const token = `\u0000${kept.size}\u0000`; kept.set(token, whole); out = out.replace(whole, () => token);continue; }const value = expandSettings(settings[name], settings, depth + 1); out = out.replace(whole, () => value); }for (const [token, whole] of kept) out = out.split(token).join(whole);return out;}/** The first reference whose name and operator contain no nested reference. */function innermost(text: string): RegExpMatchArray | null {// Walk every `$(`/`${` start and take the first whose body closes before// another reference opens — that is the innermost one.for (let i = text.indexOf("$"); i !== -1; i = text.indexOf("$", i + 1)) {const match = text.slice(i).match(REFERENCE);if (match && match.index === 0) return match; }returnnull;}/** One target's settings, resolved for a configuration and command-line overrides. */exportinterfaceSpecShape { settings: { base: SettingsLayer; configs?: Record<string, SettingsLayer> }; targets: Record<string, { settings?: { base?: SettingsLayer; configs?: Record<string, SettingsLayer> } }>;}/** Every setting a target builds with, in `config`, with `overrides` on top. */exportfunction targetSettings( spec: SpecShape, target: string, config: string, overrides: Record<string, string> = {},): Record<string, string> {const own = spec.targets[target]?.settings;return mergeLayers(spec.settings.base, spec.settings.configs?.[config], own?.base, own?.configs?.[config], overrides);}