Skip to content

Commit

Permalink
fix: match assets via keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 7, 2024
1 parent d0c5d22 commit 22bfbec
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.mjs.map

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions src/gcc/gcc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { addUpdateAlternativesToRc, installAptPack } from "setup-apt"
import { installBrewPack } from "setup-brew"
import { rcOptions } from "../cli-options.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 { isArch } from "../utils/env/isArch.js"
import { isUbuntu } from "../utils/env/isUbuntu.js"
Expand All @@ -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> {
switch (platform) {
case "win32": {
const mingwAssets = await loadGitHubAssetList(
const mingwAssets = await loadAssetList(
join(dirname, "github_brechtsanders_winlibs_mingw.json"),
)

const mingwArchMap = {
x64: "x86_64",
ia32: "i386",
} as Record<string, string | undefined>

const asset = matchAsset(
mingwAssets,
{
version,
arch: arch === "x64"
? "x86_64"
: arch === "ia32"
? "i386"
: arch,
keywords: [
mingwArchMap[arch] ?? arch,
],
},
)

Expand Down
2 changes: 1 addition & 1 deletion src/llvm/llvm_org_releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,4 @@
"polly-3.5.1.src.tar.xz",
"test-suite-3.5.1.src.tar.xz"
]
}
}
2 changes: 1 addition & 1 deletion src/utils/asset/fetch-html-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function fetchIndexFile(version: string, url: string, htmlDownloadDir: str
retry: {
delay: 100,
maxRetries: 3,
}
},
},
)
dl.on("start", () => {
Expand Down
27 changes: 16 additions & 11 deletions src/utils/asset/load-assets.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { readFile } from "fs/promises"

/**
* The list of assets of a GitHub release
* The list of assets
* @key tag The tag of the release
* @value assets The names of the assets of the release
*/
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")
return JSON.parse(data)
}

type MatchAssetOpts = {
version: string
arch?: string
keywords?: string[]
filterTag?: (version: 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(
assets: Assets,
Expand Down Expand Up @@ -60,21 +63,23 @@ export function matchAsset(
}

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.arch === undefined) {
if (opts.keywords?.length === 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) {
// search each asset name for the arch
if (name.includes(opts.arch)) {
if (opts.keywords!.every((keyword) => name.includes(keyword))) {
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)
}`,
)
}

0 comments on commit 22bfbec

Please sign in to comment.