|
| 1 | +// @ts-check |
| 2 | +import * as nodefs from "node:fs"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { URL, fileURLToPath } from "node:url"; |
| 5 | +import { loadAppConfig } from "../scripts/appConfig.mjs"; |
| 6 | +import { findFile, readTextFile, toVersionNumber } from "../scripts/helpers.js"; |
| 7 | +import { cp_r, mkdir_p } from "../scripts/utils/filesystem.mjs"; |
| 8 | +import { generateAssetsCatalogs } from "./assetsCatalog.mjs"; |
| 9 | +import { generateEntitlements } from "./entitlements.mjs"; |
| 10 | +import { generateInfoPlist } from "./infoPlist.mjs"; |
| 11 | +import { generateLocalizations, getProductName } from "./localizations.mjs"; |
| 12 | +import { isBridgelessEnabled, isNewArchEnabled } from "./newArch.mjs"; |
| 13 | +import { generatePrivacyManifest } from "./privacyManifest.mjs"; |
| 14 | +import { isObject, isString, projectPath } from "./utils.mjs"; |
| 15 | +import { |
| 16 | + PRODUCT_DISPLAY_NAME, |
| 17 | + PRODUCT_VERSION, |
| 18 | + applyBuildSettings, |
| 19 | + applyPreprocessorDefinitions, |
| 20 | + applySwiftFlags, |
| 21 | + applyUserHeaderSearchPaths, |
| 22 | + configureBuildSchemes, |
| 23 | + overrideBuildSettings, |
| 24 | +} from "./xcode.mjs"; |
| 25 | + |
| 26 | +/** |
| 27 | + * @import { |
| 28 | + * ApplePlatform, |
| 29 | + * JSONObject, |
| 30 | + * JSONValue, |
| 31 | + * ProjectConfiguration, |
| 32 | + * } from "../scripts/types.ts"; |
| 33 | + */ |
| 34 | + |
| 35 | +const SUPPORTED_PLATFORMS = ["ios", "macos", "visionos"]; |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {string} projectRoot |
| 39 | + * @param {string} destination |
| 40 | + * @returns {void} |
| 41 | + */ |
| 42 | +function exportNodeBinaryPath(projectRoot, destination, fs = nodefs) { |
| 43 | + const node = process.argv0; |
| 44 | + fs.writeFileSync( |
| 45 | + path.join(projectRoot, ".xcode.env"), |
| 46 | + `export NODE_BINARY='${node}'\n` |
| 47 | + ); |
| 48 | + fs.writeFileSync( |
| 49 | + path.join(destination, ".env"), |
| 50 | + `export PATH='${path.dirname(node)}':$PATH\n` |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param {JSONValue} platformConfig |
| 56 | + * @param {string} projectRoot |
| 57 | + * @param {ApplePlatform} targetPlatform |
| 58 | + * @returns {string} |
| 59 | + */ |
| 60 | +function findReactNativePath( |
| 61 | + platformConfig, |
| 62 | + projectRoot, |
| 63 | + targetPlatform, |
| 64 | + fs = nodefs |
| 65 | +) { |
| 66 | + if (isObject(platformConfig)) { |
| 67 | + const userPath = platformConfig["reactNativePath"]; |
| 68 | + if (isString(userPath)) { |
| 69 | + const p = findFile(userPath, projectRoot, fs); |
| 70 | + if (p) { |
| 71 | + return p; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const manifestURL = new URL("../package.json", import.meta.url); |
| 77 | + const manifest = JSON.parse(readTextFile(fileURLToPath(manifestURL), fs)); |
| 78 | + const npmPackageName = manifest.defaultPlatformPackages[targetPlatform]; |
| 79 | + if (!npmPackageName) { |
| 80 | + throw new Error(`Unsupported target platform: ${targetPlatform}`); |
| 81 | + } |
| 82 | + |
| 83 | + const pkg = findFile(`node_modules/${npmPackageName}`, projectRoot, fs); |
| 84 | + if (!pkg) { |
| 85 | + throw new Error(`Cannot find module '${npmPackageName}'`); |
| 86 | + } |
| 87 | + |
| 88 | + return pkg; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @param {string} p |
| 93 | + * @returns {number} |
| 94 | + */ |
| 95 | +function readPackageVersion(p, fs = nodefs) { |
| 96 | + const manifest = JSON.parse(readTextFile(path.join(p, "package.json"), fs)); |
| 97 | + return toVersionNumber(manifest["version"]); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @param {string} projectRoot |
| 102 | + * @param {ApplePlatform} targetPlatform |
| 103 | + * @param {JSONObject} options |
| 104 | + * @returns {ProjectConfiguration} |
| 105 | + */ |
| 106 | +export function generateProject( |
| 107 | + projectRoot, |
| 108 | + targetPlatform, |
| 109 | + options, |
| 110 | + fs = nodefs |
| 111 | +) { |
| 112 | + if (!SUPPORTED_PLATFORMS.includes(targetPlatform)) { |
| 113 | + throw new Error(`Unsupported platform: ${targetPlatform}`); |
| 114 | + } |
| 115 | + |
| 116 | + const appConfig = loadAppConfig(projectRoot, fs); |
| 117 | + |
| 118 | + const xcodeproj = "ReactTestApp.xcodeproj"; |
| 119 | + |
| 120 | + const xcodeprojSrc = projectPath(xcodeproj, targetPlatform); |
| 121 | + const nodeModulesDir = findFile("node_modules", projectRoot, fs); |
| 122 | + if (!nodeModulesDir) { |
| 123 | + throw new Error("Cannot not find 'node_modules' folder"); |
| 124 | + } |
| 125 | + |
| 126 | + const destination = path.join(nodeModulesDir, ".generated", targetPlatform); |
| 127 | + const xcodeprojDst = path.join(destination, xcodeproj); |
| 128 | + |
| 129 | + // Copy Xcode project files |
| 130 | + mkdir_p(destination, fs); |
| 131 | + cp_r(xcodeprojSrc, destination, fs); |
| 132 | + configureBuildSchemes(appConfig, targetPlatform, xcodeprojDst, fs); |
| 133 | + |
| 134 | + // Link source files |
| 135 | + const srcDirs = ["ReactTestApp", "ReactTestAppTests", "ReactTestAppUITests"]; |
| 136 | + for (const file of srcDirs) { |
| 137 | + fs.linkSync(projectPath(file, targetPlatform), destination); |
| 138 | + } |
| 139 | + |
| 140 | + // Shared code lives in `ios/ReactTestApp/` |
| 141 | + if (targetPlatform !== "ios") { |
| 142 | + const shared = path.join(destination, "Shared"); |
| 143 | + if (!fs.existsSync(shared)) { |
| 144 | + const source = new URL("ReactTestApp", import.meta.url); |
| 145 | + fs.linkSync(fileURLToPath(source), shared); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + generateAssetsCatalogs(appConfig, targetPlatform, destination, undefined, fs); |
| 150 | + generateEntitlements(appConfig, targetPlatform, destination, fs); |
| 151 | + generateInfoPlist(appConfig, targetPlatform, destination, fs); |
| 152 | + generatePrivacyManifest(appConfig, targetPlatform, destination, fs); |
| 153 | + generateLocalizations(appConfig, targetPlatform, destination, fs); |
| 154 | + |
| 155 | + // Note the location of Node so we can use it later in script phases |
| 156 | + exportNodeBinaryPath(projectRoot, destination, fs); |
| 157 | + |
| 158 | + const platformConfig = appConfig[targetPlatform]; |
| 159 | + const reactNativePath = findReactNativePath( |
| 160 | + platformConfig, |
| 161 | + projectRoot, |
| 162 | + targetPlatform, |
| 163 | + fs |
| 164 | + ); |
| 165 | + const reactNativeVersion = readPackageVersion(reactNativePath, fs); |
| 166 | + const useNewArch = isNewArchEnabled(options, reactNativeVersion); |
| 167 | + const useBridgeless = isBridgelessEnabled(options, reactNativeVersion); |
| 168 | + |
| 169 | + /** @type {ProjectConfiguration} */ |
| 170 | + const project = { |
| 171 | + xcodeprojPath: xcodeprojDst, |
| 172 | + reactNativePath, |
| 173 | + reactNativeVersion, |
| 174 | + useNewArch, |
| 175 | + useBridgeless, |
| 176 | + buildSettings: {}, |
| 177 | + testsBuildSettings: {}, |
| 178 | + uitestsBuildSettings: {}, |
| 179 | + }; |
| 180 | + |
| 181 | + if (isObject(platformConfig)) { |
| 182 | + applyBuildSettings(platformConfig, project, projectRoot, destination, fs); |
| 183 | + } |
| 184 | + |
| 185 | + const overrides = options["buildSettingOverrides"]; |
| 186 | + if (isObject(overrides)) { |
| 187 | + overrideBuildSettings(project.buildSettings, overrides); |
| 188 | + } |
| 189 | + |
| 190 | + project.buildSettings[PRODUCT_DISPLAY_NAME] = getProductName(appConfig); |
| 191 | + |
| 192 | + const productVersion = appConfig["version"]; |
| 193 | + project.buildSettings[PRODUCT_VERSION] = |
| 194 | + productVersion && isString(productVersion) ? productVersion : "1.0"; |
| 195 | + |
| 196 | + const singleApp = appConfig["singleApp"]; |
| 197 | + if (isString(singleApp)) { |
| 198 | + project.singleApp = singleApp; |
| 199 | + } |
| 200 | + |
| 201 | + applyPreprocessorDefinitions(project); |
| 202 | + applySwiftFlags(project); |
| 203 | + applyUserHeaderSearchPaths(project, destination); |
| 204 | + |
| 205 | + return project; |
| 206 | +} |
0 commit comments