forked from microsoft/vscode-jupyter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
358 lines (328 loc) · 15.6 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/* jshint node: true */
/* jshint esversion: 6 */
'use strict';
const gulp = require('gulp');
const glob = require('glob');
const spawn = require('cross-spawn');
const path = require('path');
const del = require('del');
const fs = require('fs-extra');
const _ = require('lodash');
const nativeDependencyChecker = require('node-has-native-dependencies');
const flat = require('flat');
const os = require('os');
const { spawnSync } = require('child_process');
const isCI = process.env.TF_BUILD !== undefined || process.env.GITHUB_ACTIONS === 'true';
const webpackEnv = { NODE_OPTIONS: '--max_old_space_size=9096' };
gulp.task('compile', async (done) => {
// Use tsc so we can generate source maps that look just like tsc does (gulp-sourcemap does not generate them the same way)
try {
const stdout = await spawnAsync('tsc', ['-p', './'], {}, true);
if (stdout.toLowerCase().includes('error ts')) {
throw new Error(`Compile errors: \n${stdout}`);
}
done();
} catch (e) {
done(e);
}
});
gulp.task('createNycFolder', async (done) => {
try {
const fs = require('fs');
fs.mkdirSync(path.join(__dirname, '.nyc_output'));
} catch (e) {
//
}
done();
});
gulp.task('validateTranslationFiles', (done) => {
glob.sync('package.nls.*.json', { sync: true }).forEach((file) => {
// Verify we can open and parse as JSON.
try {
JSON.parse(fs.readFileSync(file));
} catch (ex) {
throw new Error(`Error parsing Translation File ${file}`);
}
});
done();
});
gulp.task('output:clean', () => del(['coverage']));
gulp.task('clean:cleanExceptTests', () => del(['clean:vsix', 'out/client', 'out/datascience-ui', 'out/server']));
gulp.task('clean:vsix', () => del(['*.vsix']));
gulp.task('clean:out', () => del(['out/**', '!out', '!out/client_renderer/**']));
gulp.task('clean:ipywidgets', () => spawnAsync('npm', ['run', 'build-ipywidgets-clean'], webpackEnv));
gulp.task('clean', gulp.parallel('output:clean', 'clean:vsix', 'clean:out'));
gulp.task('checkNativeDependencies', (done) => {
if (hasNativeDependencies()) {
done(new Error('Native dependencies detected'));
}
done();
});
gulp.task('checkNpmDependencies', (done) => {
/**
* Sometimes we have to update the package-lock.json file to upload dependencies.
* Thisscript will ensure that even if the package-lock.json is re-generated the (minimum) version numbers are still as expected.
*/
const packageLock = require('./package-lock.json');
const errors = [];
const expectedVersions = [
{ name: 'trim', version: '0.0.3' },
{ name: 'node_modules/trim', version: '0.0.3' }
];
function checkPackageVersions(packages, parent) {
if (!packages) {
return;
}
expectedVersions.forEach((expectedVersion) => {
if (!packages[expectedVersion.name]) {
return;
}
const version = packages[expectedVersion.name].version || packages[expectedVersion.name];
if (!version) {
return;
}
if (!version.includes(expectedVersion.version)) {
errors.push(
`${expectedVersion.name} version needs to be at least ${
expectedVersion.version
}, current ${version}, ${parent ? `(parent package ${parent})` : ''}`
);
}
});
}
function checkPackageDependencies(packages) {
if (!packages) {
return;
}
Object.keys(packages).forEach((packageName) => {
const dependencies = packages[packageName]['dependencies'];
if (dependencies) {
checkPackageVersions(dependencies, packageName);
}
});
}
checkPackageVersions(packageLock['packages']);
checkPackageVersions(packageLock['dependencies']);
checkPackageDependencies(packageLock['packages']);
if (errors.length > 0) {
errors.forEach((ex) => console.error(ex));
throw new Error(errors.join(', '));
}
done();
});
gulp.task('installPythonLibs', async () => {
const output = spawnSync(
'python -m pip --disable-pip-version-check install -t ./pythonFiles/lib/python --no-cache-dir --implementation py --no-deps --upgrade -r ./requirements.txt'
);
if (output.error) {
console.error(output.stderr);
throw output.error;
}
});
gulp.task('compile-ipywidgets', () => buildIPyWidgets());
async function buildIPyWidgets() {
// if the output ipywidgest file exists, then no need to re-build.
// Barely changes. If making changes, then re-build manually.
if (!isCI && fs.existsSync(path.join(__dirname, 'out/ipywidgets/dist/ipywidgets.js'))) {
return;
}
await spawnAsync('npm', ['run', 'build-ipywidgets'], webpackEnv);
}
gulp.task('compile-renderers', async () => {
console.log('Building renderers');
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-renderers.config.js');
});
gulp.task('compile-viewers', async () => {
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-viewers.config.js');
});
gulp.task('compile-webviews', gulp.series('compile-ipywidgets', gulp.parallel('compile-viewers', 'compile-renderers')));
async function buildWebPackForDevOrProduction(configFile, configNameForProductionBuilds) {
if (configNameForProductionBuilds) {
await buildWebPack(configNameForProductionBuilds, ['--config', configFile], webpackEnv);
} else {
console.log('Building ipywidgets in dev mode');
await spawnAsync('npm', ['run', 'webpack', '--', '--config', configFile, '--mode', 'development'], webpackEnv);
}
}
gulp.task('webpack', async () => {
// Build node_modules.
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.dependencies.config.js', 'production');
// Build DS stuff (separately as it uses far too much memory and slows down CI).
// Individually is faster on CI.
await buildIPyWidgets();
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-renderers.config.js', 'production');
await buildWebPackForDevOrProduction('./build/webpack/webpack.datascience-ui-viewers.config.js', 'production');
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.config.js', 'extension');
});
gulp.task('updateBuildNumber', async () => {
await updateBuildNumber();
});
async function updateBuildNumber() {
// Edit the version number from the package.json
const packageJsonContents = await fs.readFile('package.json', 'utf-8');
const packageJson = JSON.parse(packageJsonContents);
// Change version number
// 3rd part of version is limited to Max Int32 numbers (in VSC Marketplace).
// Hence build numbers can only be YYYY.MMM.2147483647
// NOTE: For each of the following strip the first 3 characters from the build number.
// E.g. if we have build number of `build number = 3264527301, then take 4527301
// To ensure we can have point releases & insider builds, we're going with the following format:
// Insider & Release builds will be YYYY.MMM.100<build number>
// When we have a hot fix, we update the version to YYYY.MMM.110<build number>
// If we have more hot fixes, they'll be YYYY.MMM.120<build number>, YYYY.MMM.130<build number>, & so on.
const versionParts = packageJson.version.split('.');
// New build is of the form `DDDHHMM` (day of year, hours, minute) (7 digits, as out of the 10 digits first three are reserved for `100` or `101` for patches).
// Use date time for build, this way all subsequent builds are incremental and greater than the others before.
// Example build for 3Jan 12:45 will be `0031245`, and 16 Feb 8:50 will be `0470845`
const today = new Date();
const dayCount = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335];
const dayOfYear = dayCount[today.getMonth()] + today.getDate() + 1;
const buildNumberSuffix = `${dayOfYear.toString().padStart(3, '0')}${(today.getHours() + 1)
.toString()
.padStart(2, '0')}${(today.getMinutes() + 1).toString().padStart(2, '0')}`;
const buildNumber = `${versionParts[2].substring(0, 3)}${buildNumberSuffix}`;
const newVersion =
versionParts.length > 1 ? `${versionParts[0]}.${versionParts[1]}.${buildNumber}` : packageJson.version;
packageJson.version = newVersion;
// Write back to the package json
await fs.writeFile('package.json', JSON.stringify(packageJson, null, 4), 'utf-8');
}
async function buildWebPack(webpackConfigName, args, env) {
// Remember to perform a case insensitive search.
const allowedWarnings = getAllowedWarningsForWebPack(webpackConfigName).map((item) => item.toLowerCase());
const stdOut = await spawnAsync(
'npm',
['run', 'webpack', '--', ...args, ...['--mode', 'production', '--devtool', 'source-map']],
env
);
const stdOutLines = stdOut
.split(os.EOL)
.map((item) => item.trim())
.filter((item) => item.length > 0);
// Remember to perform a case insensitive search.
const warnings = stdOutLines
.filter((item) => item.startsWith('WARNING in '))
.filter(
(item) =>
allowedWarnings.findIndex((allowedWarning) =>
item.toLowerCase().startsWith(allowedWarning.toLowerCase())
) == -1
);
const errors = stdOutLines.some((item) => item.startsWith('ERROR in'));
if (errors) {
throw new Error(`Errors in ${webpackConfigName}, \n${warnings.join(', ')}\n\n${stdOut}`);
}
if (warnings.length > 0) {
throw new Error(
`Warnings in ${webpackConfigName}, Check gulpfile.js to see if the warning should be allowed., \n\n${stdOut}`
);
}
}
function getAllowedWarningsForWebPack(buildConfig) {
switch (buildConfig) {
case 'production':
return [
'WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).',
'WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.',
'WARNING in webpack performance recommendations:',
'WARNING in ./node_modules/encoding/lib/iconv-loader.js',
'WARNING in ./node_modules/keyv/src/index.js',
'ERROR in ./node_modules/got/index.js',
'WARNING in ./node_modules/ws/lib/BufferUtil.js',
'WARNING in ./node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/any-promise/register.js',
'WARNING in ./node_modules/log4js/lib/appenders/index.js',
'WARNING in ./node_modules/log4js/lib/clustering.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/out/AutoCollection/NativePerformance.js'
];
case 'extension':
return [
'WARNING in ./node_modules/cacheable-request/node_modules/keyv/src/index.js',
'WARNING in ./node_modules/encoding/lib/iconv-loader.js',
'WARNING in ./node_modules/keyv/src/index.js',
'WARNING in ./node_modules/ws/lib/BufferUtil.js',
'WARNING in ./node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/any-promise/register.js',
'remove-files-plugin@1.4.0:',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/buffer-util.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/validation.js',
'WARNING in ./node_modules/@jupyterlab/services/node_modules/ws/lib/Validation.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/out/AutoCollection/NativePerformance.js'
];
case 'debugAdapter':
return [
'WARNING in ./node_modules/vscode-uri/lib/index.js',
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
'WARNING in ./node_modules/applicationinsights/out/AutoCollection/NativePerformance.js'
];
default:
throw new Error('Unknown WebPack Configuration');
}
}
gulp.task('prePublishBundle', gulp.series('webpack'));
gulp.task('checkDependencies', gulp.series('checkNativeDependencies', 'checkNpmDependencies'));
gulp.task('prePublishNonBundle', gulp.parallel('compile', gulp.series('compile-webviews')));
function spawnAsync(command, args, env, rejectOnStdErr = false) {
env = env || {};
env = { ...process.env, ...env };
return new Promise((resolve, reject) => {
let stdOut = '';
console.info(`> ${command} ${args.join(' ')}`);
const proc = spawn(command, args, { cwd: __dirname, env });
proc.stdout.on('data', (data) => {
// Log output on CI (else travis times out when there's not output).
stdOut += data.toString();
if (isCI) {
console.log(data.toString());
}
});
proc.stderr.on('data', (data) => {
console.error(data.toString());
if (rejectOnStdErr) {
reject(data.toString());
}
});
proc.on('close', () => resolve(stdOut));
proc.on('error', (error) => reject(error));
});
}
function hasNativeDependencies() {
let nativeDependencies = nativeDependencyChecker.check(path.join(__dirname, 'node_modules'));
if (!Array.isArray(nativeDependencies) || nativeDependencies.length === 0) {
return false;
}
const dependencies = JSON.parse(spawn.sync('npm', ['ls', '--json', '--prod']).stdout.toString());
const jsonProperties = Object.keys(flat.flatten(dependencies));
nativeDependencies = _.flatMap(nativeDependencies, (item) =>
path.dirname(item.substring(item.indexOf('node_modules') + 'node_modules'.length)).split(path.sep)
)
.filter((item) => item.length > 0)
.filter((item) => !item.includes('zeromq') && !item.includes('canvas') && !item.includes('keytar')) // Known native modules
.filter(
(item) =>
jsonProperties.findIndex((flattenedDependency) =>
flattenedDependency.endsWith(`dependencies.${item}.version`)
) >= 0
);
if (nativeDependencies.length > 0) {
console.error('Native dependencies detected', nativeDependencies);
return true;
}
return false;
}
gulp.task('generateTelemetryMd', async () => {
const generator = require('./out/tools/telemetryGenerator');
return generator.default();
});