Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/extension-editing/src/extensionEngineValidation.ts
3291 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
// https://github.com/microsoft/vscode/blob/6cb34eb22385bc93ab25aa2e5113f59c7a2299ac/src/vs/platform/extensions/common/extensionValidator.ts
7
8
export interface IParsedVersion {
9
hasCaret: boolean;
10
hasGreaterEquals: boolean;
11
majorBase: number;
12
majorMustEqual: boolean;
13
minorBase: number;
14
minorMustEqual: boolean;
15
patchBase: number;
16
patchMustEqual: boolean;
17
preRelease: string | null;
18
}
19
20
export interface INormalizedVersion {
21
majorBase: number;
22
majorMustEqual: boolean;
23
minorBase: number;
24
minorMustEqual: boolean;
25
patchBase: number;
26
patchMustEqual: boolean;
27
notBefore: number; /* milliseconds timestamp, or 0 */
28
isMinimum: boolean;
29
}
30
31
const VERSION_REGEXP = /^(\^|>=)?((\d+)|x)\.((\d+)|x)\.((\d+)|x)(\-.*)?$/;
32
const NOT_BEFORE_REGEXP = /^-(\d{4})(\d{2})(\d{2})$/;
33
34
export function isValidVersionStr(version: string): boolean {
35
version = version.trim();
36
return (version === '*' || VERSION_REGEXP.test(version));
37
}
38
39
export function parseVersion(version: string): IParsedVersion | null {
40
if (!isValidVersionStr(version)) {
41
return null;
42
}
43
44
version = version.trim();
45
46
if (version === '*') {
47
return {
48
hasCaret: false,
49
hasGreaterEquals: false,
50
majorBase: 0,
51
majorMustEqual: false,
52
minorBase: 0,
53
minorMustEqual: false,
54
patchBase: 0,
55
patchMustEqual: false,
56
preRelease: null
57
};
58
}
59
60
const m = version.match(VERSION_REGEXP);
61
if (!m) {
62
return null;
63
}
64
return {
65
hasCaret: m[1] === '^',
66
hasGreaterEquals: m[1] === '>=',
67
majorBase: m[2] === 'x' ? 0 : parseInt(m[2], 10),
68
majorMustEqual: (m[2] === 'x' ? false : true),
69
minorBase: m[4] === 'x' ? 0 : parseInt(m[4], 10),
70
minorMustEqual: (m[4] === 'x' ? false : true),
71
patchBase: m[6] === 'x' ? 0 : parseInt(m[6], 10),
72
patchMustEqual: (m[6] === 'x' ? false : true),
73
preRelease: m[8] || null
74
};
75
}
76
77
export function normalizeVersion(version: IParsedVersion | null): INormalizedVersion | null {
78
if (!version) {
79
return null;
80
}
81
82
const majorBase = version.majorBase;
83
const majorMustEqual = version.majorMustEqual;
84
const minorBase = version.minorBase;
85
let minorMustEqual = version.minorMustEqual;
86
const patchBase = version.patchBase;
87
let patchMustEqual = version.patchMustEqual;
88
89
if (version.hasCaret) {
90
if (majorBase === 0) {
91
patchMustEqual = false;
92
} else {
93
minorMustEqual = false;
94
patchMustEqual = false;
95
}
96
}
97
98
let notBefore = 0;
99
if (version.preRelease) {
100
const match = NOT_BEFORE_REGEXP.exec(version.preRelease);
101
if (match) {
102
const [, year, month, day] = match;
103
notBefore = Date.UTC(Number(year), Number(month) - 1, Number(day));
104
}
105
}
106
107
return {
108
majorBase: majorBase,
109
majorMustEqual: majorMustEqual,
110
minorBase: minorBase,
111
minorMustEqual: minorMustEqual,
112
patchBase: patchBase,
113
patchMustEqual: patchMustEqual,
114
isMinimum: version.hasGreaterEquals,
115
notBefore,
116
};
117
}
118
119