-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathwithRockAutolinking.ts
More file actions
176 lines (156 loc) · 7.1 KB
/
withRockAutolinking.ts
File metadata and controls
176 lines (156 loc) · 7.1 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
// Expo Config Plugin that re-applies Rock's autolinking patches on
// every `expo prebuild`. Designed to live in `app.config.ts plugins:
// ['@rock-js/plugin-expo-config-plugins']` so the patches survive
// Expo CNG (Continuous Native Generation) regenerating `ios/` and
// `android/` from scratch.
//
// Mirrors the one-shot transforms in
// `packages/create-app/src/lib/utils/initInExistingProject.ts`:
// - iOS Podfile — point `use_native_modules!` at Rock
// - Android build.gradle — point `cliFile` at Rock's bin.js
// - Android settings.gradle — point `autolinkLibrariesFromCommand`
// at Rock
// - Xcode project.pbxproj — rewrite the React Native build phase
// shellScript to source Rock's CLI
//
// Each step is idempotent so repeated prebuilds don't break.
import { type ConfigPlugin, withDangerousMod } from '@expo/config-plugins';
import * as fs from 'node:fs';
import * as path from 'node:path';
// ----- iOS / Podfile ---------------------------------------------------------
export function patchPodfile(content: string): string {
if (content.includes(`(['npx', 'rock', 'config', '-p', 'ios'])`)) {
// Already patched — leave alone (idempotent).
return content;
}
return content.replace(
/(config\s*=\s*use_native_modules!)(\s*\([^)]*\))?/g,
"$1(['npx', 'rock', 'config', '-p', 'ios'])",
);
}
const withRockIosPodfile: ConfigPlugin = (config) =>
withDangerousMod(config, [
'ios',
async (cfg) => {
const podfilePath = path.join(cfg.modRequest.platformProjectRoot, 'Podfile');
if (!fs.existsSync(podfilePath)) return cfg;
const original = await fs.promises.readFile(podfilePath, 'utf8');
const patched = patchPodfile(original);
if (patched !== original) {
await fs.promises.writeFile(podfilePath, patched);
}
return cfg;
},
]);
// ----- iOS / Xcode project.pbxproj ------------------------------------------
const XCODE_REACT_PHASE_TARGET =
'shellScript = "set -e\\nif [[ -f \\"$PODS_ROOT/../.xcode.env\\" ]]; then\\nsource \\"$PODS_ROOT/../.xcode.env\\"\\nfi\\nif [[ -f \\"$PODS_ROOT/../.xcode.env.local\\" ]]; then\\nsource \\"$PODS_ROOT/../.xcode.env.local\\"\\nfi\\nexport CONFIG_CMD=\\"dummy-workaround-value\\"\\nexport CLI_PATH=\\"$(\\"$NODE_BINARY\\" --print \\"require(\'path\').dirname(require.resolve(\'rock/package.json\')) + \'/dist/src/bin.js\'\\")\\"\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\n";';
const XCODE_REACT_PHASE_SOURCES = [
// Default Expo / Community-CLI format
'shellScript = "set -e\\n\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\nREACT_NATIVE_XCODE=\\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\\"\\n\\n/bin/sh -c \\"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\\"\\n";',
// RN 0.83 format
'shellScript = "set -e\\n\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\nREACT_NATIVE_XCODE=\\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\\"\\n\\n/bin/sh -c \\"\\\\\\"$WITH_ENVIRONMENT\\\\\\" \\\\\\"$REACT_NATIVE_XCODE\\\\\\"\\"\\n";',
];
export function patchXcodeProject(content: string): string {
if (content.includes(XCODE_REACT_PHASE_TARGET)) {
return content;
}
for (const source of XCODE_REACT_PHASE_SOURCES) {
if (content.includes(source)) {
return content.replace(source, XCODE_REACT_PHASE_TARGET);
}
}
return content;
}
const withRockIosXcode: ConfigPlugin = (config) =>
withDangerousMod(config, [
'ios',
async (cfg) => {
const iosDir = cfg.modRequest.platformProjectRoot;
const xcodeProjectFolder = (await fs.promises.readdir(iosDir)).find((f) =>
f.endsWith('.xcodeproj'),
);
if (!xcodeProjectFolder) return cfg;
const projectPath = path.join(iosDir, xcodeProjectFolder, 'project.pbxproj');
if (!fs.existsSync(projectPath)) return cfg;
const original = await fs.promises.readFile(projectPath, 'utf8');
const patched = patchXcodeProject(original);
if (patched !== original) {
await fs.promises.writeFile(projectPath, patched);
}
return cfg;
},
]);
// ----- Android / app/build.gradle -------------------------------------------
const ANDROID_CLI_FILE_TARGET = 'cliFile = file("../../node_modules/rock/dist/src/bin.js")';
export function patchAndroidBuildGradle(content: string): string {
if (content.includes(ANDROID_CLI_FILE_TARGET)) {
return content;
}
return content.replace(
/(?:\/\/\s+)?cliFile\s*=\s*file\([^)]*\)/g,
ANDROID_CLI_FILE_TARGET,
);
}
// ----- Android / settings.gradle --------------------------------------------
const ANDROID_AUTOLINK_TARGET =
"autolinkLibrariesFromCommand(['npx', 'rock', 'config', '-p', 'android'])";
export function patchAndroidSettingsGradle(content: string): string {
if (content.includes(ANDROID_AUTOLINK_TARGET)) {
return content;
}
// Try the full block first (`extensions.configure(...) { ... }`)…
const blockPattern =
/extensions\.configure\(com\.facebook\.react\.ReactSettingsExtension\)\{[^}]*autolinkLibrariesFromCommand\([^)]*\)[^}]*\}/gs;
const blockReplacement = `extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.${ANDROID_AUTOLINK_TARGET} }`;
if (blockPattern.test(content)) {
return content.replace(blockPattern, blockReplacement);
}
// …otherwise fall back to replacing just the inner call.
return content.replace(/autolinkLibrariesFromCommand\([^)]*\)/g, ANDROID_AUTOLINK_TARGET);
}
const withRockAndroid: ConfigPlugin = (config) =>
withDangerousMod(config, [
'android',
async (cfg) => {
const androidDir = cfg.modRequest.platformProjectRoot;
const appBuildGradlePath = path.join(androidDir, 'app', 'build.gradle');
const settingsGradlePath = path.join(androidDir, 'settings.gradle');
if (fs.existsSync(appBuildGradlePath)) {
const original = await fs.promises.readFile(appBuildGradlePath, 'utf8');
const patched = patchAndroidBuildGradle(original);
if (patched !== original) {
await fs.promises.writeFile(appBuildGradlePath, patched);
}
}
if (fs.existsSync(settingsGradlePath)) {
const original = await fs.promises.readFile(settingsGradlePath, 'utf8');
const patched = patchAndroidSettingsGradle(original);
if (patched !== original) {
await fs.promises.writeFile(settingsGradlePath, patched);
}
}
return cfg;
},
]);
// ----- Top-level plugin -----------------------------------------------------
/**
* Expo Config Plugin that re-applies Rock's autolinking patches to the
* native dirs generated by `expo prebuild`. Add it once to
* `app.config.ts` `plugins`:
*
* plugins: [
* '@rock-js/plugin-expo-config-plugins/withRockAutolinking',
* // …
* ],
*
* Idempotent — repeated `expo prebuild --clean` runs converge on the
* same patched files.
*/
export const withRockAutolinking: ConfigPlugin = (config) => {
config = withRockIosPodfile(config);
config = withRockIosXcode(config);
config = withRockAndroid(config);
return config;
};
export default withRockAutolinking;