fix: match assets via keywords

This commit is contained in:
Amin Yahyaabadi 2024-09-07 16:04:03 -07:00
parent d0c5d225d2
commit 22bfbec1e0
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
8 changed files with 33 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ import { addUpdateAlternativesToRc, installAptPack } from "setup-apt"
import { installBrewPack } from "setup-brew" import { installBrewPack } from "setup-brew"
import { rcOptions } from "../cli-options.js" import { rcOptions } from "../cli-options.js"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk.js" import { setupMacOSSDK } from "../macos-sdk/macos-sdk.js"
import { loadGitHubAssetList, matchAsset } from "../utils/asset/load-assets.js" import { loadAssetList, matchAsset } from "../utils/asset/load-assets.js"
import { hasDnf } from "../utils/env/hasDnf.js" import { hasDnf } from "../utils/env/hasDnf.js"
import { isArch } from "../utils/env/isArch.js" import { isArch } from "../utils/env/isArch.js"
import { isUbuntu } from "../utils/env/isUbuntu.js" import { isUbuntu } from "../utils/env/isUbuntu.js"
@ -29,18 +29,22 @@ const dirname = typeof __dirname === "string" ? __dirname : path.dirname(fileURL
async function getGccPackageInfo(version: string, platform: NodeJS.Platform, arch: string): Promise<PackageInfo> { async function getGccPackageInfo(version: string, platform: NodeJS.Platform, arch: string): Promise<PackageInfo> {
switch (platform) { switch (platform) {
case "win32": { case "win32": {
const mingwAssets = await loadGitHubAssetList( const mingwAssets = await loadAssetList(
join(dirname, "github_brechtsanders_winlibs_mingw.json"), join(dirname, "github_brechtsanders_winlibs_mingw.json"),
) )
const mingwArchMap = {
x64: "x86_64",
ia32: "i386",
} as Record<string, string | undefined>
const asset = matchAsset( const asset = matchAsset(
mingwAssets, mingwAssets,
{ {
version, version,
arch: arch === "x64" keywords: [
? "x86_64" mingwArchMap[arch] ?? arch,
: arch === "ia32" ],
? "i386"
: arch,
}, },
) )

View File

@ -69,7 +69,7 @@ async function fetchIndexFile(version: string, url: string, htmlDownloadDir: str
retry: { retry: {
delay: 100, delay: 100,
maxRetries: 3, maxRetries: 3,
} },
}, },
) )
dl.on("start", () => { dl.on("start", () => {

View File

@ -1,26 +1,29 @@
import { readFile } from "fs/promises" import { readFile } from "fs/promises"
/** /**
* The list of assets of a GitHub release * The list of assets
* @key tag The tag of the release * @key tag The tag of the release
* @value assets The names of the assets of the release * @value assets The names of the assets of the release
*/ */
export type Assets = Record<string, string[]> export type Assets = Record<string, string[]>
export async function loadGitHubAssetList(path: string): Promise<Assets> { /**
* Load the list of assets from a json file
*/
export async function loadAssetList(path: string): Promise<Assets> {
const data = await readFile(path, "utf-8") const data = await readFile(path, "utf-8")
return JSON.parse(data) return JSON.parse(data)
} }
type MatchAssetOpts = { type MatchAssetOpts = {
version: string version: string
arch?: string keywords?: string[]
filterTag?: (version: string) => boolean filterTag?: (version: string) => boolean
filterName?: (asset: string) => boolean filterName?: (asset: string) => boolean
} }
/** /**
* Match the asset that matches the version and arch * Match the asset that matches the version and given keywords
*/ */
export function matchAsset( export function matchAsset(
assets: Assets, assets: Assets,
@ -60,21 +63,23 @@ export function matchAsset(
} }
if (matchedNames.length === 0) { if (matchedNames.length === 0) {
throw new Error(`no asset found for version ${opts.version} and arch ${opts.arch}`) throw new Error(`no asset found for version ${opts.version}`)
} }
// use the first asset if the arch is not specified if (opts.keywords?.length === 0) {
if (opts.arch === undefined) {
return { tag, name: matchedNames[0] } return { tag, name: matchedNames[0] }
} }
// find the asset that matches the arch // find the first asset that matches the keywords
for (const name of matchedNames) { for (const name of matchedNames) {
// search each asset name for the arch if (opts.keywords!.every((keyword) => name.includes(keyword))) {
if (name.includes(opts.arch)) {
return { tag, name } return { tag, name }
} }
} }
throw new Error(`arch ${opts.arch} could not be found among ${JSON.stringify(matchedNames)}`) throw new Error(
`Could not find a matching asset for version ${opts.version} and keywords ${JSON.stringify(opts.keywords)} among ${
JSON.stringify(matchedNames)
}`,
)
} }