-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapple.ts
More file actions
162 lines (153 loc) · 4.97 KB
/
apple.ts
File metadata and controls
162 lines (153 loc) · 4.97 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
import assert from "node:assert/strict";
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { spawn } from "bufout";
import { getLatestMtime, getLibraryName } from "../path-utils.js";
import {
getLinkedModuleOutputPath,
LinkModuleOptions,
LinkModuleResult,
} from "./link-modules.js";
type UpdateInfoPlistOptions = {
filePath: string;
oldLibraryName: string;
newLibraryName: string;
};
/**
* Update the Info.plist file of an xcframework to use the new library name.
*/
export async function updateInfoPlist({
filePath,
oldLibraryName,
newLibraryName,
}: UpdateInfoPlistOptions) {
const infoPlistContents = await fs.promises.readFile(filePath, "utf-8");
// TODO: Use a proper plist parser
const updatedContents = infoPlistContents.replaceAll(
oldLibraryName,
newLibraryName
);
await fs.promises.writeFile(filePath, 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({
filePath: path.join(newFrameworkPath, "Info.plist"),
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 });
}
}