-
Notifications
You must be signed in to change notification settings - Fork 2
/
maven-types.js
193 lines (179 loc) · 6.87 KB
/
maven-types.js
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
const semver = require('semver')
const MavenContentSource = require('./maven-content-source')
const selectVersion = require('./version-selector');
class MavenRepository {
baseUrl;
fetchOptions;
constructor({baseUrl, fetchOptions}) {
this.baseUrl = baseUrl;
this.fetchOptions = fetchOptions;
}
toString() {
return 'MavenRepository(' + this.baseUrl + ')'
}
}
class MavenArtifact {
groupId;
artifactId;
version;
extension;
classifier;
constructor({groupId, artifactId, version, classifier = 'docs', extension = 'zip'}) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.classifier = classifier;
this.extension = extension;
}
toString() {
return 'MavenArtifact(' + this.groupId + ':' + this.artifactId + ':' + this.version
+ (this.classifier ? ':' + this.classifier : '')
+ (this.extension ? '@' + this.extension : '')
+ ')';
}
}
class MavenContentCoordinate {
groupId;
artifactId;
versionRange;
version;
extension;
classifier;
limit;
limitBy;
includeSnapshots;
startPath;
startPaths;
constructor({
groupId,
artifactId,
version = "*",
classifier = 'docs',
extension = 'zip',
limit = 1,
limitBy = 'major',
includeSnapshots = false,
startPath = null,
startPaths = null,
includePrerelease = true
}) {
this.groupId = groupId;
this.artifactId = artifactId;
this.includeSnapshots = includeSnapshots;
this.startPath = startPath;
this.startPaths = startPaths;
this.version = version;
this.versionRange = new semver.Range(version, {loose: false, includePrerelease});
this.classifier = classifier;
this.extension = extension;
this.limit = limit;
this.limitBy = limitBy;
if (!['major', 'minor', 'patch', 'any'].includes(this.limitBy)) {
throw new TypeError("limitBy must be one of 'major', 'minor', 'patch', 'any'");
}
}
toString() {
return 'MavenContentCoordinate(' + this.groupId + ':' + this.artifactId + ':[' + this.versionRange + ']'
+ (this.classifier ? ':' + this.classifier : '')
+ (this.extension ? '@' + this.extension : '')
+ (limit !== 1 ? ', up to ' + limit + ' versions' : '')
+ (this.includeSnapshots ? ' including ' : ' excluding ') + 'snapshots'
+ ', selected by "' + this.regExp + '")';
}
/**
*
* @param {MavenClient} mavenClient
* @param {MavenRepository[]} repositories
* @param {Git} git
* @param {string} cacheFolder
* @param logger
* @returns {Promise<MavenContentSource[]>}
*/
async resolveToContentSources(mavenClient, repositories, git, cacheFolder, logger) {
let versionRepoTupleList = [];
try {
versionRepoTupleList = await mavenClient.retrieveAvailableVersions(repositories, {
groupId: this.groupId,
artifactId: this.artifactId,
fallback: {
version: this.version,
classifier: this.classifier,
extension: this.extension
}
})
} catch (e) {
logger.warn('failure when trying to download available versions for ' + this.groupId + ':' + this.artifactId + ' (' + e.message + '). Skipping.');
}
if (!this.includeSnapshots) {
versionRepoTupleList = versionRepoTupleList.filter(({
version,
repository
}) => !version.endsWith('-SNAPSHOT'))
}
return this.createSourcesForMatchingVersions(versionRepoTupleList, mavenClient, git, cacheFolder, logger);
}
createSourcesForMatchingVersions(versionRepoTupleList, mavenClient, git, cacheFolder, logger) {
let results = [];
selectVersion(this.versionRange, this.limit, this.limitBy, versionRepoTupleList, versionRepoTuple => {
results.push(new MavenContentSource({
mavenClient: mavenClient,
logger: mavenClient.logger,
repository: versionRepoTuple.repository,
git: git,
cacheFolder: cacheFolder,
startPath: this.startPath,
startPaths: this.startPaths,
mavenArtifact: new MavenArtifact({
groupId: this.groupId,
artifactId: this.artifactId,
version: versionRepoTuple.version,
classifier: this.classifier,
extension: this.extension
})
}
))
}, logger);
return results;
}
/**
* @param {{repository: MavenRepository, version: string}[]} versionRepoTupleList
* @param {function({repository: MavenRepository, version: string})} consumer
* @param logger
*/
forEachSelectedTuple(versionRepoTupleList, consumer, logger) {
let usedVersionFragments = new Set();
let idx = 0;
do {
let candidate = versionRepoTupleList[idx];
if (this.versionRange.test(candidate.version)) {
const candidateFragment = this.#extractVersionFragment(candidate.version);
if (!usedVersionFragments.has(candidateFragment)) {
consumer(candidate);
usedVersionFragments.add(candidateFragment);
} else {
logger.debug('Skipping version ' + candidate.version + ' of ' + this + ': version fragment already present');
}
} else {
logger.debug('Skipping version ' + candidate.version + ' of ' + this + ': does not match ' + this.versionRange);
}
} while (++idx < versionRepoTupleList.length && usedVersionFragments.length < this.limit)
}
#extractVersionFragment(version) {
let semanticVersion = semver.parse(version);
switch (this.limitBy) {
case 'major':
return semanticVersion.major;
case 'minor':
return semanticVersion.major + '.' + semanticVersion.minor;
case 'patch':
return semanticVersion.major + '.' + semanticVersion.minor + '.' + semanticVersion.patch;
default:
return version;
}
}
}
module.exports = {
MavenRepository,
MavenArtifact,
MavenContentCoordinate
}