fix: handle edge-cases for default versions

This commit is contained in:
Amin Yahyaabadi 2023-06-14 00:47:34 -07:00
parent 18a5143dce
commit 24a5993a98
6 changed files with 23 additions and 23 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

@ -33,7 +33,7 @@ export const DefaultVersions: Record<string, string | undefined> = {
/// If an ubuntu versions is not in this map:
// - the newer ubuntu versions use the first entry (e.g. v20),
// - the older ones use ""
export const DefaultLinuxVersion: Record<string, Record<number, string>> = {
export const DefaultLinuxVersion: Record<string, Record<number, string> | undefined> = {
gcc: {
22: "13",
20: "11",

View File

@ -4,32 +4,32 @@ import { DefaultLinuxVersion, DefaultVersions } from "./default_versions"
/** Get the default version if passed true or undefined, otherwise return the version itself */
export function getVersion(name: string, version: string | undefined, osVersion: number[] | null = null) {
if (isDefault(version, name)) {
if (process.platform === "linux" && osVersion !== null) {
return getDefaultLinuxVersion(name, osVersion)
console.log("isDefault", version, name, isVersionDefault(version))
if (isVersionDefault(version) && process.platform === "linux" && osVersion !== null && name in DefaultLinuxVersion) {
return getDefaultLinuxVersion(osVersion, DefaultLinuxVersion[name]!)
} else if (isVersionDefault(version) && name in DefaultVersions) {
return DefaultVersions[name]!
} else if (version === "true") {
return ""
}
// anything else
return DefaultVersions[name]! // checked by isDefault
} else {
return version ?? ""
}
function isVersionDefault(version: string | undefined) {
return version === "true" || version === undefined
}
/// choose the default linux version based on ubuntu version
function getDefaultLinuxVersion(name: string, osVersion: number[]) {
function getDefaultLinuxVersion(osVersion: number[], toolLinuxVersions: Record<number, string>) {
const osVersionMaj = osVersion[0]
// find which version block the os version is in
const satisfyingVersion = Object.keys(DefaultLinuxVersion[name])
const satisfyingVersion = Object.keys(toolLinuxVersions)
.map((v) => parseInt(v, 10))
.sort((a, b) => b - a) // sort in descending order
.find((v) => osVersionMaj >= v)
return satisfyingVersion === undefined ? "" : DefaultLinuxVersion[name][satisfyingVersion]
}
function isDefault(version: string | undefined, name: string) {
return (version === "true" || version === undefined) && name in DefaultLinuxVersion
return satisfyingVersion === undefined ? "" : toolLinuxVersions[satisfyingVersion]
}
/**
@ -39,7 +39,7 @@ function isDefault(version: string | undefined, name: string) {
*/
export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
const toolsInUse = tools.filter((tool) => opts[tool] !== undefined)
const toolsNonDefaultVersion = toolsInUse.filter((tool) => !isDefault(opts[tool], tool))
const toolsNonDefaultVersion = toolsInUse.filter((tool) => !isVersionDefault(opts[tool]))
const targetVersion = toolsNonDefaultVersion.length >= 1 ? opts[toolsNonDefaultVersion[0]] : "true"