Skip to content

Commit de13205

Browse files
committed
refactor(apple): move host and autolinking resolution to JS
1 parent 0d6913a commit de13205

File tree

6 files changed

+104
-26
lines changed

6 files changed

+104
-26
lines changed

ios/app.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isMain,
99
readTextFile,
1010
toVersionNumber,
11+
v,
1112
} from "../scripts/helpers.js";
1213
import { cp_r, mkdir_p, rm_r } from "../scripts/utils/filesystem.mjs";
1314
import { generateAssetsCatalogs } from "./assetsCatalog.mjs";
@@ -66,6 +67,45 @@ function exportNodeBinaryPath(projectRoot, destination, fs = nodefs) {
6667
);
6768
}
6869

70+
/**
71+
* @param {string} reactNativePath
72+
* @param {number} reactNativeVersion
73+
* @returns {string | undefined}
74+
*/
75+
function findCommunityAutolinkingScriptPath(
76+
reactNativePath,
77+
reactNativeVersion,
78+
fs = nodefs
79+
) {
80+
// As of 0.75, we should use `use_native_modules!` from `react-native` instead
81+
if (reactNativeVersion < v(0, 75, 0)) {
82+
const pkgPath = findFile(
83+
"node_modules/@react-native-community/cli-platform-ios",
84+
reactNativePath,
85+
fs
86+
);
87+
if (pkgPath) {
88+
return path.join(pkgPath, "native_modules.rb");
89+
}
90+
}
91+
92+
return undefined;
93+
}
94+
95+
/**
96+
* @param {string} projectRoot
97+
* @returns {string}
98+
*/
99+
function findReactNativeHostPath(projectRoot, fs = nodefs) {
100+
const dir = fileURLToPath(import.meta.url);
101+
const rnhPath = findFile("node_modules/@rnx-kit/react-native-host", dir, fs);
102+
if (!rnhPath) {
103+
throw new Error("Cannot find module '@rnx-kit/react-native-host'");
104+
}
105+
106+
return path.relative(projectRoot, rnhPath);
107+
}
108+
69109
/**
70110
* @param {JSONValue} platformConfig
71111
* @param {string} projectRoot
@@ -188,6 +228,12 @@ export function generateProject(
188228
xcodeprojPath: path.resolve(xcodeprojDst),
189229
reactNativePath: path.resolve(reactNativePath),
190230
reactNativeVersion,
231+
reactNativeHostPath: findReactNativeHostPath(projectRoot, fs),
232+
communityAutolinkingScriptPath: findCommunityAutolinkingScriptPath(
233+
reactNativePath,
234+
reactNativeVersion,
235+
fs
236+
),
191237
useNewArch,
192238
useBridgeless,
193239
buildSettings: {},

ios/pod_helpers.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ def resolve_module(request, start_dir = Pod::Config.instance.installation_root)
5757
@module_cache[request] = resolve_module_uncached(request, start_dir).to_s
5858
end
5959

60-
def resolve_module_relative(request)
61-
path = resolve_module_uncached(request, Pathname.new(__dir__))
62-
path.relative_path_from(Pod::Config.instance.installation_root).to_s
63-
end
64-
6560
def resolve_module_uncached(request, start_dir)
6661
# Always resolve `start_dir` as it may be a symlink
6762
package_json = find_file("node_modules/#{request}/package.json", start_dir.realdirpath)
@@ -81,10 +76,6 @@ def supports_new_architecture?(react_native_version)
8176
react_native_version.zero? || react_native_version >= v(0, 71, 0)
8277
end
8378

84-
def try_pod(name, podspec, project_root)
85-
pod name, :podspec => podspec if File.exist?(File.join(project_root, podspec))
86-
end
87-
8879
def use_hermes?(options)
8980
use_hermes = ENV.fetch('USE_HERMES', nil)
9081
return use_hermes == '1' unless use_hermes.nil?

ios/test_app.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ def apply_config_plugins(project_root, target_platform)
2626
raise 'Failed to apply config plugins' unless result
2727
end
2828

29-
def autolink_script_path(project_root, react_native_path, react_native_version)
30-
start_dir = if react_native_version >= v(0, 76, 0)
31-
project_root
32-
else
33-
react_native_path
34-
end
35-
package_path = resolve_module('@react-native-community/cli-platform-ios', start_dir)
36-
File.join(package_path, 'native_modules')
37-
end
38-
3929
def target_product_type(target)
4030
target.product_type if target.respond_to?(:product_type)
4131
end
@@ -175,6 +165,8 @@ def make_project!(project_root, target_platform, options)
175165
},
176166
:react_native_path => project['reactNativePath'],
177167
:react_native_version => project['reactNativeVersion'],
168+
:react_native_host_path => project['reactNativeHostPath'],
169+
:community_autolinking_script_path => project['communityAutolinkingScriptPath'],
178170
:use_new_arch => project['useNewArch'],
179171
:code_sign_identity => build_settings[CODE_SIGN_IDENTITY] || '',
180172
:development_team => build_settings[DEVELOPMENT_TEAM] || '',
@@ -198,8 +190,8 @@ def use_test_app_internal!(target_platform, options)
198190
end
199191

200192
# As of 0.75, we should use `use_native_modules!` from `react-native` instead
201-
if react_native_version < v(0, 75, 0)
202-
require_relative(autolink_script_path(project_root, react_native_path, react_native_version))
193+
if project_target[:community_autolinking_script_path].is_a? String
194+
require_relative(project_target[:community_autolinking_script_path])
203195
end
204196

205197
begin
@@ -220,7 +212,7 @@ def use_test_app_internal!(target_platform, options)
220212
react_native_version,
221213
options)
222214

223-
pod 'ReactNativeHost', :path => resolve_module_relative('@rnx-kit/react-native-host')
215+
pod 'ReactNativeHost', :path => project_target[:react_native_host_path]
224216

225217
if (resources_pod_path = resources_pod(project_root, target_platform, platforms))
226218
pod 'ReactTestApp-Resources', :path => resources_pod_path

scripts/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export type ProjectConfiguration = {
136136
xcodeprojPath: string;
137137
reactNativePath: string;
138138
reactNativeVersion: number;
139+
reactNativeHostPath: string;
140+
communityAutolinkingScriptPath?: string;
139141
singleApp?: string;
140142
useNewArch: boolean;
141143
useBridgeless: boolean;

test/ios/app.test.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ describe("generateProject()", macosOnly, () => {
2323
return generateProjectActual(projectRoot, platform, options, mockfs);
2424
}
2525

26-
function makeMockProject(overrides?: Record<string, unknown>) {
26+
function makeMockProject(
27+
overrides?: Record<string, unknown>,
28+
reactNativeVersion = "1000.0.0"
29+
) {
2730
const manifestURL = new URL("../../package.json", import.meta.url);
2831
const manifest = readTextFile(fileURLToPath(manifestURL));
2932
const { name, version, defaultPlatformPackages } = JSON.parse(manifest);
@@ -58,15 +61,21 @@ describe("generateProject()", macosOnly, () => {
5861
"node_modules/@callstack/react-native-visionos/package.json":
5962
JSON.stringify({
6063
name: "@callstack/react-native-visionos",
61-
version: "1000.0.0",
64+
version: reactNativeVersion,
6265
}),
66+
"node_modules/@react-native-community/cli-platform-ios/native_modules.rb":
67+
"",
68+
"node_modules/@rnx-kit/react-native-host/package.json": JSON.stringify({
69+
name: "@rnx-kit/react-native-host",
70+
version: reactNativeVersion,
71+
}),
6372
"node_modules/react-native/package.json": JSON.stringify({
6473
name: "react-native",
65-
version: "1000.0.0",
74+
version: reactNativeVersion,
6675
}),
6776
"node_modules/react-native-macos/package.json": JSON.stringify({
6877
name: "react-native-macos",
69-
version: "1000.0.0",
78+
version: reactNativeVersion,
7079
}),
7180
};
7281
}
@@ -214,6 +223,17 @@ describe("generateProject()", macosOnly, () => {
214223

215224
deepEqual(trimPaths(result, cwd), PROJECT_FILES.customReactNative);
216225
});
226+
227+
it("finds community autolinking script for older versions", () => {
228+
setMockFiles(makeMockProject(undefined, "0.74.0"));
229+
230+
const result = generateProject("ios", "ios", {});
231+
232+
equal(
233+
result.communityAutolinkingScriptPath,
234+
"node_modules/@react-native-community/cli-platform-ios/native_modules.rb"
235+
);
236+
});
217237
});
218238

219239
const PROJECT_FILES = {
@@ -232,6 +252,8 @@ const PROJECT_FILES = {
232252
PRODUCT_VERSION: "1.0",
233253
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
234254
},
255+
communityAutolinkingScriptPath: undefined,
256+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
235257
reactNativePath: "/~/node_modules/react-native-macos",
236258
reactNativeVersion: 1000000000,
237259
testsBuildSettings: {},
@@ -257,6 +279,8 @@ const PROJECT_FILES = {
257279
PRODUCT_VERSION: "1.0",
258280
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
259281
},
282+
communityAutolinkingScriptPath: undefined,
283+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
260284
reactNativePath: "/~/node_modules/react-native",
261285
reactNativeVersion: 1000000000,
262286
testsBuildSettings: {},
@@ -288,6 +312,8 @@ const PROJECT_FILES = {
288312
"/visionos/ReactTestAppUITests/Info.plist",
289313
"/package.json",
290314
"/node_modules/@callstack/react-native-visionos/package.json",
315+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
316+
"/node_modules/@rnx-kit/react-native-host/package.json",
291317
"/node_modules/react-native/package.json",
292318
"/node_modules/react-native-macos/package.json",
293319
"/node_modules/.generated/ios/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",
@@ -316,6 +342,8 @@ const PROJECT_FILES = {
316342
PRODUCT_VERSION: "1.0",
317343
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
318344
},
345+
communityAutolinkingScriptPath: undefined,
346+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
319347
reactNativePath: "/~/node_modules/react-native-macos",
320348
reactNativeVersion: 1000000000,
321349
testsBuildSettings: {},
@@ -348,6 +376,8 @@ const PROJECT_FILES = {
348376
"/visionos/ReactTestAppUITests/Info.plist",
349377
"/package.json",
350378
"/node_modules/@callstack/react-native-visionos/package.json",
379+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
380+
"/node_modules/@rnx-kit/react-native-host/package.json",
351381
"/node_modules/react-native/package.json",
352382
"/node_modules/react-native-macos/package.json",
353383
"/node_modules/.generated/macos/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",
@@ -376,6 +406,8 @@ const PROJECT_FILES = {
376406
PRODUCT_VERSION: "1.0",
377407
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
378408
},
409+
communityAutolinkingScriptPath: undefined,
410+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
379411
reactNativePath: "/~/node_modules/@callstack/react-native-visionos",
380412
reactNativeVersion: 1000000000,
381413
testsBuildSettings: {},
@@ -408,6 +440,8 @@ const PROJECT_FILES = {
408440
"/visionos/.xcode.env",
409441
"/package.json",
410442
"/node_modules/@callstack/react-native-visionos/package.json",
443+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
444+
"/node_modules/@rnx-kit/react-native-host/package.json",
411445
"/node_modules/react-native/package.json",
412446
"/node_modules/react-native-macos/package.json",
413447
"/node_modules/.generated/visionos/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",
@@ -432,6 +466,8 @@ const PROJECT_FILES = {
432466
PRODUCT_VERSION: "1.0",
433467
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
434468
},
469+
communityAutolinkingScriptPath: undefined,
470+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
435471
reactNativePath: "/~/node_modules/react-native",
436472
reactNativeVersion: 1000000000,
437473
testsBuildSettings: {},
@@ -463,6 +499,8 @@ const PROJECT_FILES = {
463499
"/visionos/ReactTestAppUITests/Info.plist",
464500
"/package.json",
465501
"/node_modules/@callstack/react-native-visionos/package.json",
502+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
503+
"/node_modules/@rnx-kit/react-native-host/package.json",
466504
"/node_modules/react-native/package.json",
467505
"/node_modules/react-native-macos/package.json",
468506
"/node_modules/.generated/ios/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",
@@ -485,6 +523,8 @@ const PROJECT_FILES = {
485523
PRODUCT_VERSION: "1.0",
486524
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
487525
},
526+
communityAutolinkingScriptPath: undefined,
527+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
488528
reactNativePath: "/~/node_modules/react-native-macos",
489529
reactNativeVersion: 1000000000,
490530
testsBuildSettings: {},
@@ -517,6 +557,8 @@ const PROJECT_FILES = {
517557
"/visionos/ReactTestAppUITests/Info.plist",
518558
"/package.json",
519559
"/node_modules/@callstack/react-native-visionos/package.json",
560+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
561+
"/node_modules/@rnx-kit/react-native-host/package.json",
520562
"/node_modules/react-native/package.json",
521563
"/node_modules/react-native-macos/package.json",
522564
"/node_modules/.generated/macos/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",
@@ -539,6 +581,8 @@ const PROJECT_FILES = {
539581
PRODUCT_VERSION: "1.0",
540582
USER_HEADER_SEARCH_PATHS: ["/~/node_modules/.generated"],
541583
},
584+
communityAutolinkingScriptPath: undefined,
585+
reactNativeHostPath: "../node_modules/@rnx-kit/react-native-host",
542586
reactNativePath: "/~/node_modules/@callstack/react-native-visionos",
543587
reactNativeVersion: 1000000000,
544588
testsBuildSettings: {},
@@ -571,6 +615,8 @@ const PROJECT_FILES = {
571615
"/visionos/.xcode.env",
572616
"/package.json",
573617
"/node_modules/@callstack/react-native-visionos/package.json",
618+
"/node_modules/@react-native-community/cli-platform-ios/native_modules.rb",
619+
"/node_modules/@rnx-kit/react-native-host/package.json",
574620
"/node_modules/react-native/package.json",
575621
"/node_modules/react-native-macos/package.json",
576622
"/node_modules/.generated/visionos/ReactTestApp.xcodeproj/xcshareddata/xcschemes/ReactTestApp.xcscheme",

test/ios/xcode.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function makeProjectConfiguration(): ProjectConfiguration {
4242
xcodeprojPath: "",
4343
reactNativePath: "",
4444
reactNativeVersion: 0,
45+
reactNativeHostPath: "",
4546
useNewArch: false,
4647
useBridgeless: false,
4748
buildSettings: {},

0 commit comments

Comments
 (0)