diff --git a/src/platform.ts b/src/platform.ts index 5711d5663d..b35ceaf9a4 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -20,6 +20,31 @@ export enum Platform { Ubuntu16 } +function convertOSReleaseTextToMap(text : string) : Map { + let ret: Map = new Map(); + const lines: string[] = text.split('\n'); + + for (let line of lines) { + line = line.trim(); + + let equalsIndex = line.indexOf('='); + if (equalsIndex >= 0) { + let key = line.substring(0, equalsIndex); + let value = line.substring(equalsIndex + 1); + + // Strip double quotes if necessary + if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) { + value = value.substring(1, value.length - 1); + } + + ret.set(key, value); + } + } + + return ret; +} + + export function getCurrentPlatform() { if (process.platform === 'win32') { return Platform.Windows; @@ -32,35 +57,14 @@ export function getCurrentPlatform() { // For details: https://www.freedesktop.org/software/systemd/man/os-release.html // When any new distro or version is added, please update GetClrDbg.sh in MIEngine or inform the contributers of MIEngine. const text = child_process.execSync('cat /etc/os-release').toString(); - const lines = text.split('\n'); - - function getValue(name: string) { - for (let line of lines) { - line = line.trim(); - if (line.startsWith(name)) { - const equalsIndex = line.indexOf('='); - if (equalsIndex >= 0) { - let value = line.substring(equalsIndex + 1); - - // Strip double quotes if necessary - if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) { - value = value.substring(1, value.length - 1); - } - - return value; - } - } - } - - return undefined; - } + const osReleaseMap = convertOSReleaseTextToMap(text); - const id = getValue("ID"); + const id = osReleaseMap.get("ID"); switch (id) { case 'ubuntu': - const versionId = getValue("VERSION_ID"); + const versionId = osReleaseMap.get("VERSION_ID"); if (versionId.startsWith("14")) { // This also works for Linux Mint return Platform.Ubuntu14; @@ -84,7 +88,7 @@ export function getCurrentPlatform() { // Oracle Linux is binary compatible with CentOS return Platform.CentOS; case 'elementary OS': - const eOSVersionId = getValue("VERSION_ID"); + const eOSVersionId = osReleaseMap.get("VERSION_ID"); if (eOSVersionId.startsWith("0.3")) { // Elementary OS 0.3 Freya is binary compatible with Ubuntu 14.04 return Platform.Ubuntu14; @@ -96,7 +100,7 @@ export function getCurrentPlatform() { break; case 'linuxmint': - const lmVersionId = getValue("VERSION_ID"); + const lmVersionId = osReleaseMap.get("VERSION_ID"); if (lmVersionId.startsWith("18")) { // Linux Mint 18 is binary compatible with Ubuntu 16.04 return Platform.Ubuntu16;