refactor: getSpecificVersionAndUrl

This commit is contained in:
Amin Yahyaabadi 2021-09-15 02:42:12 -05:00
parent fb4b68daca
commit de191063e3
2 changed files with 31 additions and 24 deletions

View File

@ -4,7 +4,7 @@ import semverLte from "semver/functions/lte"
import { isValidUrl } from "../utils/http/validate_url"
import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin"
import { extractExe, extractTarByExe } from "../utils/setup/extract"
import { getSpecificVersions, getVersions } from "../utils/setup/version"
import { getSpecificVersionAndUrl, getVersions } from "../utils/setup/version"
//================================================
// Version
@ -212,24 +212,6 @@ function getUrl(platform: string, version: string): string | null | Promise<stri
}
}
/** Gets the most recent specific LLVM version for which there is a valid download URL. */
export async function getSpecificVersionAndUrl(platform: string, version: string): Promise<[string, string]> {
if (!VERSIONS.has(version)) {
throw new Error(`Unsupported target! (platform='${platform}', version='${version}')`)
}
for (const specificVersion of getSpecificVersions(VERSIONS, version)) {
// eslint-disable-next-line no-await-in-loop
const url = await getUrl(platform, specificVersion)
// eslint-disable-next-line no-await-in-loop
if (url !== null && (await isValidUrl(url))) {
return [specificVersion, url]
}
}
throw new Error(`Unsupported target! (platform='${platform}', version='${version}')`)
}
//================================================
// Action
//================================================
@ -238,7 +220,7 @@ const DEFAULT_NIX_DIRECTORY = "./llvm"
const DEFAULT_WIN32_DIRECTORY = "C:/Program Files/LLVM"
async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): Promise<PackageInfo> {
const [specificVersion, url] = await getSpecificVersionAndUrl(platform, version)
const [specificVersion, url] = await getSpecificVersionAndUrl(VERSIONS, platform, version, getUrl)
core.setOutput("version", specificVersion)
return {
url,

View File

@ -1,6 +1,8 @@
import { isValidUrl } from "../http/validate_url"
/**
* Gets the specific LLVM versions supported by this action compatible with the supplied (specific or minimum) LLVM
* version in descending order of release (e.g., `5.0.2`, `5.0.1`, and `5.0.0` for `5`).
* Gets the specific versions supported by this action compatible with the supplied (specific or minimum) version in
* descending order of release (e.g., `5.0.2`, `5.0.1`, and `5.0.0` for `5`).
*/
export function getSpecificVersions(versions: Set<string>, semversion: string): string[] {
return Array.from(versions)
@ -10,8 +12,8 @@ export function getSpecificVersions(versions: Set<string>, semversion: string):
}
/**
* Gets the specific and minimum LLVM versions that can be used to refer to the supplied specific LLVM versions (e.g.,
* `3`, `3.5`, `3.5.2` for `3.5.2`).
* Gets the specific and minimum versions that can be used to refer to the supplied specific versions (e.g., `3`, `3.5`,
* `3.5.2` for `3.5.2`).
*/
export function getVersions(specific: string[]): Set<string> {
const versions = new Set(specific)
@ -23,3 +25,26 @@ export function getVersions(specific: string[]): Set<string> {
return versions
}
/** Gets the most recent specific version for which there is a valid download URL. */
export async function getSpecificVersionAndUrl(
versions: Set<string>,
platform: string,
version: string,
getUrl: (platform: string, version: string) => string | null | Promise<string | null>
): Promise<[string, string]> {
if (!versions.has(version)) {
throw new Error(`Unsupported target! (platform='${platform}', version='${version}')`)
}
for (const specificVersion of getSpecificVersions(versions, version)) {
// eslint-disable-next-line no-await-in-loop
const url = await getUrl(platform, specificVersion)
// eslint-disable-next-line no-await-in-loop
if (url !== null && (await isValidUrl(url))) {
return [specificVersion, url]
}
}
throw new Error(`Unsupported target! (platform='${platform}', version='${version}')`)
}