-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathindex.ts
More file actions
191 lines (158 loc) · 4.73 KB
/
index.ts
File metadata and controls
191 lines (158 loc) · 4.73 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
import { satisfies } from 'semver';
import type { RegExpVersion } from '../regex/index.ts';
import { RegExpVersioningApi } from '../regex/index.ts';
import type { VersioningApiConstructor } from '../types.ts';
export const id = 'hermit';
export const displayName = 'Hermit';
export const urls = [
'https://cashapp.github.io/hermit/packaging/reference/#versions',
];
export const supportsRanges = false;
export class HermitVersioning extends RegExpVersioningApi {
// add <supplement> element to accomondate openjdk versioning defined in JEP322
// https://openjdk.org/jeps/322
static versionRegex =
'^(?<major>\\d+)(\\.(?<minor>\\d+))?(\\.(?<patch>\\d+))?(\\.(?<supplement>\\d+))?(_(?<build>\\d+))?([-]?(?<prerelease>[^.+][^+]*))?([+](?<compatibility>[^.-][^+]*))?$';
public constructor() {
super(HermitVersioning.versionRegex);
}
private _isValid(version: string): boolean {
return super._parse(version) !== null;
}
private _parseHermitVersioning(version: string): RegExpVersion | null {
const groups = this._config?.exec(version)?.groups;
if (!groups) {
return null;
}
const {
major,
minor,
patch,
supplement,
build,
prerelease,
compatibility,
} = groups;
const release = [
Number.parseInt(major, 10),
typeof minor === 'undefined' ? 0 : Number.parseInt(minor, 10),
typeof patch === 'undefined' ? 0 : Number.parseInt(patch, 10),
typeof supplement === 'undefined' ? 0 : Number.parseInt(supplement, 10),
];
if (build) {
release.push(Number.parseInt(build, 10));
}
return {
release,
prerelease,
compatibility,
};
}
protected override _parse(version: string): RegExpVersion | null {
const parsed = this._parseHermitVersioning(version);
if (parsed) {
return parsed;
}
const channelVer = HermitVersioning._getChannel(version);
const groups = this._config?.exec(channelVer)?.groups;
if (!groups) {
return null;
}
const {
major,
minor,
patch,
supplement,
build,
prerelease,
compatibility,
} = groups;
const release = [];
if (major) {
release.push(Number.parseInt(major, 10));
}
if (minor) {
release.push(Number.parseInt(minor, 10));
}
if (patch) {
release.push(Number.parseInt(patch, 10));
}
if (supplement) {
release.push(Number.parseInt(supplement, 10));
}
if (build) {
release.push(Number.parseInt(build, 10));
}
return {
release,
prerelease,
compatibility,
};
}
private static _isChannel(version: string): boolean {
return version.startsWith('@');
}
private static _getChannel(version: string): string {
return version.substring(1);
}
override isStable(version: string): boolean {
if (this._isValid(version)) {
return super.isStable(version);
}
// channel and the rest should be considered unstable version
// as channels are changing values
return false;
}
override isValid(version: string): boolean {
return this._isValid(version) || HermitVersioning._isChannel(version);
}
override isLessThanRange(version: string, range: string): boolean {
return this._compare(version, range) < 0;
}
protected override _compare(version: string, other: string): number {
if (this._isValid(version) && this._isValid(other)) {
return super._compare(version, other);
}
const parsedVersion = this._parse(version);
const parsedOther = this._parse(other);
if (parsedVersion === null || parsedOther === null) {
if (parsedVersion === null && parsedOther === null) {
return version.localeCompare(other);
}
return parsedVersion === null ? -1 : 1;
}
const versionReleases = parsedVersion.release;
const otherReleases = parsedOther.release;
const maxLength =
versionReleases.length > otherReleases.length
? versionReleases.length
: otherReleases.length;
for (let i = 0; i < maxLength; i++) {
const verVal = versionReleases[i];
const otherVal = otherReleases[i];
if (
verVal !== undefined &&
otherVal !== undefined &&
verVal !== otherVal
) {
return verVal - otherVal;
} else if (verVal === undefined) {
return 1;
} else if (otherVal === undefined) {
return -1;
}
}
return 0;
}
override matches(version: string, range: string): boolean {
if (
HermitVersioning._isChannel(version) ||
HermitVersioning._isChannel(range)
) {
return this.equals(version, range);
}
return satisfies(version, range);
}
}
export const api: VersioningApiConstructor = HermitVersioning;
export default api;