forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.ts
More file actions
239 lines (213 loc) · 6.1 KB
/
platform.ts
File metadata and controls
239 lines (213 loc) · 6.1 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as getos from 'getos';
import * as os from 'os';
import * as semver from 'semver';
import { IPlatformInfo } from '../client/common/platform/types';
import { parseVersion } from './version';
export enum Architecture {
Unknown = 1,
x86 = 2,
x64 = 3
}
export enum OSType {
Unknown,
Windows,
OSX,
Linux
}
export enum OSDistro {
Unknown,
// linux:
Ubuntu,
Debian,
RHEL,
Fedora,
CentOS,
// The remainder aren't officially supported.
// See: https://code.visualstudio.com/docs/supporting/requirements
Suse,
Gentoo,
Arch
}
let local: Info;
function getLocal(): Info {
if (!local) {
local = getInfo();
}
return local;
}
export 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 {
return OSType.Unknown;
}
}
export class Info implements IPlatformInfo {
constructor(
public readonly type: OSType,
private readonly arch: string = os.arch(),
// See:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/semver/index.d.ts#L152
public readonly version: semver.SemVer = new semver.SemVer('0.0.0'),
public readonly distro: OSDistro = OSDistro.Unknown
) { }
public get architecture(): Architecture {
return this.arch === 'x64' ? Architecture.x64 : Architecture.x86;
}
public matchPlatform(names: string): boolean {
return matchPlatform(names, this);
}
}
export function getInfo(
getArch: () => string = os.arch,
getRelease: () => string = os.release,
getDistro: () => [OSDistro, semver.SemVer] = getLinuxDistro,
platform?: string
): Info {
const osType = getOSType(platform);
const arch = getArch();
switch (osType) {
case OSType.Windows:
return getDefaultInfo(osType, arch, getRelease);
case OSType.OSX:
return getDefaultInfo(osType, arch, getRelease);
case OSType.Linux:
return getLinuxInfo(arch, getDistro);
default:
return new Info(OSType.Unknown, arch);
}
}
function getDefaultInfo(osType: OSType, arch: string, getRelease: () => string): Info {
const version = parseVersion(getRelease());
return new Info(osType, arch, version);
}
function getLinuxInfo(arch: string, getDistro: () => [OSDistro, semver.SemVer]): Info {
const [distro, version] = getDistro();
return new Info(OSType.Linux, arch, version, distro);
}
function getLinuxDistro(): [OSDistro, semver.SemVer] {
let distro: OSDistro = OSDistro.Unknown;
let version: semver.SemVer = new semver.SemVer('0.0.0');
getos((exc, info) => {
if (exc) {
throw exc;
}
distro = getLinuxDistroFromName(info.dist);
version = parseVersion(info.release);
});
return [distro, version];
}
function getLinuxDistroFromName(name: string): OSDistro {
name = name.toLowerCase();
// See https://github.com/zyga/os-release-zoo.
if (/ubuntu/.test(name)) {
return OSDistro.Ubuntu;
} else if (/debian/.test(name)) {
return OSDistro.Debian;
} else if (/rhel/.test(name) || /red hat/.test(name)) {
return OSDistro.RHEL;
} else if (/fedora/.test(name)) {
return OSDistro.Fedora;
} else if (/centos/.test(name)) {
return OSDistro.CentOS;
}
// The remainder aren't officially supported by VS Code.
if (/suse/.test(name)) {
return OSDistro.Suse;
} else if (/gentoo/.test(name)) {
return OSDistro.Suse;
} else if (/arch/.test(name)) {
return OSDistro.Arch;
} else {
return OSDistro.Unknown;
}
}
// helpers
export function isWindows(info?: Info): boolean {
if (!info) {
info = getLocal();
}
return info.type === OSType.Windows;
}
export function isMac(info?: Info): boolean {
if (!info) {
info = getLocal();
}
return info.type === OSType.OSX;
}
export function isLinux(info?: Info): boolean {
if (!info) {
info = getLocal();
}
return info.type === OSType.Linux;
}
export function is64bit(info?: Info): boolean {
if (!info) {
info = getLocal();
}
return info.architecture === Architecture.x64;
}
// Match the platform string to the given OS info.
export function matchPlatform(names: string, info: Info = getInfo()): boolean {
if (info.type === OSType.Unknown) {
return false;
}
names = names.trim();
if (names === '') {
return true;
}
for (let name of names.split('|')) {
name = name.trim();
if (matchOnePlatform(name, info)) {
return true;
}
}
return false;
}
function matchOnePlatform(name: string, info: Info): boolean {
if (name === '' || name === '-') {
return false;
}
const negate = name[0] === '-';
if (negate) {
name = name.replace(/^-/, '');
}
const [osType, distro] = identifyOS(name);
if (osType === OSType.Unknown) {
return false;
}
let result = false;
if (osType === info.type) {
result = true;
if (osType === OSType.Linux) {
if (distro !== OSDistro.Unknown) {
result = distro === info.distro;
}
}
}
return negate ? !result : result;
}
function identifyOS(name: string): [OSType, OSDistro] {
name = name.toLowerCase();
if (/win/.test(name)) {
return [OSType.Windows, OSDistro.Unknown];
} else if (/darwin|mac|osx/.test(name)) {
return [OSType.OSX, OSDistro.Unknown];
} else if (/linux/.test(name)) {
return [OSType.Linux, OSDistro.Unknown];
}
// Try linux distros.
const distro = getLinuxDistroFromName(name);
if (distro !== OSDistro.Unknown) {
return [OSType.Linux, distro];
} else {
return [OSType.Unknown, OSDistro.Unknown];
}
}