forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
534 lines (475 loc) · 18.8 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*---------------------------------------------------------------------------------------------
* 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 filter = require('gulp-filter');
const es = require('event-stream');
const tsfmt = require('typescript-formatter');
const tslint = require('tslint');
const relative = require('relative');
const ts = require('gulp-typescript');
const cp = require('child_process');
const spawn = require('cross-spawn');
const colors = require('colors/safe');
const gitmodified = require('gulp-gitmodified');
const path = require('path');
const debounce = require('debounce');
const jeditor = require("gulp-json-editor");
const del = require('del');
const sourcemaps = require('gulp-sourcemaps');
const fs = require('fs');
const remapIstanbul = require('remap-istanbul');
const istanbul = require('istanbul');
const glob = require('glob');
const os = require('os');
const _ = require('lodash');
const nativeDependencyChecker = require('node-has-native-dependencies');
const flat = require('flat');
const inlinesource = require('gulp-inline-source');
/**
* Hygiene works by creating cascading subsets of all our files and
* passing them through a sequence of checks. Here are the current subsets,
* named according to the checks performed on them. Each subset contains
* the following one, as described in mathematical notation:
*
* all ⊃ indentation ⊃ typescript
*/
const all = [
'src/**/*',
'src/client/**/*',
];
const tsFilter = [
'src/**/*.ts',
];
const indentationFilter = [
'src/**/*.ts',
'!**/typings/**/*',
];
const tslintFilter = [
'src/**/*.ts',
'test/**/*.ts',
'!**/node_modules/**',
'!out/**/*',
'!images/**/*',
'!.vscode/**/*',
'!pythonFiles/**/*',
'!resources/**/*',
'!snippets/**/*',
'!syntaxes/**/*',
'!**/typings/**/*'
];
const copyrightHeader = [
'// Copyright (c) Microsoft Corporation. All rights reserved.',
'// Licensed under the MIT License.',
'',
'\'use strict\';'
];
const copyrightHeaders = [copyrightHeader.join('\n'), copyrightHeader.join('\r\n')];
gulp.task('hygiene', () => run({ mode: 'all', skipFormatCheck: true, skipIndentationCheck: true }));
gulp.task('compile', () => run({ mode: 'compile', skipFormatCheck: true, skipIndentationCheck: true, skipLinter: true }));
gulp.task('watch', ['hygiene-modified', 'hygiene-watch']);
// Duplicate to allow duplicate task in tasks.json (one ith problem matching, and one without)
gulp.task('watchProblems', ['hygiene-modified', 'hygiene-watch']);
gulp.task('debugger-coverage', () => buildDebugAdapterCoverage());
gulp.task('hygiene-watch', () => gulp.watch(tsFilter, debounce(() => run({ mode: 'changes', skipFormatCheck: true, skipIndentationCheck: true, skipCopyrightCheck: true }), 100)));
gulp.task('hygiene-all', () => run({ mode: 'all' }));
gulp.task('hygiene-modified', ['compile'], () => run({ mode: 'changes' }));
gulp.task('clean', ['output:clean', 'cover:clean'], () => { });
gulp.task('output:clean', () => del(['coverage', 'debug_coverage*']));
gulp.task('cover:clean', () => del(['coverage', 'debug_coverage*']));
gulp.task('clean:ptvsd', () => del(['coverage', 'pythonFiles/experimental/ptvsd*']));
gulp.task('checkNativeDependencies', () => {
if (hasNativeDependencies()) {
throw new Error('Native dependencies deteced');
}
});
gulp.task('cover:enable', () => {
return gulp.src("./coverconfig.json")
.pipe(jeditor((json) => {
json.enabled = true;
return json;
}))
.pipe(gulp.dest("./out", { 'overwrite': true }));
});
gulp.task('cover:disable', () => {
return gulp.src("./coverconfig.json")
.pipe(jeditor((json) => {
json.enabled = false;
return json;
}))
.pipe(gulp.dest("./out", { 'overwrite': true }));
});
/**
* Inline CSS into the coverage report for better visualizations on
* the VSTS report page for code coverage.
*/
gulp.task('inlinesource', () => {
return gulp.src('./coverage/lcov-report/*.html')
.pipe(inlinesource({attribute: false}))
.pipe(gulp.dest('./coverage/lcov-report-inline'));
});
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 => jsonProperties.findIndex(flattenedDependency => flattenedDependency.endsWith(`dependencies.${item}.version`)) >= 0);
if (nativeDependencies.length > 0) {
console.error('Native dependencies detected', nativeDependencies);
return true;
}
return false;
}
function buildDebugAdapterCoverage() {
const matches = glob.sync(path.join(__dirname, 'debug_coverage*/coverage.json'));
matches.forEach(coverageFile => {
const finalCoverageFile = path.join(path.dirname(coverageFile), 'coverage-final-upload.json');
const remappedCollector = remapIstanbul.remap(JSON.parse(fs.readFileSync(coverageFile, 'utf8')), {
warn: warning => {
// We expect some warnings as any JS file without a typescript mapping will cause this.
// By default, we'll skip printing these to the console as it clutters it up.
console.warn(warning);
}
});
const reporter = new istanbul.Reporter(undefined, path.dirname(coverageFile));
reporter.add('lcov');
reporter.write(remappedCollector, true, () => { });
});
}
/**
* @typedef {Object} hygieneOptions - creates a new type named 'SpecialType'
* @property {'changes'|'staged'|'all'|'compile'} [mode=] - Mode.
* @property {boolean=} skipIndentationCheck - Skip indentation checks.
* @property {boolean=} skipFormatCheck - Skip format checks.
* @property {boolean=} skipCopyrightCheck - Skip copyright checks.
* @property {boolean=} skipLinter - Skip linter.
*/
const tsProjectMap = {};
/**
*
* @param {hygieneOptions} options
*/
function getTsProject(options) {
const tsOptions = options.mode === 'compile' ? undefined : { strict: true, noImplicitAny: false, noImplicitThis: false };
const mode = tsOptions && tsOptions.mode ? tsOptions.mode : '';
return tsProjectMap[mode] ? tsProjectMap[mode] : tsProjectMap[mode] = ts.createProject('tsconfig.json', tsOptions);
}
let configuration;
/**
*
* @param {hygieneOptions} options
*/
function getLinter(options) {
configuration = configuration ? configuration : tslint.Configuration.findConfiguration(null, '.');
const program = tslint.Linter.createProgram('./tsconfig.json');
const linter = new tslint.Linter({ formatter: 'json' }, program);
return { linter, configuration };
}
let compilationInProgress = false;
let reRunCompilation = false;
/**
*
* @param {hygieneOptions} options
* @returns {NodeJS.ReadWriteStream}
*/
const hygiene = (options) => {
if (compilationInProgress) {
reRunCompilation = true;
return;
}
const fileListToProcess = options.mode === 'compile' ? undefined : getFileListToProcess(options);
if (Array.isArray(fileListToProcess) && fileListToProcess !== all
&& fileListToProcess.filter(item => item.endsWith('.ts')).length === 0) {
return;
}
const started = new Date().getTime();
compilationInProgress = true;
options = options || {};
let errorCount = 0;
const addedFiles = options.skipCopyrightCheck ? [] : getAddedFilesSync();
console.log(colors.blue('Hygiene started.'));
const copyrights = es.through(function (file) {
if (addedFiles.indexOf(file.path) !== -1) {
const contents = file.contents.toString('utf8');
if (!copyrightHeaders.some(header => contents.indexOf(header) === 0)) {
// Use tslint format.
console.error(`ERROR: (copyright) ${file.relative}[1,1]: Missing or bad copyright statement`);
errorCount++;
}
}
this.emit('data', file);
});
const indentation = es.through(function (file) {
file.contents
.toString('utf8')
.split(/\r\n|\r|\n/)
.forEach((line, i) => {
if (/^\s*$/.test(line) || /^\S+.*$/.test(line)) {
// Empty or whitespace lines are OK.
} else if (/^(\s\s\s\s)+.*/.test(line)) {
// Good indent.
} else if (/^[\t]+.*/.test(line)) {
console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation (use 4 spaces instead of tabs or other)');
errorCount++;
}
});
this.emit('data', file);
});
const formatOptions = { verify: true, tsconfig: true, tslint: true, editorconfig: true, tsfmt: true };
const formatting = es.map(function (file, cb) {
tsfmt.processString(file.path, file.contents.toString('utf8'), formatOptions)
.then(result => {
if (result.error) {
let message = result.message.trim();
let formattedMessage = '';
if (message.startsWith(__dirname)) {
message = message.substr(__dirname.length);
message = message.startsWith(path.sep) ? message.substr(1) : message;
const index = message.indexOf('.ts ');
if (index === -1) {
formattedMessage = colors.red(message);
} else {
const file = message.substr(0, index + 3);
const errorMessage = message.substr(index + 4).trim();
formattedMessage = `${colors.red(file)} ${errorMessage}`;
}
} else {
formattedMessage = colors.red(message);
}
console.error(formattedMessage);
errorCount++;
}
cb(null, file);
})
.catch(cb);
});
let reportedLinterFailures = [];
/**
* Report the linter failures
* @param {any[]} failures
*/
function reportLinterFailures(failures) {
return failures
.map(failure => {
const name = failure.name || failure.fileName;
const position = failure.startPosition;
const line = position.lineAndCharacter ? position.lineAndCharacter.line : position.line;
const character = position.lineAndCharacter ? position.lineAndCharacter.character : position.character;
// Output in format similar to tslint for the linter to pickup.
const message = `ERROR: (${failure.ruleName}) ${relative(__dirname, name)}[${line + 1}, ${character + 1}]: ${failure.failure}`;
if (reportedLinterFailures.indexOf(message) === -1) {
console.error(message);
reportedLinterFailures.push(message);
return true;
} else {
return false;
}
})
.filter(reported => reported === true)
.length > 0;
}
const { linter, configuration } = getLinter(options);
const tsl = es.through(function (file) {
const contents = file.contents.toString('utf8');
// Don't print anything to the console, we'll do that.
// Yes this is a hack, but tslinter doesn't provide an option to prevent this.
const oldWarn = console.warn;
console.warn = () => { };
linter.failures = [];
linter.fixes = [];
linter.lint(file.relative, contents, configuration.results);
console.warn = oldWarn;
const result = linter.getResult();
if (result.failureCount > 0 || result.errorCount > 0) {
const reported = reportLinterFailures(result.failures);
if (result.failureCount && reported) {
errorCount += result.failureCount;
}
if (result.errorCount && reported) {
errorCount += result.errorCount;
}
}
this.emit('data', file);
});
const tsFiles = [];
const tscFilesTracker = es.through(function (file) {
tsFiles.push(file.path.replace(/\\/g, '/'));
tsFiles.push(file.path);
this.emit('data', file);
});
const tsProject = getTsProject(options);
const tsc = function () {
function customReporter() {
return {
error: function (error) {
const fullFilename = error.fullFilename || '';
const relativeFilename = error.relativeFilename || '';
if (tsFiles.findIndex(file => fullFilename === file || relativeFilename === file) === -1) {
return;
}
errorCount += 1;
console.error(error.message);
},
finish: function () {
// forget the summary.
}
};
}
const reporter = customReporter();
return tsProject(reporter);
}
const files = options.mode === 'compile' ? tsProject.src() : getFilesToProcess(fileListToProcess);
const dest = options.mode === 'compile' ? './out' : '.';
let result = files
.pipe(filter(f => f && f.stat && !f.stat.isDirectory()));
if (!options.skipIndentationCheck) {
result = result.pipe(filter(indentationFilter))
.pipe(indentation);
}
result = result
.pipe(filter(tslintFilter));
if (!options.skipCopyrightCheck) {
result = result.pipe(copyrights);
}
if (!options.skipFormatCheck) {
// result = result
// .pipe(formatting);
}
if (!options.skipLinter) {
result = result
.pipe(tsl);
}
let totalTime = 0;
result = result
.pipe(tscFilesTracker)
.pipe(sourcemaps.init())
.pipe(tsc())
.pipe(sourcemaps.mapSources(function (sourcePath, file) {
const tsFileName = path.basename(file.path).replace(/js$/, 'ts');
const qualifiedSourcePath = path.dirname(file.path).replace('out/', 'src/').replace('out\\', 'src\\');
if (!fs.existsSync(path.join(qualifiedSourcePath, tsFileName))) {
console.error(`ERROR: (source-maps) ${file.path}[1,1]: Source file not found`);
}
return path.join(path.relative(path.dirname(file.path), qualifiedSourcePath), tsFileName);
}))
.pipe(sourcemaps.write('.', { includeContent: false }))
.pipe(gulp.dest(dest))
.pipe(es.through(null, function () {
if (errorCount > 0) {
const errorMessage = `Hygiene failed with errors 👎 . Check 'gulpfile.js' (completed in ${new Date().getTime() - started}ms).`;
console.error(colors.red(errorMessage));
exitHandler(options);
} else {
console.log(colors.green(`Hygiene passed with 0 errors 👍 (completed in ${new Date().getTime() - started}ms).`));
}
// Reset error counter.
errorCount = 0;
reportedLinterFailures = [];
compilationInProgress = false;
if (reRunCompilation) {
reRunCompilation = false;
setTimeout(() => {
hygiene(options);
}, 10);
}
this.emit('end');
}))
.on('error', exitHandler.bind(this, options));
return result;
};
/**
* @typedef {Object} runOptions
* @property {boolean=} exitOnError - Exit on error.
* @property {'changes'|'staged'|'all'} [mode=] - Mode.
* @property {string[]=} files - Optional list of files to be modified.
* @property {boolean=} skipIndentationCheck - Skip indentation checks.
* @property {boolean=} skipFormatCheck - Skip format checks.
* @property {boolean=} skipCopyrightCheck - Skip copyright checks.
* @property {boolean=} skipLinter - Skip linter.
* @property {boolean=} watch - Watch mode.
*/
/**
* Run the linters.
* @param {runOptions} options
* @param {Error} ex
*/
function exitHandler(options, ex) {
console.error();
if (ex) {
console.error(ex);
console.error(colors.red(ex));
}
if (options.exitOnError) {
console.log('exit');
process.exit(1);
}
}
/**
* Run the linters.
* @param {runOptions} options
*/
function run(options) {
options = options ? options : {};
process.once('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
exitHandler(options);
});
return hygiene(options);
}
function getStagedFilesSync() {
const out = cp.execSync('git diff --cached --name-only', { encoding: 'utf8' });
return out
.split(/\r?\n/)
.filter(l => !!l);
}
function getAddedFilesSync() {
const out = cp.execSync('git status -u -s', { encoding: 'utf8' });
return out
.split(/\r?\n/)
.filter(l => !!l)
.filter(l => _.intersection(['A', '?', 'U'], l.substring(0, 2).trim().split('')).length > 0)
.map(l => path.join(__dirname, l.substring(2).trim()));
}
function getModifiedFilesSync() {
const out = cp.execSync('git status -u -s', { encoding: 'utf8' });
return out
.split(/\r?\n/)
.filter(l => !!l)
.filter(l => _.intersection(['M', 'A', 'R', 'C', 'U', '?'], l.substring(0, 2).trim().split('')).length > 0)
.map(l => path.join(__dirname, l.substring(2).trim()));
}
/**
* @param {hygieneOptions} options
*/
function getFilesToProcess(fileList) {
const gulpSrcOptions = { base: '.' };
return gulp.src(fileList, gulpSrcOptions);
}
/**
* @param {hygieneOptions} options
*/
function getFileListToProcess(options) {
const mode = options ? options.mode : 'all';
const gulpSrcOptions = { base: '.' };
// If we need only modified files, then filter the glob.
if (options && options.mode === 'changes') {
return getModifiedFilesSync();
}
if (options && options.mode === 'staged') {
return getStagedFilesSync();
}
return all;
}
exports.hygiene = hygiene;
// this allows us to run hygiene as a git pre-commit hook.
if (require.main === module) {
run({ exitOnError: true, mode: 'staged' });
}