-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapple.ts
More file actions
220 lines (201 loc) · 6.66 KB
/
apple.ts
File metadata and controls
220 lines (201 loc) · 6.66 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import assert from "node:assert/strict";
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { spawn } from "@react-native-node-api/cli-utils";
import { getLatestMtime, getLibraryName } from "../path-utils.js";
import {
getLinkedModuleOutputPath,
LinkModuleOptions,
LinkModuleResult,
} from "./link-modules.js";
function determineInfoPlistPath(frameworkPath: string) {
const checkedPaths = new Array<string>();
// First, assume it is an "unversioned" framework that keeps its Info.plist in
// the root. This is the convention for iOS, tvOS, and friends.
let infoPlistPath = path.join(frameworkPath, "Info.plist");
if (fs.existsSync(infoPlistPath)) {
return infoPlistPath;
}
checkedPaths.push(infoPlistPath);
// Next, assume it is a "versioned" framework that keeps its Info.plist
// under a subdirectory. This is the convention for macOS.
infoPlistPath = path.join(
frameworkPath,
"Versions/Current/Resources/Info.plist",
);
if (fs.existsSync(infoPlistPath)) {
return infoPlistPath;
}
checkedPaths.push(infoPlistPath);
throw new Error(
[
`Unable to locate an Info.plist file within framework. Checked the following paths:`,
...checkedPaths.map((checkedPath) => `- ${checkedPath}`),
].join("\n"),
);
}
/**
* Resolves the Info.plist file within a framework and reads its contents.
*/
export async function readInfoPlist(frameworkPath: string) {
let infoPlistPath: string;
try {
infoPlistPath = determineInfoPlistPath(frameworkPath);
} catch (cause) {
throw new Error(
`Unable to read Info.plist for framework at path "${frameworkPath}", as an Info.plist file couldn't be found.`,
{ cause },
);
}
let contents: string;
try {
contents = await fs.promises.readFile(infoPlistPath, "utf-8");
} catch (cause) {
throw new Error(
`Unable to read Info.plist for framework at path "${frameworkPath}", due to a file system error.`,
{ cause },
);
}
return { infoPlistPath, contents };
}
type UpdateInfoPlistOptions = {
frameworkPath: string;
oldLibraryName: string;
newLibraryName: string;
};
/**
* Update the Info.plist file of an xcframework to use the new library name.
*/
export async function updateInfoPlist({
frameworkPath,
oldLibraryName,
newLibraryName,
}: UpdateInfoPlistOptions) {
const { infoPlistPath, contents } = await readInfoPlist(frameworkPath);
// TODO: Use a proper plist parser
const updatedContents = contents.replaceAll(oldLibraryName, newLibraryName);
await fs.promises.writeFile(infoPlistPath, updatedContents, "utf-8");
}
export async function linkXcframework({
platform,
modulePath,
incremental,
naming,
}: LinkModuleOptions): Promise<LinkModuleResult> {
// Copy the xcframework to the output directory and rename the framework and binary
const newLibraryName = getLibraryName(modulePath, naming);
const outputPath = getLinkedModuleOutputPath(platform, modulePath, naming);
const tempPath = await fs.promises.mkdtemp(
path.join(os.tmpdir(), `react-native-node-api-${newLibraryName}-`),
);
try {
if (incremental && fs.existsSync(outputPath)) {
const moduleModified = getLatestMtime(modulePath);
const outputModified = getLatestMtime(outputPath);
if (moduleModified < outputModified) {
return {
originalPath: modulePath,
libraryName: newLibraryName,
outputPath,
skipped: true,
};
}
}
// Delete any existing xcframework (or xcodebuild will try to amend it)
await fs.promises.rm(outputPath, { recursive: true, force: true });
await fs.promises.cp(modulePath, tempPath, { recursive: true });
// Following extracted function mimics `glob("*/*.framework/")`
function globFrameworkDirs<T>(
startPath: string,
fn: (parentPath: string, name: string) => Promise<T>,
) {
return fs
.readdirSync(startPath, { withFileTypes: true })
.filter((tripletEntry) => tripletEntry.isDirectory())
.flatMap((tripletEntry) => {
const tripletPath = path.join(startPath, tripletEntry.name);
return fs
.readdirSync(tripletPath, { withFileTypes: true })
.filter(
(frameworkEntry) =>
frameworkEntry.isDirectory() &&
path.extname(frameworkEntry.name) === ".framework",
)
.flatMap(
async (frameworkEntry) =>
await fn(tripletPath, frameworkEntry.name),
);
});
}
const frameworkPaths = await Promise.all(
globFrameworkDirs(tempPath, async (tripletPath, frameworkEntryName) => {
const frameworkPath = path.join(tripletPath, frameworkEntryName);
const oldLibraryName = path.basename(frameworkEntryName, ".framework");
const oldLibraryPath = path.join(frameworkPath, oldLibraryName);
const newFrameworkPath = path.join(
tripletPath,
`${newLibraryName}.framework`,
);
const newLibraryPath = path.join(newFrameworkPath, newLibraryName);
assert(
fs.existsSync(oldLibraryPath),
`Expected a library at '${oldLibraryPath}'`,
);
// Rename the library
await fs.promises.rename(
oldLibraryPath,
// Cannot use newLibraryPath here, because the framework isn't renamed yet
path.join(frameworkPath, newLibraryName),
);
// Rename the framework
await fs.promises.rename(frameworkPath, newFrameworkPath);
// Expect the library in the new location
assert(fs.existsSync(newLibraryPath));
// Update the binary
await spawn(
"install_name_tool",
[
"-id",
`@rpath/${newLibraryName}.framework/${newLibraryName}`,
newLibraryPath,
],
{
outputMode: "buffered",
},
);
// Update the Info.plist file for the framework
await updateInfoPlist({
frameworkPath: newFrameworkPath,
oldLibraryName,
newLibraryName,
});
return newFrameworkPath;
}),
);
// Create a new xcframework from the renamed frameworks
await spawn(
"xcodebuild",
[
"-create-xcframework",
...frameworkPaths.flatMap((frameworkPath) => [
"-framework",
frameworkPath,
]),
"-output",
outputPath,
],
{
outputMode: "buffered",
},
);
return {
originalPath: modulePath,
libraryName: newLibraryName,
outputPath,
skipped: false,
};
} finally {
await fs.promises.rm(tempPath, { recursive: true, force: true });
}
}