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: /// If an ubuntu versions is not in this map:
// - the newer ubuntu versions use the first entry (e.g. v20), // - the newer ubuntu versions use the first entry (e.g. v20),
// - the older ones use "" // - the older ones use ""
export const DefaultLinuxVersion: Record<string, Record<number, string>> = { export const DefaultLinuxVersion: Record<string, Record<number, string> | undefined> = {
gcc: { gcc: {
22: "13", 22: "13",
20: "11", 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 */ /** 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) { export function getVersion(name: string, version: string | undefined, osVersion: number[] | null = null) {
if (isDefault(version, name)) { console.log("isDefault", version, name, isVersionDefault(version))
if (process.platform === "linux" && osVersion !== null) { if (isVersionDefault(version) && process.platform === "linux" && osVersion !== null && name in DefaultLinuxVersion) {
return getDefaultLinuxVersion(name, osVersion) return getDefaultLinuxVersion(osVersion, DefaultLinuxVersion[name]!)
} } else if (isVersionDefault(version) && name in DefaultVersions) {
// anything else return DefaultVersions[name]!
return DefaultVersions[name]! // checked by isDefault } else if (version === "true") {
} else { return ""
return version ?? ""
} }
return version ?? ""
}
function isVersionDefault(version: string | undefined) {
return version === "true" || version === undefined
} }
/// choose the default linux version based on ubuntu version /// 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] const osVersionMaj = osVersion[0]
// find which version block the os version is in // 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)) .map((v) => parseInt(v, 10))
.sort((a, b) => b - a) // sort in descending order .sort((a, b) => b - a) // sort in descending order
.find((v) => osVersionMaj >= v) .find((v) => osVersionMaj >= v)
return satisfyingVersion === undefined ? "" : DefaultLinuxVersion[name][satisfyingVersion] return satisfyingVersion === undefined ? "" : toolLinuxVersions[satisfyingVersion]
}
function isDefault(version: string | undefined, name: string) {
return (version === "true" || version === undefined) && name in DefaultLinuxVersion
} }
/** /**
@ -39,7 +39,7 @@ function isDefault(version: string | undefined, name: string) {
*/ */
export function syncVersions(opts: Opts, tools: Inputs[]): boolean { export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
const toolsInUse = tools.filter((tool) => opts[tool] !== undefined) 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" const targetVersion = toolsNonDefaultVersion.length >= 1 ? opts[toolsNonDefaultVersion[0]] : "true"