Skip to content

Commit ea0b560

Browse files
mani3xisMariusz Pasinski
andauthored
Simplify source code while visiting few cli/ sources (#125)
* refactor: simplify by using `Object.values()`; reuse `absoluteModulePaths` * refactor: extract iteration code from linking logic This change attempts to separate the Xcframework linking logic from the directory iteration by extracting the "glob-like" function out. This change also opens the door for easier migration to a proper glob() function. --------- Co-authored-by: Mariusz Pasinski <[email protected]>
1 parent 45e472b commit ea0b560

File tree

2 files changed

+81
-86
lines changed

2 files changed

+81
-86
lines changed

packages/host/src/node/cli/apple.ts

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -64,73 +64,73 @@ export async function linkXcframework({
6464
await fs.promises.rm(outputPath, { recursive: true, force: true });
6565
await fs.promises.cp(modulePath, tempPath, { recursive: true });
6666

67-
const frameworkPaths = await Promise.all(
68-
fs
69-
.readdirSync(tempPath, {
70-
withFileTypes: true,
71-
})
67+
// Following extracted function mimics `glob("*/*.framework/")`
68+
function globFrameworkDirs<T>(
69+
startPath: string,
70+
fn: (parentPath: string, name: string) => Promise<T>
71+
) {
72+
return fs
73+
.readdirSync(startPath, { withFileTypes: true })
7274
.filter((tripletEntry) => tripletEntry.isDirectory())
7375
.flatMap((tripletEntry) => {
74-
const tripletPath = path.join(tempPath, tripletEntry.name);
76+
const tripletPath = path.join(startPath, tripletEntry.name);
7577
return fs
76-
.readdirSync(tripletPath, {
77-
withFileTypes: true,
78-
})
78+
.readdirSync(tripletPath, { withFileTypes: true })
7979
.filter(
8080
(frameworkEntry) =>
8181
frameworkEntry.isDirectory() &&
8282
path.extname(frameworkEntry.name) === ".framework"
8383
)
84-
.flatMap(async (frameworkEntry) => {
85-
const frameworkPath = path.join(tripletPath, frameworkEntry.name);
86-
const oldLibraryName = path.basename(
87-
frameworkEntry.name,
88-
".framework"
89-
);
90-
const oldLibraryPath = path.join(frameworkPath, oldLibraryName);
91-
const newFrameworkPath = path.join(
92-
tripletPath,
93-
`${newLibraryName}.framework`
94-
);
95-
const newLibraryPath = path.join(
96-
newFrameworkPath,
97-
newLibraryName
98-
);
99-
assert(
100-
fs.existsSync(oldLibraryPath),
101-
`Expected a library at '${oldLibraryPath}'`
102-
);
103-
// Rename the library
104-
await fs.promises.rename(
105-
oldLibraryPath,
106-
// Cannot use newLibraryPath here, because the framework isn't renamed yet
107-
path.join(frameworkPath, newLibraryName)
108-
);
109-
// Rename the framework
110-
await fs.promises.rename(frameworkPath, newFrameworkPath);
111-
// Expect the library in the new location
112-
assert(fs.existsSync(newLibraryPath));
113-
// Update the binary
114-
await spawn(
115-
"install_name_tool",
116-
[
117-
"-id",
118-
`@rpath/${newLibraryName}.framework/${newLibraryName}`,
119-
newLibraryPath,
120-
],
121-
{
122-
outputMode: "buffered",
123-
}
124-
);
125-
// Update the Info.plist file for the framework
126-
await updateInfoPlist({
127-
filePath: path.join(newFrameworkPath, "Info.plist"),
128-
oldLibraryName,
129-
newLibraryName,
130-
});
131-
return newFrameworkPath;
132-
});
133-
})
84+
.flatMap(async (frameworkEntry) =>
85+
await fn(tripletPath, frameworkEntry.name)
86+
);
87+
});
88+
}
89+
90+
const frameworkPaths = await Promise.all(
91+
globFrameworkDirs(tempPath, async (tripletPath, frameworkEntryName) => {
92+
const frameworkPath = path.join(tripletPath, frameworkEntryName);
93+
const oldLibraryName = path.basename(frameworkEntryName, ".framework");
94+
const oldLibraryPath = path.join(frameworkPath, oldLibraryName);
95+
const newFrameworkPath = path.join(
96+
tripletPath,
97+
`${newLibraryName}.framework`
98+
);
99+
const newLibraryPath = path.join(newFrameworkPath, newLibraryName);
100+
assert(
101+
fs.existsSync(oldLibraryPath),
102+
`Expected a library at '${oldLibraryPath}'`
103+
);
104+
// Rename the library
105+
await fs.promises.rename(
106+
oldLibraryPath,
107+
// Cannot use newLibraryPath here, because the framework isn't renamed yet
108+
path.join(frameworkPath, newLibraryName)
109+
);
110+
// Rename the framework
111+
await fs.promises.rename(frameworkPath, newFrameworkPath);
112+
// Expect the library in the new location
113+
assert(fs.existsSync(newLibraryPath));
114+
// Update the binary
115+
await spawn(
116+
"install_name_tool",
117+
[
118+
"-id",
119+
`@rpath/${newLibraryName}.framework/${newLibraryName}`,
120+
newLibraryPath,
121+
],
122+
{
123+
outputMode: "buffered",
124+
}
125+
);
126+
// Update the Info.plist file for the framework
127+
await updateInfoPlist({
128+
filePath: path.join(newFrameworkPath, "Info.plist"),
129+
oldLibraryName,
130+
newLibraryName,
131+
});
132+
return newFrameworkPath;
133+
})
134134
);
135135

136136
// Create a new xcframework from the renamed frameworks

packages/host/src/node/cli/link-modules.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ export async function linkModules({
6868
});
6969

7070
// Find absolute paths to xcframeworks
71-
const absoluteModulePaths = Object.entries(dependenciesByName).flatMap(
72-
([, dependency]) => {
73-
return dependency.modulePaths.map((modulePath) =>
74-
path.join(dependency.path, modulePath)
75-
);
76-
}
71+
const absoluteModulePaths = Object.values(dependenciesByName).flatMap(
72+
(dependency) => dependency.modulePaths.map(
73+
(modulePath) => path.join(dependency.path, modulePath)
74+
)
7775
);
7876

7977
if (hasDuplicateLibraryNames(absoluteModulePaths, naming)) {
@@ -82,28 +80,25 @@ export async function linkModules({
8280
}
8381

8482
return Promise.all(
85-
Object.entries(dependenciesByName).flatMap(([, dependency]) => {
86-
return dependency.modulePaths.map(async (modulePath) => {
87-
const originalPath = path.join(dependency.path, modulePath);
88-
try {
89-
return await linker({
90-
modulePath: originalPath,
91-
incremental,
92-
naming,
93-
platform,
94-
});
95-
} catch (error) {
96-
if (error instanceof SpawnFailure) {
97-
return {
98-
originalPath,
99-
skipped: false,
100-
failure: error,
101-
};
102-
} else {
103-
throw error;
104-
}
83+
absoluteModulePaths.map(async (originalPath) => {
84+
try {
85+
return await linker({
86+
modulePath: originalPath,
87+
incremental,
88+
naming,
89+
platform,
90+
});
91+
} catch (error) {
92+
if (error instanceof SpawnFailure) {
93+
return {
94+
originalPath,
95+
skipped: false,
96+
failure: error,
97+
};
98+
} else {
99+
throw error;
105100
}
106-
});
101+
}
107102
})
108103
);
109104
}

0 commit comments

Comments
 (0)