-
Notifications
You must be signed in to change notification settings - Fork 211
/
publish-extensions.js
368 lines (335 loc) · 15.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/********************************************************************************
* 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("@vscode/vsce/out/util");
const { PublicGalleryAPI } = require("@vscode/vsce/out/publicgalleryapi");
const { ExtensionQueryFlags, PublishedExtension } = require("azure-devops-node-api/interfaces/GalleryInterfaces");
const semver = require("semver");
const Ajv = require("ajv/dist/2020").default;
const resolveExtension = require("./lib/resolveExtension").resolveExtension;
const exec = require("./lib/exec");
const { artifactDirectory, registryHost } = require("./lib/constants");
const msGalleryApi = getPublicGalleryAPI();
msGalleryApi.client["_allowRetries"] = true;
msGalleryApi.client["_maxRetries"] = 5;
const openGalleryApi = new PublicGalleryAPI(`https://${registryHost}/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 prerelease 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";
}
const ensureBuildPrerequisites = async () => {
// Make yarn use bash
await exec("yarn config set script-shell /bin/bash");
// Don't show large git advice blocks
await exec("git config --global advice.detachedHead false");
// Create directory for storing built extensions
if (fs.existsSync(artifactDirectory)) {
// If the folder has any files, delete them
try {
fs.rmSync(`${artifactDirectory}*`);
} catch {}
} else {
fs.mkdirSync(artifactDirectory);
}
};
(async () => {
await ensureBuildPrerequisites();
/**
* @type {string[] | undefined}
*/
let toVerify = undefined;
if (process.env.EXTENSIONS) {
toVerify = process.env.EXTENSIONS === "," ? [] : 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 monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
for (const id in extensions) {
if (id === "$schema") {
continue;
}
if (toVerify && !toVerify.includes(id)) {
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 {
const extensionId = extension.msMarketplaceIdOverride ?? extension.id;
/** @type {[PromiseSettledResult<PublishedExtension | undefined>]} */
let [msExtension] = await Promise.allSettled([msGalleryApi.getExtension(extensionId, 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;
}
// Check if the extension is published by either Microsoft or GitHub
if (
["https://microsoft.com", "https://github.com"].includes(msExtension?.value?.publisher.domain) &&
msExtension?.value.publisher.isDomainVerified
) {
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 {
// Some extensions have versioning which is a bit different, like for example in the format of 1.71.8240911. If this is the case and we don't have this version published, we do some more checking to get more context about this version string.
const weirdVersionNumberPattern = new RegExp(/^\d{1,3}\.\d{1,}\.\d{4,}/g); // https://regexr.com/6t02m
if (context.msVersion.match(weirdVersionNumberPattern)) {
if (
`${semver.major(context.msVersion)}.${semver.minor(context.msVersion)}` ===
`${semver.major(context.ovsxVersion)}.${semver.minor(context.ovsxVersion)}`
) {
// If major.minor are the same on both marketplaces, we assume we're up-to-date
stat.upToDate[extension.id] = extStat;
debugger;
} else {
stat.outdated[extension.id] = extStat;
debugger;
}
} 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 newer 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 && !resolved?.resolution.releaseAsset) {
context.repo = resolved.path;
}
if (resolved?.resolution?.releaseAsset) {
console.log(`${extension.id}: resolved from release`);
context.files = resolved.files;
} else if (resolved?.resolution?.releaseTag) {
console.log(`${extension.id}: resolved ${resolved.resolution.releaseTag} from release tag`);
context.ref = resolved.resolution.releaseTag;
} else if (resolved?.resolution?.tag) {
console.log(`${extension.id}: resolved ${resolved.resolution.tag} from tags`);
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.ref = resolved.resolution.latest;
} else if (resolved?.resolution?.matchedLatest) {
console.log(
`${extension.id}: resolved ${resolved.resolution.matchedLatest} from the very latest commit`,
);
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.ref = resolved.resolution.matched;
} else {
throw `${extension.id}: failed to resolve`;
}
if (process.env.SKIP_BUILD === "true") {
continue;
}
let timeout;
const publishVersion = async (extension, context) => {
const env = {
...process.env,
...context.environmentVariables,
};
console.debug(`Publishing ${extension.id} for ${context.target || "universal"}...`);
await new Promise((resolve, reject) => {
const p = cp.spawn(
process.execPath,
["publish-extension.js", JSON.stringify({ extension, context, extensions })],
{
stdio: ["ignore", "inherit", "inherit"],
cwd: process.cwd(),
env,
},
);
p.on("error", reject);
p.on("exit", (code) => {
if (code) {
return reject(new Error("failed with exit status: " + code));
}
resolve("done");
});
timeout = setTimeout(
() => {
try {
p.kill("SIGKILL");
} catch {}
reject(new Error(`timeout after ${timeoutDelay} mins`));
},
timeoutDelay * 60 * 1000,
);
});
if (timeout !== undefined) {
clearTimeout(timeout);
}
};
if (context.files) {
// Publish all targets of extension from GitHub Release assets
for (const [target, file] of Object.entries(context.files)) {
if (!extension.target || Object.keys(extension.target).includes(target)) {
context.file = file;
context.target = target;
await publishVersion(extension, context);
} else {
console.log(`${extension.id}: skipping, since target ${target} is not included`);
}
}
} else if (extension.target) {
// Publish all specified targets of extension from sources
for (const [target, targetData] of Object.entries(extension.target)) {
context.target = target;
if (targetData !== true) {
context.environmentVariables = targetData.env;
}
await publishVersion(extension, context);
}
} else {
// Publish only the universal target of extension from sources
await publishVersion(extension, context);
}
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();
})();