forked from microsoft/vscode-js-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
490 lines (422 loc) · 14.2 KB
/
gulpfile.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
const gulp = require('gulp');
const glob = require('glob');
const path = require('path');
const rename = require('gulp-rename');
const merge = require('merge2');
const vsce = require('vsce');
const execSync = require('child_process').execSync;
const fs = require('fs');
const cp = require('child_process');
const util = require('util');
const esbuild = require('esbuild');
const esbuildPlugins = require('./src/build/esbuildPlugins');
const got = require('got').default;
const { HttpsProxyAgent } = require('https-proxy-agent');
const jszip = require('jszip');
const stream = require('stream');
const pipelineAsync = util.promisify(stream.pipeline);
const dirname = 'js-debug';
const sources = ['src/**/*.{ts,tsx}'];
const externalModules = ['@vscode/dwarf-debugging'];
const allPackages = [];
const srcDir = 'src';
const buildDir = 'dist';
const buildSrcDir = `${buildDir}/src`;
const nodeTargetsDir = `targets/node`;
const isWatch = process.argv.includes('watch') || process.argv.includes('--watch');
const isDebug = process.argv.includes('--debug');
/**
* Whether we're running a nightly build.
*/
const isNightly = process.argv.includes('--nightly') || isWatch;
/**
* Extension ID to build. Appended with '-nightly' as necessary.
*/
const extensionName = isNightly ? 'js-debug-nightly' : 'js-debug';
async function runBuildScript(name) {
return new Promise((resolve, reject) =>
cp.execFile(
process.execPath,
[path.join(__dirname, buildDir, 'src', 'build', name)],
(err, stdout, stderr) => {
process.stderr.write(stderr);
if (err) {
return reject(err);
}
const outstr = stdout.toString('utf-8');
try {
resolve(JSON.parse(outstr));
} catch {
resolve(outstr);
}
},
)
);
}
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
async function readJson(file) {
const contents = await readFile(path.join(__dirname, file), 'utf-8');
return JSON.parse(contents);
}
const del = async patterns => {
const files = glob.sync(patterns, { cwd: __dirname });
await Promise.all(
files.map(f => fs.promises.rm(path.join(__dirname, f), { force: true, recursive: true })),
);
};
gulp.task('clean-assertions', () => del(['src/test/**/*.txt.actual']));
gulp.task('clean', () => del(['dist/**', 'src/*/package.nls.*.json', 'packages/**', '*.vsix']));
async function fixNightlyReadme() {
const readmePath = `${buildDir}/README.md`;
const readmeText = await readFile(readmePath);
const readmeNightlyText = await readFile(`README.nightly.md`);
await writeFile(readmePath, readmeNightlyText + '\n' + readmeText);
}
const getVersionNumber = () => {
if (process.env.JS_DEBUG_VERSION) {
return process.env.JS_DEBUG_VERSION;
}
const date = new Date(new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' }));
const monthMinutes = (date.getDate() - 1) * 24 * 60 + date.getHours() * 60 + date.getMinutes();
return [
// YY
date.getFullYear(),
// MM,
date.getMonth() + 1,
// DDHH
`${date.getDate()}${String(date.getHours()).padStart(2, '0')}`,
].join('.');
};
const cachedBuilds = new Map();
const incrementalEsbuild = async (/** @type {esbuild.BuildOptions} */ options) => {
const key = JSON.stringify(options);
if (cachedBuilds.has(key)) {
return cachedBuilds.get(key).rebuild();
}
if (!isWatch) {
const r = await esbuild.build(options);
if (r.metafile) {
console.log(await esbuild.analyzeMetafile(r.metafile));
}
return;
}
const ctx = await esbuild.context(options);
cachedBuilds.set(key, ctx);
await ctx.rebuild();
};
gulp.task('compile:build-scripts', async () =>
incrementalEsbuild({
entryPoints: fs
.readdirSync('src/build')
.filter(f => f.endsWith('.ts'))
.map(f => `src/build/${f}`),
outdir: `${buildDir}/src/build`,
define: await getConstantDefines(),
bundle: true,
platform: 'node',
}));
gulp.task('compile:dynamic', async () => {
const [contributions] = await Promise.all([
runBuildScript('generate-contributions'),
runBuildScript('documentReadme'),
]);
let packageJson = await readJson('package.json');
packageJson.name = extensionName;
if (isNightly) {
packageJson.displayName += ' (Nightly)';
packageJson.version = getVersionNumber();
packageJson.preview = true;
await fixNightlyReadme();
}
packageJson = Object.assign(packageJson, contributions);
await writeFile(`${buildDir}/package.json`, JSON.stringify(packageJson));
});
gulp.task('compile:static', () =>
merge(
gulp.src(
[
'LICENSE',
'resources/**/*',
'README.md',
'package.nls.json',
'src/**/*.sh',
'src/ui/basic-wat.tmLanguage.json',
'.vscodeignore',
],
{
base: '.',
},
),
gulp.src(['node_modules/@c4312/chromehash/pkg/*.wasm']).pipe(rename({ dirname: 'src' })),
).pipe(gulp.dest(buildDir)));
const resolveDefaultExts = ['.tsx', '.ts', '.jsx', '.js', '.css', '.json'];
async function getConstantDefines() {
const packageJson = await readJson('package.json');
return {
EXTENSION_NAME: JSON.stringify(extensionName),
EXTENSION_VERSION: JSON.stringify(isNightly ? getVersionNumber() : packageJson.version),
EXTENSION_PUBLISHER: JSON.stringify(packageJson.publisher),
};
}
function compileVendorLibrary(name) {
return {
name,
entryPoints: [require.resolve(name)],
outdir: `${buildSrcDir}/vendor`,
entryNames: `${name}`,
};
}
async function compileTs({
packages = [],
sourcemap = false,
compileInPlace = false,
minify = isWatch ? false : true,
watch = false,
} = options) {
const vendorPrefix = 'vendor';
// don't watch these, they won't really change:
const vendors = new Map(
await Promise.all(
[
{
...compileVendorLibrary('acorn-loose'),
plugins: [esbuildPlugins.hackyVendorBundle(new Map([['acorn', './acorn']]))],
},
compileVendorLibrary('acorn'),
].map(async ({ name, ...opts }) => {
await esbuild.build({
...opts,
sourcemap,
bundle: true,
platform: 'node',
format: 'cjs',
target: 'node20',
minify,
});
return [name, `./${vendorPrefix}/${name}.js`];
}),
),
);
// add the entrypoints common to both vscode and vs here
packages = [
...packages,
{ entry: `${srcDir}/common/hash/hash.ts`, library: false },
{ entry: `${srcDir}/common/sourceMaps/renameWorker.ts`, library: false },
{ entry: `${srcDir}/targets/node/bootloader.ts`, library: false, target: 'node10' },
{ entry: `${srcDir}/targets/node/watchdog.ts`, library: false, target: 'node10' },
{
entry: `${srcDir}/diagnosticTool/diagnosticTool.tsx`,
library: false,
target: 'chrome102',
platform: 'browser',
},
];
const define = await getConstantDefines();
let todo = [];
for (
const {
entry,
platform = 'node',
library,
isInVsCode,
nodePackages,
target = 'node20',
} of packages
) {
todo.push(
incrementalEsbuild({
entryPoints: [entry],
platform,
bundle: true,
outdir: buildSrcDir,
resolveExtensions: isInVsCode
? ['.extensionOnly.ts', ...resolveDefaultExts]
: resolveDefaultExts,
external: isInVsCode ? ['vscode', ...externalModules] : externalModules,
sourcemap: !!sourcemap,
sourcesContent: false,
packages: nodePackages,
minify,
define,
target,
alias: platform === 'node' ? {} : { path: 'path-browserify' },
plugins: [
esbuildPlugins.nativeNodeModulesPlugin(),
esbuildPlugins.importGlobLazy(),
esbuildPlugins.dirname(/src.test./),
esbuildPlugins.hackyVendorBundle(vendors),
],
format: library ? 'cjs' : 'iife',
}),
);
}
await Promise.all(todo);
await fs.promises.appendFile(
path.resolve(buildSrcDir, 'bootloader.js'),
'\n//# sourceURL=bootloader.bundle.cdp',
);
}
/** Run webpack to bundle the extension output files */
gulp.task('compile:extension', async () => {
const packages = [
{ entry: `${srcDir}/extension.ts`, library: true, isInVsCode: true },
{
entry: `${srcDir}/test/testRunner.ts`,
library: true,
isInVsCode: true,
nodePackages: 'external',
},
];
return compileTs({ packages, sourcemap: true });
});
gulp.task(
'compile',
gulp.series('compile:static', 'compile:build-scripts', 'compile:dynamic', 'compile:extension'),
);
/** Run webpack to bundle into the flat session launcher (for VS or standalone debug server) */
gulp.task('flatSessionBundle:webpack-bundle', async () => {
const packages = [{ entry: `${srcDir}/flatSessionLauncher.ts`, library: true }];
return compileTs({ packages, sourcemap: isWatch });
});
/** Run webpack to bundle into the standard DAP debug server */
gulp.task('dapDebugServer:webpack-bundle', async () => {
const packages = [{ entry: `${srcDir}/dapDebugServer.ts`, library: false }];
return compileTs({ packages, sourcemap: isWatch });
});
/** Run webpack to bundle into the VS debug server */
gulp.task('vsDebugServerBundle:webpack-bundle', async () => {
const packages = [{ entry: `${srcDir}/vsDebugServer.ts`, library: true }];
return compileTs({ packages, sourcemap: isDebug, minify: !isDebug });
});
const vsceUrls = {
baseContentUrl: 'https://github.com/microsoft/vscode-js-debug/blob/main',
baseImagesUrl: 'https://github.com/microsoft/vscode-js-debug/raw/main',
};
/** Create a VSIX package using the vsce command line tool */
gulp.task('package:createVSIX', () =>
vsce.createVSIX({
...vsceUrls,
cwd: buildDir,
dependencies: false,
packagePath: path.join(buildDir, `${extensionName}.vsix`),
}));
gulp.task('l10n:bundle-download', async () => {
const opts = {};
const proxy = process.env.https_proxy || process.env.HTTPS_PROXY || null;
if (proxy) {
opts.agent = {
https: new HttpsProxyAgent(proxy),
};
}
const res = await got('https://github.com/microsoft/vscode-loc/archive/main.zip', opts).buffer();
const content = await jszip.loadAsync(res);
for (const fileName of Object.keys(content.files)) {
const match = /vscode-language-pack-(.*?)\/.+ms-vscode\.js-debug.*?\.i18n\.json$/.exec(
fileName,
);
if (match) {
const locale = match[1];
const file = content.files[fileName];
const extractPath = path.join(buildDir, `nls.bundle.${locale}.json`);
await pipelineAsync(file.nodeStream(), fs.createWriteStream(extractPath));
}
}
});
/** Clean, compile, bundle, and create vsix for the extension */
gulp.task(
'package:prepare',
gulp.series(
'clean',
'compile:static',
'compile:build-scripts',
'compile:dynamic',
'compile:extension',
'package:createVSIX',
),
);
/** Prepares the package and then hoists it to the root directory. Destructive. */
gulp.task(
'package:hoist',
gulp.series('package:prepare', async () => {
const srcFiles = await fs.promises.readdir(buildDir);
const ignoredFiles = new Set(await fs.promises.readdir(__dirname));
ignoredFiles.delete('l10n-extract'); // special case: made in the pipeline
for (const file of srcFiles) {
ignoredFiles.delete(file);
await fs.promises.rm(path.join(__dirname, file), { force: true, recursive: true });
await fs.promises.rename(path.join(buildDir, file), path.join(__dirname, file));
}
await fs.promises.appendFile(
path.join(__dirname, '.vscodeignore'),
[...ignoredFiles].join('\n'),
);
}),
);
gulp.task('package', gulp.series('package:prepare', 'package:createVSIX'));
gulp.task('flatSessionBundle', gulp.series('clean', 'compile', 'flatSessionBundle:webpack-bundle'));
gulp.task(
'dapDebugServer',
gulp.series('clean', 'compile:static', 'dapDebugServer:webpack-bundle'),
);
gulp.task(
'vsDebugServerBundle',
gulp.series('clean', 'compile', 'vsDebugServerBundle:webpack-bundle', 'l10n:bundle-download'),
);
/** Publishes the build extension to the marketplace */
gulp.task('publish:vsce', () =>
vsce.publish({
...vsceUrls,
noVerify: true, // for proposed API usage
pat: process.env.MARKETPLACE_TOKEN,
dependencies: false,
cwd: buildDir,
}));
gulp.task('publish', gulp.series('package', 'publish:vsce'));
gulp.task('default', gulp.series('compile'));
gulp.task(
'watch',
gulp.series('clean', 'compile', done => {
gulp.watch([...sources, '*.json'], gulp.series('compile'));
done();
}),
);
const runFormatting = (onlyStaged, fix, callback) => {
const child = cp.fork('./node_modules/dprint/bin.js', [fix ? 'fmt' : 'check'], {
stdio: 'inherit',
});
child.on('exit', code => (code ? callback(`Formatter exited with code ${code}`) : callback()));
};
const runEslint = (fix, callback) => {
const child = cp.fork(
'./node_modules/eslint/bin/eslint.js',
['--color', 'src/**/*.ts', fix ? '--fix' : ['--max-warnings=0']],
{ stdio: 'inherit' },
);
child.on('exit', code => (code ? callback(`Eslint exited with code ${code}`) : callback()));
};
gulp.task('format:code', callback => runFormatting(false, true, callback));
gulp.task('format:eslint', callback => runEslint(true, callback));
gulp.task('format', gulp.series('format:code', 'format:eslint'));
gulp.task('lint:code', callback => runFormatting(false, false, callback));
gulp.task('lint:eslint', callback => runEslint(false, callback));
gulp.task('lint', gulp.parallel('lint:code', 'lint:eslint'));
/**
* Run a command in the terminal using exec, and wrap it in a promise
* @param {string} cmd The command line command + args to execute
* @param {ExecOptions} options, see here for options: https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_child_process_exec_command_options_callback
*/
function runCommand(cmd, options) {
return new Promise((resolve, reject) => {
let execError = undefined;
try {
execSync(cmd, { stdio: 'inherit', ...options });
} catch (err) {
reject(err);
}
resolve();
});
}