forked from EclipseFdn/publish-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-extensions.js
265 lines (239 loc) · 10.5 KB
/
publish-extensions.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/********************************************************************************
* Copyright (c) 2020 TypeFox and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
// @ts-check
const fs = require('fs');
const cp = require('child_process');
const { getPublicGalleryAPI } = require('vsce/out/util');
const { PublicGalleryAPI } = require('vsce/out/publicgalleryapi');
const { ExtensionQueryFlags, PublishedExtension } = require('azure-devops-node-api/interfaces/GalleryInterfaces');
const semver = require('semver');
const Ajv = require("ajv").default;
const resolveExtension = require('./lib/resolveExtension').resolveExtension;
const exec = require('./lib/exec');
const msGalleryApi = getPublicGalleryAPI();
msGalleryApi.client['_allowRetries'] = true;
msGalleryApi.client['_maxRetries'] = 5;
const openGalleryApi = new PublicGalleryAPI('https://open-vsx.org/vscode', '3.0-preview.1');
openGalleryApi.client['_allowRetries'] = true;
openGalleryApi.client['_maxRetries'] = 5;
openGalleryApi.post = (url, data, additionalHeaders) =>
openGalleryApi.client.post(`${openGalleryApi.baseUrl}${url}`, data, additionalHeaders);
const flags = [
ExtensionQueryFlags.IncludeStatistics,
ExtensionQueryFlags.IncludeVersions,
ExtensionQueryFlags.IncludeVersionProperties
];
/**
* Checks whether the provided `version` is a prerelase or not
* @param {Readonly<import('./types').IRawGalleryExtensionProperty[]>} version
* @returns
*/
function isPreReleaseVersion(version) {
const values = version ? version.filter(p => p.key === 'Microsoft.VisualStudio.Code.PreRelease') : [];
return values.length > 0 && values[0].value === 'true';
}
(async () => {
// Make yarn use bash
exec('yarn config set script-shell /bin/bash');
// Don't show large git advice blocks
exec('git config --global advice.detachedHead false');
/**
* @type {string[] | undefined}
*/
let toVerify = undefined;
if (process.env.EXTENSIONS) {
toVerify = process.env.EXTENSIONS.split(',').map(s => s.trim());
}
/**
* @type {Readonly<import('./types').Extensions>}
*/
const extensions = JSON.parse(await fs.promises.readFile('./extensions.json', 'utf-8'));
// Validate that extensions.json
const JSONSchema = JSON.parse(await fs.promises.readFile('./extensions-schema.json', 'utf-8'));
const ajv = new Ajv();
const validate = ajv.compile(JSONSchema);
const valid = validate(extensions);
if (!valid) {
console.error('extensions.json is invalid:');
console.error(validate.errors);
process.exit(1);
}
// Also install extensions' devDependencies when using `npm install` or `yarn install`.
process.env.NODE_ENV = 'development';
/** @type{import('./types').PublishStat}*/
const stat = {
upToDate: {},
outdated: {},
unstable: {},
notInOpen: {},
notInMS: [],
failed: [],
msPublished: {},
hitMiss: {},
resolutions: {}
}
const msPublishers = new Set(['ms-python', 'ms-toolsai', 'ms-vscode', 'dbaeumer', 'GitHub', 'Tyriar', 'ms-azuretools', 'msjsdiag', 'ms-mssql', 'vscjava', 'ms-vsts']);
const monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
for (const id in extensions) {
if (id === '$schema') {
continue;
}
if (toVerify && toVerify.indexOf(id) === -1) {
continue;
}
const extension = Object.freeze({ id, ...extensions[id] });
/** @type {import('./types').PublishContext} */
const context = {};
let timeoutDelay = Number(extension.timeout);
if (!Number.isInteger(timeoutDelay)) {
timeoutDelay = 5;
}
try {
/** @type {[PromiseSettledResult<PublishedExtension | undefined>]} */
let [msExtension] = await Promise.allSettled([msGalleryApi.getExtension(extension.id, flags)]);
if (msExtension.status === 'fulfilled') {
const lastNonPrereleaseVersion = msExtension.value?.versions.find(version => !isPreReleaseVersion(version.properties));
context.msVersion = lastNonPrereleaseVersion?.version;
context.msLastUpdated = lastNonPrereleaseVersion?.lastUpdated;
context.msInstalls = msExtension.value?.statistics?.find(s => s.statisticName === 'install')?.value;
context.msPublisher = msExtension.value?.publisher.publisherName;
}
if (msPublishers.has(context.msPublisher)) {
stat.msPublished[extension.id] = { msInstalls: context.msInstalls, msVersion: context.msVersion };
}
async function updateStat() {
/** @type {[PromiseSettledResult<PublishedExtension | undefined>]} */
const [ovsxExtension] = await Promise.allSettled([openGalleryApi.getExtension(extension.id, flags)]);
if (ovsxExtension.status === 'fulfilled') {
context.ovsxVersion = ovsxExtension.value?.versions[0]?.version;
context.ovsxLastUpdated = ovsxExtension.value?.versions[0]?.lastUpdated;
}
const daysInBetween = context.ovsxLastUpdated && context.msLastUpdated ? ((context.ovsxLastUpdated.getTime() - context.msLastUpdated.getTime()) / (1000 * 3600 * 24)) : undefined;
const extStat = { msInstalls: context.msInstalls, msVersion: context.msVersion, openVersion: context.ovsxVersion, daysInBetween };
const i = stat.notInMS.indexOf(extension.id);
if (i !== -1) {
stat.notInMS.splice(i, 1);
}
delete stat.notInOpen[extension.id];
delete stat.upToDate[extension.id];
delete stat.outdated[extension.id];
delete stat.unstable[extension.id];
delete stat.hitMiss[extension.id];
if (!context.msVersion) {
stat.notInMS.push(extension.id);
} else if (!context.ovsxVersion) {
stat.notInOpen[extension.id] = extStat;
} else if (semver.eq(context.msVersion, context.ovsxVersion)) {
stat.upToDate[extension.id] = extStat;
} else if (semver.gt(context.msVersion, context.ovsxVersion)) {
stat.outdated[extension.id] = extStat;
} else if (semver.lt(context.msVersion, context.ovsxVersion)) {
stat.unstable[extension.id] = extStat;
}
if (context.msVersion && context.msLastUpdated && monthAgo.getTime() <= context.msLastUpdated.getTime()) {
stat.hitMiss[extension.id] = extStat;
}
}
await updateStat();
await exec('rm -rf /tmp/repository /tmp/download', { quiet: true });
const resolved = await resolveExtension(extension, context.msVersion && {
version: context.msVersion,
lastUpdated: context.msLastUpdated
});
stat.resolutions[extension.id] = {
msInstalls: context.msInstalls,
msVersion: context.msVersion,
...resolved?.resolution
};
context.version = resolved?.version;
if (process.env.FORCE !== 'true') {
if (stat.upToDate[extension.id]) {
console.log(`${extension.id}: skipping, since up-to-date`);
continue;
}
if (stat.unstable[extension.id]) {
console.log(`${extension.id}: skipping, since version in Open VSX is never than in MS marketplace`);
continue;
}
if (resolved?.resolution?.latest && context.version === context.ovsxVersion) {
console.log(`${extension.id}: skipping, since very latest commit already published to Open VSX`);
stat.upToDate[extension.id] = stat.outdated[extension.id];
delete stat.outdated[extension.id];
continue;
}
}
if (resolved?.resolution?.releaseAsset) {
console.log(`${extension.id}: resolved ${resolved.resolution.releaseAsset} from release`);
context.file = resolved.path;
} else if (resolved?.resolution?.releaseTag) {
console.log(`${extension.id}: resolved ${resolved.resolution.releaseTag} from release tag`);
context.repo = resolved.path;
context.ref = resolved.resolution.releaseTag;
} else if (resolved?.resolution?.tag) {
console.log(`${extension.id}: resolved ${resolved.resolution.tag} from tags`);
context.repo = resolved.path;
context.ref = resolved.resolution.tag;
} else if (resolved?.resolution?.latest) {
if (context.msVersion) {
console.log(`${extension.id}: resolved ${resolved.resolution.latest} from the very latest commit, since it is not actively maintained`);
} else {
console.log(`${extension.id}: resolved ${resolved.resolution.latest} from the very latest commit, since it is not published to MS marketplace`);
}
context.repo = resolved.path;
context.ref = resolved.resolution.latest;
} else if (resolved?.resolution?.matchedLatest) {
console.log(`${extension.id}: resolved ${resolved.resolution.matchedLatest} from the very latest commit`);
context.repo = resolved.path;
context.ref = resolved.resolution.matchedLatest;
} else if (resolved?.resolution?.matched) {
console.log(`${extension.id}: resolved ${resolved.resolution.matched} from the latest commit on the last update date`);
context.repo = resolved.path;
context.ref = resolved.resolution.matched;
} else {
throw `${extension.id}: failed to resolve`;
}
if (process.env.SKIP_BUILD === 'true') {
continue;
}
let timeout;
await new Promise((resolve, reject) => {
const p = cp.spawn(process.execPath, ['publish-extension.js', JSON.stringify({ extension, context })], {
stdio: ['ignore', 'inherit', 'inherit'],
cwd: process.cwd(),
env: process.env
});
p.on('error', reject);
p.on('exit', code => {
if (code) {
return reject(new Error('failed with exit status: ' + code));
}
resolve();
});
timeout = setTimeout(() => {
try {
p.kill('SIGKILL');
} catch { }
reject(new Error(`timeout after ${timeoutDelay} mins`));
}, timeoutDelay * 60 * 1000);
});
if (timeout !== undefined) {
clearTimeout(timeout);
}
await updateStat();
} catch (error) {
stat.failed.push(extension.id);
console.error(`[FAIL] Could not process extension: ${JSON.stringify({ extension, context }, null, 2)}`);
console.error(error);
}
}
await fs.promises.writeFile("/tmp/stat.json", JSON.stringify(stat), { encoding: 'utf8' });
process.exit();
})();