-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(install): unpack native libraries
- Add functions to unzip native libraries.
- Loading branch information
Showing
6 changed files
with
77 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Container } from "@/main/container/spec"; | ||
import { nativeLib } from "@/main/profile/native-lib"; | ||
import { filterRules } from "@/main/profile/rules"; | ||
import type { VersionProfile } from "@/main/profile/version-profile"; | ||
import { unwrapESM } from "@/main/util/module"; | ||
import fs from "fs-extra"; | ||
import type streamZip from "node-stream-zip"; | ||
import path from "path"; | ||
|
||
let zl: typeof streamZip; | ||
|
||
async function getLibrary(): Promise<typeof streamZip> { | ||
if (!zl) { | ||
zl = (await unwrapESM(import("node-stream-zip"))).default; | ||
} | ||
return zl; | ||
} | ||
|
||
async function unpackOne(lib: string, out: string, exclude?: string[]): Promise<void> { | ||
const z = await getLibrary(); | ||
const filter = (p: string) => !(p.endsWith("/") || exclude?.find(e => p.startsWith(e))); | ||
const f = new z.async({ file: lib }); | ||
|
||
try { | ||
const files = Object.values(await f.entries()).filter(ent => !ent.isDirectory && filter(ent.name)); | ||
|
||
await Promise.all(files.map(async ent => { | ||
const t = path.join(out, ent.name); | ||
console.debug(`Extracting native artifact: ${lib} (${ent.name}) -> ${t}`); | ||
|
||
await fs.ensureDir(path.dirname(t)); | ||
await f.extract(ent, t); | ||
})); | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
await f.close(); | ||
} | ||
} | ||
|
||
async function unpack(profile: VersionProfile, container: Container, features: Set<string>): Promise<void> { | ||
const nativesRoot = container.nativesRoot(profile.id); | ||
const sources = profile.libraries.filter(l => filterRules(l.rules, features) && nativeLib.isNative(l)); | ||
|
||
await Promise.all(sources.map(async s => { | ||
const name = nativeLib.getArtifactName(s); | ||
await unpackOne(container.nativeLibrary(s.name, name), nativesRoot, s.extract?.exclude); | ||
})); | ||
} | ||
|
||
export const nativesLint = { unpack, unpackOne }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
import { MavenName } from "@/main/profile/maven-name"; | ||
import type { Library } from "@/main/profile/version-profile"; | ||
import { getOSBits, getOSName } from "@/main/sys/os"; | ||
|
||
/** | ||
* Check whether the given library is a native library (which requires unpacking). | ||
*/ | ||
export function isNativeLibrary(l: Library): boolean { | ||
function isNative(l: Library): boolean { | ||
return l.natives !== undefined && getOSName() in l.natives; | ||
} | ||
|
||
/** | ||
* Gets the name of the native artifact. | ||
*/ | ||
export function getNativeArtifactName(l: Library): string { | ||
function getArtifactName(l: Library): string { | ||
const index = l.natives?.[getOSName()]; | ||
if (!index) throw `Library ${l.name} has no native artifact for ${getOSName()}`; | ||
return index.replaceAll("${arch}", getOSBits()); | ||
} | ||
|
||
/** | ||
* Gets the path to the native artifact (generated by the library name and the classifier). | ||
*/ | ||
export function getNativeArtifactPath(l: Library): string { | ||
const nativeName = getNativeArtifactName(l); | ||
const name = new MavenName(l.name); | ||
name.classifier = nativeName; | ||
return name.toPath(); | ||
} | ||
export const nativeLib = { isNative, getArtifactName }; |