From 8253257444d502413e863990fb1d117601f20777 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Thu, 6 Oct 2016 11:16:49 -0700 Subject: [PATCH 1/3] Fixing tsc error TS1252 in platform.ts. Moved the getValue function within getCurrentPlatform function outside of function scope. Added string array parameter to getValue function because lines was originally a local variable within the getCurrentPlatform function. --- src/platform.ts | 51 +++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index 5711d5663d..9f4e58eae2 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -20,6 +20,28 @@ export enum Platform { Ubuntu16 } +function getValue(name: string, lines: 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; +} + + export function getCurrentPlatform() { if (process.platform === 'win32') { return Platform.Windows; @@ -34,33 +56,12 @@ export function getCurrentPlatform() { 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 id = getValue("ID"); + const id = getValue("ID", lines); switch (id) { case 'ubuntu': - const versionId = getValue("VERSION_ID"); + const versionId = getValue("VERSION_ID", lines); if (versionId.startsWith("14")) { // This also works for Linux Mint return Platform.Ubuntu14; @@ -84,7 +85,7 @@ export function getCurrentPlatform() { // Oracle Linux is binary compatible with CentOS return Platform.CentOS; case 'elementary OS': - const eOSVersionId = getValue("VERSION_ID"); + const eOSVersionId = getValue("VERSION_ID", lines); if (eOSVersionId.startsWith("0.3")) { // Elementary OS 0.3 Freya is binary compatible with Ubuntu 14.04 return Platform.Ubuntu14; @@ -96,7 +97,7 @@ export function getCurrentPlatform() { break; case 'linuxmint': - const lmVersionId = getValue("VERSION_ID"); + const lmVersionId = getValue("VERSION_ID", lines); if (lmVersionId.startsWith("18")) { // Linux Mint 18 is binary compatible with Ubuntu 16.04 return Platform.Ubuntu16; From 4cf9a767f3c6ecc900f34b7b50718bcfc6d934d2 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Fri, 7 Oct 2016 11:40:15 -0700 Subject: [PATCH 2/3] Changing getValue function to be Map etc/os-release is a file that has multiple 'key=value' lines. Removing the getValue function and then have the text in os-release to be converted to a map instead does the same thing. --- src/platform.ts | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index 9f4e58eae2..ad3f915d32 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -20,25 +20,29 @@ export enum Platform { Ubuntu16 } -function getValue(name: string, lines: string[]) { +function convertOSReleaseTextToMap(text : string) : Map { + let ret: Map = new Map(); + const lines: string[] = text.split('\n'); + 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; + let equalsIndex = line.indexOf('='); + let key = line.substring(0, equalsIndex); + + 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); } + + ret.set(key, value); } } - return undefined; + return ret; } @@ -54,14 +58,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'); + const osReleaseMap = convertOSReleaseTextToMap(text); - const id = getValue("ID", lines); + const id = osReleaseMap.get("ID"); switch (id) { case 'ubuntu': - const versionId = getValue("VERSION_ID", lines); + const versionId = osReleaseMap.get("VERSION_ID"); if (versionId.startsWith("14")) { // This also works for Linux Mint return Platform.Ubuntu14; @@ -85,7 +89,7 @@ export function getCurrentPlatform() { // Oracle Linux is binary compatible with CentOS return Platform.CentOS; case 'elementary OS': - const eOSVersionId = getValue("VERSION_ID", lines); + 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; @@ -97,7 +101,7 @@ export function getCurrentPlatform() { break; case 'linuxmint': - const lmVersionId = getValue("VERSION_ID", lines); + const lmVersionId = osReleaseMap.get("VERSION_ID"); if (lmVersionId.startsWith("18")) { // Linux Mint 18 is binary compatible with Ubuntu 16.04 return Platform.Ubuntu16; From 46eb62b9590e5d18b3ce26806f159ebacb22abe3 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Fri, 7 Oct 2016 12:18:40 -0700 Subject: [PATCH 3/3] Fixing code review issues Moving key variable into if statement. --- src/platform.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index ad3f915d32..b35ceaf9a4 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -28,9 +28,8 @@ function convertOSReleaseTextToMap(text : string) : Map { line = line.trim(); let equalsIndex = line.indexOf('='); - let key = line.substring(0, equalsIndex); - if (equalsIndex >= 0) { + let key = line.substring(0, equalsIndex); let value = line.substring(equalsIndex + 1); // Strip double quotes if necessary