-
Notifications
You must be signed in to change notification settings - Fork 459
/
platform.ts
396 lines (340 loc) · 11.4 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as semver from 'semver';
import * as plist from 'plist';
const unknown = 'unknown';
export enum Runtime {
UnknownRuntime = <any>'Unknown',
UnknownVersion = <any>'Unknown',
Windows_7_86 = <any>'Windows_7_86',
Windows_7_64 = <any>'Windows_7_64',
OSX_10_11_64 = <any>'OSX_10_11_64',
CentOS_7 = <any>'CentOS_7',
Debian_8 = <any>'Debian_8',
Fedora_23 = <any>'Fedora_23',
OpenSUSE_13_2 = <any>'OpenSUSE_13_2',
SLES_12_2 = <any>'SLES_12_2',
RHEL_7 = <any>'RHEL_7',
Ubuntu_14 = <any>'Ubuntu_14',
Ubuntu_16 = <any>'Ubuntu_16'
}
export function getRuntimeDisplayName(runtime: Runtime): string {
switch (runtime) {
case Runtime.Windows_7_64:
return 'Windows';
case Runtime.Windows_7_86:
return 'Windows';
case Runtime.OSX_10_11_64:
return 'OSX';
case Runtime.CentOS_7:
return 'CentOS';
case Runtime.Debian_8:
return 'Debian';
case Runtime.Fedora_23:
return 'Fedora';
case Runtime.OpenSUSE_13_2:
return 'OpenSUSE';
case Runtime.SLES_12_2:
return 'SLES';
case Runtime.RHEL_7:
return 'RHEL';
case Runtime.Ubuntu_14:
return 'Ubuntu14';
case Runtime.Ubuntu_16:
return 'Ubuntu16';
default:
return 'Unknown';
}
}
/**
* There is no standard way on Linux to find the distribution name and version.
* Recently, systemd has pushed to standardize the os-release file. This has
* seen adoption in "recent" versions of all major distributions.
* https://www.freedesktop.org/software/systemd/man/os-release.html
*/
export class LinuxDistribution {
public constructor(
public name: string,
public version: string,
public idLike?: string[]) { }
public static getCurrent(): Promise<LinuxDistribution> {
// Try /etc/os-release and fallback to /usr/lib/os-release per the synopsis
// at https://www.freedesktop.org/software/systemd/man/os-release.html.
return LinuxDistribution.fromFilePath('/etc/os-release')
.catch(() => LinuxDistribution.fromFilePath('/usr/lib/os-release'))
.catch(() => Promise.resolve(new LinuxDistribution(unknown, unknown)));
}
public toString(): string {
return `name=${this.name}, version=${this.version}`;
}
private static fromFilePath(filePath: string): Promise<LinuxDistribution> {
return new Promise<LinuxDistribution>((resolve, reject) => {
fs.readFile(filePath, 'utf8', (error, data) => {
if (error) {
reject(error);
} else {
resolve(LinuxDistribution.fromReleaseInfo(data));
}
});
});
}
public static fromReleaseInfo(releaseInfo: string, eol: string = os.EOL): LinuxDistribution {
let name = unknown;
let version = unknown;
let idLike: string[] = undefined;
const lines = releaseInfo.split(eol);
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 quotes if necessary
if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) {
value = value.substring(1, value.length - 1);
} else if (value.length > 1 && value.startsWith('\'') && value.endsWith('\'')) {
value = value.substring(1, value.length - 1);
}
if (key === 'ID') {
name = value;
} else if (key === 'VERSION_ID') {
version = value;
} else if (key === 'ID_LIKE') {
idLike = value.split(' ');
}
if (name !== unknown && version !== unknown && idLike !== undefined) {
break;
}
}
}
return new LinuxDistribution(name, version, idLike);
}
}
export class PlatformInformation {
public runtimeId: Runtime;
public constructor(
public platform: string,
public architecture: string,
public distribution: LinuxDistribution = undefined) {
try {
this.runtimeId = PlatformInformation.getRuntimeId(platform, architecture, distribution);
} catch (err) {
this.runtimeId = undefined;
}
}
public get isWindows(): boolean {
return this.platform === 'win32';
}
public get isMacOS(): boolean {
return this.platform === 'darwin';
}
public get isLinux(): boolean {
return this.platform === 'linux';
}
public get isValidRuntime(): boolean {
return this.runtimeId !== undefined && this.runtimeId !== Runtime.UnknownRuntime && this.runtimeId !== Runtime.UnknownVersion;
}
public getRuntimeDisplayName(): string {
return getRuntimeDisplayName(this.runtimeId);
}
public isMacVersionLessThan(version: string): boolean {
if (this.isMacOS) {
try {
let versionInfo = plist.parse(fs.readFileSync('/System/Library/CoreServices/SystemVersion.plist', 'utf-8'));
if (versionInfo && versionInfo['ProductVersion'] && semver.lt(versionInfo['ProductVersion'], version)) {
return true;
}
} catch (e) {
// do nothing for now. Assume version is supported
}
}
return false;
}
public toString(): string {
let result = this.platform;
if (this.architecture) {
if (result) {
result += ', ';
}
result += this.architecture;
}
if (this.distribution) {
if (result) {
result += ', ';
}
result += this.distribution.toString();
}
return result;
}
public static getCurrent(): Promise<PlatformInformation> {
let platform = os.platform();
let architecturePromise: Promise<string>;
let distributionPromise: Promise<LinuxDistribution>;
switch (platform) {
case 'win32':
architecturePromise = PlatformInformation.getWindowsArchitecture();
distributionPromise = Promise.resolve(undefined);
break;
case 'darwin':
architecturePromise = PlatformInformation.getUnixArchitecture();
distributionPromise = Promise.resolve(undefined);
break;
case 'linux':
architecturePromise = PlatformInformation.getUnixArchitecture();
distributionPromise = LinuxDistribution.getCurrent();
break;
default:
throw new Error(`Unsupported platform: ${platform}`);
}
return architecturePromise.then(arch => {
return distributionPromise.then(distro => {
return new PlatformInformation(platform, arch, distro);
});
});
}
private static getWindowsArchitecture(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (process.env.PROCESSOR_ARCHITECTURE === 'x86' && process.env.PROCESSOR_ARCHITEW6432 === undefined) {
resolve('x86');
} else {
resolve('x86_64');
}
});
}
private static getUnixArchitecture(): Promise<string> {
return this.execChildProcess('uname -m')
.then(architecture => {
if (architecture) {
return architecture.trim();
}
return undefined;
});
}
private static execChildProcess(process: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
child_process.exec(process, { maxBuffer: 500 * 1024 }, (error: Error, stdout: string, stderr: string) => {
if (error) {
reject(error);
return;
}
if (stderr && stderr.length > 0) {
reject(new Error(stderr));
return;
}
resolve(stdout);
});
});
}
/**
* Returns a supported .NET Core Runtime ID (RID) for the current platform. The list of Runtime IDs
* is available at https://github.com/dotnet/corefx/tree/master/pkg/Microsoft.NETCore.Platforms.
*/
private static getRuntimeId(platform: string, architecture: string, distribution: LinuxDistribution): Runtime {
// Note: We could do much better here. Currently, we only return a limited number of RIDs that
// are officially supported.
switch (platform) {
case 'win32':
switch (architecture) {
case 'x86': return Runtime.Windows_7_86;
case 'x86_64': return Runtime.Windows_7_64;
default:
}
throw new Error(`Unsupported Windows architecture: ${architecture}`);
case 'darwin':
switch (architecture) {
// Note: We return the El Capitan RID for Sierra
case 'x86_64': return Runtime.OSX_10_11_64;
case 'arm64': return Runtime.OSX_10_11_64;
default:
}
throw new Error(`Unsupported macOS architecture: ${architecture}`);
case 'linux':
if (architecture === 'x86_64') {
// First try the distribution name
let runtimeId = PlatformInformation.getRuntimeIdHelper(distribution.name, distribution.version);
// If the distribution isn't one that we understand, but the 'ID_LIKE' field has something that we understand, use that
//
// NOTE: 'ID_LIKE' doesn't specify the version of the 'like' OS. So we will use the 'VERSION_ID' value. This will restrict
// how useful ID_LIKE will be since it requires the version numbers to match up, but it is the best we can do.
if (runtimeId === Runtime.UnknownRuntime && distribution.idLike && distribution.idLike.length > 0) {
for (let id of distribution.idLike) {
runtimeId = PlatformInformation.getRuntimeIdHelper(id, distribution.version);
if (runtimeId !== Runtime.UnknownRuntime) {
break;
}
}
}
if (runtimeId !== Runtime.UnknownRuntime && runtimeId !== Runtime.UnknownVersion) {
return runtimeId;
}
}
// If we got here, this is not a Linux distro or architecture that we currently support.
throw new Error(`Unsupported Linux distro: ${distribution.name}, ${distribution.version}, ${architecture}`);
default:
// If we got here, we've ended up with a platform we don't support like 'freebsd' or 'sunos'.
// Chances are, VS Code doesn't support these platforms either.
throw Error('Unsupported platform ' + platform);
}
}
private static getRuntimeIdHelper(distributionName: string, distributionVersion: string): Runtime {
switch (distributionName) {
case 'arch':
case 'antergos':
// NOTE: currently Arch Linux seems to be compatible enough with Ubuntu 16 that this works,
// though in the future this may need to change as Arch follows a rolling release model.
return Runtime.Ubuntu_16;
case 'ubuntu':
if (distributionVersion.startsWith('14')) {
// This also works for Linux Mint
return Runtime.Ubuntu_14;
} else if (distributionVersion.startsWith('16')) {
return Runtime.Ubuntu_16;
}
break;
case 'elementary':
case 'elementary OS':
if (distributionVersion.startsWith('0.3')) {
// Elementary OS 0.3 Freya is binary compatible with Ubuntu 14.04
return Runtime.Ubuntu_14;
} else if (distributionVersion.startsWith('0.4')) {
// Elementary OS 0.4 Loki is binary compatible with Ubuntu 16.04
return Runtime.Ubuntu_16;
}
break;
case 'linuxmint':
if (distributionVersion.startsWith('18') || distributionVersion.startsWith('19')) {
// Linux Mint 18 is binary compatible with Ubuntu 16.04
return Runtime.Ubuntu_16;
}
break;
case 'centos':
case 'ol':
// Oracle Linux is binary compatible with CentOS
return Runtime.CentOS_7;
case 'fedora':
return Runtime.Fedora_23;
case 'opensuse':
return Runtime.OpenSUSE_13_2;
case 'sles':
return Runtime.SLES_12_2;
case 'rhel':
return Runtime.RHEL_7;
case 'debian':
case 'deepin':
return Runtime.Debian_8;
case 'galliumos':
if (distributionVersion.startsWith('2.0')) {
return Runtime.Ubuntu_16;
}
break;
default:
return Runtime.Ubuntu_16;
}
return Runtime.Ubuntu_16;
}
}