forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatformService.ts
More file actions
81 lines (77 loc) · 2.95 KB
/
platformService.ts
File metadata and controls
81 lines (77 loc) · 2.95 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { injectable } from 'inversify';
import * as os from 'os';
import { coerce, SemVer } from 'semver';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName, PlatformErrors } from '../../telemetry/constants';
import { OSType } from '../utils/platform';
import { parseVersion } from '../utils/version';
import { NON_WINDOWS_PATH_VARIABLE_NAME, WINDOWS_PATH_VARIABLE_NAME } from './constants';
import { IPlatformService } from './types';
@injectable()
export class PlatformService implements IPlatformService {
public readonly osType: OSType = getOSType();
public version?: SemVer;
public get pathVariableName() {
return this.isWindows ? WINDOWS_PATH_VARIABLE_NAME : NON_WINDOWS_PATH_VARIABLE_NAME;
}
public get virtualEnvBinName() {
return this.isWindows ? 'Scripts' : 'bin';
}
public async getVersion(): Promise<SemVer> {
if (this.version) {
return this.version;
}
switch (this.osType) {
case OSType.Windows:
case OSType.OSX:
// Release section of https://en.wikipedia.org/wiki/MacOS_Sierra.
// Version 10.12 maps to Darwin 16.0.0.
// Using os.relase() we get the darwin release #.
try {
const ver = coerce(os.release());
if (ver) {
sendTelemetryEvent(EventName.PLATFORM_INFO, undefined, { osVersion: `${ver.major}.${ver.minor}.${ver.patch}` });
return this.version = ver;
}
throw new Error('Unable to parse version');
} catch (ex) {
sendTelemetryEvent(EventName.PLATFORM_INFO, undefined, { failureType: PlatformErrors.FailedToParseVersion });
return parseVersion(os.release());
}
default:
throw new Error('Not Supported');
}
}
public get isWindows(): boolean {
return this.osType === OSType.Windows;
}
public get isMac(): boolean {
return this.osType === OSType.OSX;
}
public get isLinux(): boolean {
return this.osType === OSType.Linux;
}
public get osRelease(): string {
return os.release();
}
public get is64bit(): boolean {
// tslint:disable-next-line:no-require-imports
const arch = require('arch');
return arch() === 'x64';
}
}
function getOSType(platform: string = process.platform): OSType {
if (/^win/.test(platform)) {
return OSType.Windows;
} else if (/^darwin/.test(platform)) {
return OSType.OSX;
} else if (/^linux/.test(platform)) {
return OSType.Linux;
} else {
sendTelemetryEvent(EventName.PLATFORM_INFO, undefined, { failureType: PlatformErrors.FailedToDetermineOS });
return OSType.Unknown;
}
}