vscode/build/gulpfile.hygiene.js

380 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-11-24 18:09:31 +00:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
2016-07-28 08:28:01 +00:00
'use strict';
2015-11-24 18:09:31 +00:00
2016-07-28 08:28:01 +00:00
const gulp = require('gulp');
const filter = require('gulp-filter');
const es = require('event-stream');
const gulptslint = require('gulp-tslint');
2017-06-20 14:21:44 +00:00
const gulpeslint = require('gulp-eslint');
2016-10-07 15:25:32 +00:00
const tsfmt = require('typescript-formatter');
2016-07-28 08:28:01 +00:00
const tslint = require('tslint');
2018-02-27 10:05:11 +00:00
const VinylFile = require('vinyl');
2017-10-10 15:53:33 +00:00
const vfs = require('vinyl-fs');
const path = require('path');
const fs = require('fs');
2018-02-27 10:05:11 +00:00
const pall = require('p-all');
2016-07-28 08:28:01 +00:00
2016-11-11 08:12:25 +00:00
/**
* 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:
2016-11-11 08:12:25 +00:00
*
* all eol indentation copyright typescript
*/
2016-07-28 08:28:01 +00:00
const all = [
'*',
2015-11-24 18:09:31 +00:00
'build/**/*',
'extensions/**/*',
'scripts/**/*',
'src/**/*',
2018-03-05 11:38:25 +00:00
'test/**/*',
'!**/node_modules/**'
];
2018-03-05 11:38:25 +00:00
const indentationFilter = [
'**',
2018-03-05 11:38:25 +00:00
// except specific files
'!ThirdPartyNotices.txt',
'!LICENSE.txt',
2018-03-05 11:38:25 +00:00
'!src/vs/nls.js',
'!src/vs/css.js',
'!src/vs/loader.js',
2018-03-15 15:16:04 +00:00
'!src/vs/base/common/marked/marked.js',
'!src/vs/base/common/winjs.base.js',
2018-03-05 11:38:25 +00:00
'!src/vs/base/node/terminateProcess.sh',
'!src/vs/base/node/cpuUsage.sh',
2018-03-05 11:38:25 +00:00
'!test/assert.js',
// except specific folders
2017-09-04 12:41:46 +00:00
'!test/smoke/out/**',
2018-03-05 11:38:25 +00:00
'!extensions/vscode-api-tests/testWorkspace/**',
'!extensions/vscode-api-tests/testWorkspace2/**',
'!build/monaco/**',
2017-05-12 14:38:39 +00:00
'!build/win32/**',
2015-11-24 18:09:31 +00:00
2018-03-05 11:38:25 +00:00
// except multiple specific files
2015-11-24 18:09:31 +00:00
'!**/package.json',
2018-03-05 11:38:25 +00:00
'!**/yarn.lock',
2018-04-12 17:38:24 +00:00
'!**/yarn-error.log',
2018-03-05 11:38:25 +00:00
// except multiple specific folders
2015-11-24 18:09:31 +00:00
'!**/octicons/**',
2018-03-05 11:38:25 +00:00
'!**/fixtures/**',
'!**/lib/**',
'!extensions/**/out/**',
2015-11-24 18:09:31 +00:00
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
2016-04-11 13:54:58 +00:00
'!extensions/**/themes/**',
'!extensions/**/colorize-fixtures/**',
2018-03-05 11:38:25 +00:00
// except specific file types
'!src/vs/*/**/*.d.ts',
'!src/typings/**/*.d.ts',
'!extensions/**/*.d.ts',
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot,md,ps1,template,yaml,yml,d.ts.recipe}',
'!build/{lib,tslintRules}/**/*.js',
'!build/**/*.sh',
'!build/tfs/**/*.js',
'!**/Dockerfile',
'!extensions/markdown-language-features/media/*.js'
2015-11-24 18:09:31 +00:00
];
2016-07-28 08:28:01 +00:00
const copyrightFilter = [
2015-11-24 18:09:31 +00:00
'**',
'!**/*.desktop',
2015-11-24 18:09:31 +00:00
'!**/*.json',
'!**/*.html',
'!**/*.template',
2015-11-24 18:09:31 +00:00
'!**/*.md',
'!**/*.bat',
'!**/*.cmd',
2016-06-12 15:54:03 +00:00
'!**/*.xml',
2015-11-24 18:09:31 +00:00
'!**/*.sh',
'!**/*.txt',
2016-03-24 21:01:51 +00:00
'!**/*.xpm',
'!**/*.opts',
'!**/*.disabled',
2018-01-26 15:16:36 +00:00
'!**/*.code-workspace',
2017-07-03 09:07:57 +00:00
'!build/**/*.init',
2017-10-04 16:53:27 +00:00
'!resources/linux/snap/snapcraft.yaml',
2017-12-20 09:31:43 +00:00
'!resources/linux/snap/electron-launch',
'!resources/win32/bin/code.js',
'!extensions/markdown-language-features/media/highlight.css',
'!extensions/html-language-features/server/src/modes/typescript/*',
'!extensions/*/server/bin/*'
2015-11-24 18:09:31 +00:00
];
2017-06-20 14:21:44 +00:00
const eslintFilter = [
'src/**/*.js',
'build/gulpfile.*.js',
2017-06-20 14:21:44 +00:00
'!src/vs/loader.js',
'!src/vs/css.js',
2017-06-20 14:21:44 +00:00
'!src/vs/nls.js',
'!src/vs/css.build.js',
'!src/vs/nls.build.js',
2018-03-15 15:16:04 +00:00
'!src/**/winjs.base.js',
'!src/**/marked.js',
2017-06-20 14:21:44 +00:00
'!**/test/**'
];
2016-07-28 08:28:01 +00:00
const tslintFilter = [
2016-01-18 08:50:41 +00:00
'src/**/*.ts',
2017-06-21 15:16:34 +00:00
'test/**/*.ts',
2016-01-18 08:50:41 +00:00
'extensions/**/*.ts',
'!**/fixtures/**',
2016-01-18 08:50:41 +00:00
'!**/typings/**',
2016-11-10 09:03:57 +00:00
'!**/node_modules/**',
2016-09-15 09:17:56 +00:00
'!extensions/typescript/test/colorize-fixtures/**',
'!extensions/vscode-api-tests/testWorkspace/**',
2018-01-26 15:16:36 +00:00
'!extensions/vscode-api-tests/testWorkspace2/**',
2017-11-07 10:53:59 +00:00
'!extensions/**/*.test.ts',
2018-03-17 15:32:55 +00:00
'!extensions/html-language-features/server/lib/jquery.d.ts'
2016-01-18 08:50:41 +00:00
];
2018-03-05 11:38:25 +00:00
const copyrightHeaderLines = [
2015-11-24 18:09:31 +00:00
'/*---------------------------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Licensed under the MIT License. See License.txt in the project root for license information.',
' *--------------------------------------------------------------------------------------------*/'
2018-03-05 11:38:25 +00:00
];
2015-11-24 18:09:31 +00:00
2017-06-20 14:21:44 +00:00
gulp.task('eslint', () => {
2017-10-31 15:11:10 +00:00
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
2017-06-20 14:21:44 +00:00
.pipe(filter(eslintFilter))
.pipe(gulpeslint('src/.eslintrc'))
.pipe(gulpeslint.formatEach('compact'))
.pipe(gulpeslint.failAfterError());
});
2016-02-18 15:02:16 +00:00
2016-07-28 08:28:01 +00:00
gulp.task('tslint', () => {
2017-11-13 10:12:56 +00:00
const options = { emitError: true };
2016-02-18 15:02:16 +00:00
2017-10-31 15:11:10 +00:00
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
2016-01-18 08:50:41 +00:00
.pipe(filter(tslintFilter))
2018-02-01 13:39:12 +00:00
.pipe(gulptslint.default({ rulesDirectory: 'build/lib/tslint' }))
.pipe(gulptslint.default.report(options));
2016-01-18 08:50:41 +00:00
});
2018-03-05 11:38:25 +00:00
function hygiene(some) {
2016-07-28 08:28:01 +00:00
let errorCount = 0;
2015-11-24 18:09:31 +00:00
2016-07-28 08:28:01 +00:00
const indentation = es.through(function (file) {
2018-03-05 11:38:25 +00:00
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
file.__lines = lines;
lines
2016-07-28 08:28:01 +00:00
.forEach((line, i) => {
2015-11-25 08:47:15 +00:00
if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK
2015-11-24 18:09:31 +00:00
} else if (/^[\t]*[^\s]/.test(line)) {
// good indent
} else if (/^[\t]* \*/.test(line)) {
// block comment using an extra space
} else {
console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
2015-11-24 18:09:31 +00:00
errorCount++;
}
});
this.emit('data', file);
});
2016-07-28 08:28:01 +00:00
const copyrights = es.through(function (file) {
2018-03-05 11:38:25 +00:00
const lines = file.__lines;
for (let i = 0; i < copyrightHeaderLines.length; i++) {
if (lines[i] !== copyrightHeaderLines[i]) {
console.error(file.relative + ': Missing or bad copyright statement');
errorCount++;
break;
}
2015-11-24 18:09:31 +00:00
}
2015-11-25 08:47:15 +00:00
2015-11-25 08:29:48 +00:00
this.emit('data', file);
2015-11-24 18:09:31 +00:00
});
2016-10-07 15:39:26 +00:00
const formatting = es.map(function (file, cb) {
2016-10-07 15:25:32 +00:00
tsfmt.processString(file.path, file.contents.toString('utf8'), {
2018-03-06 15:33:20 +00:00
verify: false,
2016-10-07 15:25:32 +00:00
tsfmt: true,
2018-04-22 18:12:03 +00:00
// verbose: true,
2018-02-01 13:39:12 +00:00
// keep checkJS happy
editorconfig: undefined,
replace: undefined,
tsconfig: undefined,
2018-04-22 18:12:03 +00:00
tsconfigFile: undefined,
tslint: undefined,
tslintFile: undefined,
tsfmtFile: undefined,
vscode: undefined,
vscodeFile: undefined
2016-10-07 15:25:32 +00:00
}).then(result => {
2018-03-06 21:24:47 +00:00
let original = result.src.replace(/\r\n/gm, '\n');
let formatted = result.dest.replace(/\r\n/gm, '\n');
2018-03-06 15:33:20 +00:00
if (original !== formatted) {
console.error('File not formatted:', file.relative);
2016-10-07 15:25:32 +00:00
errorCount++;
}
2016-10-07 15:39:26 +00:00
cb(null, file);
2016-10-07 15:25:32 +00:00
}, err => {
2016-10-07 15:39:26 +00:00
cb(err);
2016-10-07 15:25:32 +00:00
});
});
const tslintConfiguration = tslint.Configuration.findConfiguration('tslint.json', '.');
const tslintOptions = { fix: false, formatter: 'json' };
const tsLinter = new tslint.Linter(tslintOptions);
2016-10-07 15:25:32 +00:00
const tsl = es.through(function (file) {
2016-07-28 08:28:01 +00:00
const contents = file.contents.toString('utf8');
tsLinter.lint(file.relative, contents, tslintConfiguration.results);
2016-02-18 14:24:52 +00:00
this.emit('data', file);
});
2018-02-27 10:05:11 +00:00
let input;
if (Array.isArray(some) || typeof some === 'string' || !some) {
input = vfs.src(some || all, { base: '.', follow: true, allowEmpty: true });
} else {
input = some;
}
const result = input
2016-07-28 08:28:01 +00:00
.pipe(filter(f => !f.stat.isDirectory()))
2015-11-24 18:09:31 +00:00
.pipe(filter(indentationFilter))
.pipe(indentation)
2016-01-18 08:50:41 +00:00
.pipe(filter(copyrightFilter))
2017-06-20 14:21:44 +00:00
.pipe(copyrights);
const typescript = result
.pipe(filter(tslintFilter))
2016-10-07 15:25:32 +00:00
.pipe(formatting)
2017-06-20 14:21:44 +00:00
.pipe(tsl);
const javascript = result
.pipe(filter(eslintFilter))
.pipe(gulpeslint('src/.eslintrc'))
.pipe(gulpeslint.formatEach('compact'))
.pipe(gulpeslint.failAfterError());
let count = 0;
2017-06-20 14:21:44 +00:00
return es.merge(typescript, javascript)
.pipe(es.through(function (data) {
count++;
2017-11-13 10:32:38 +00:00
if (process.env['TRAVIS'] && count % 10 === 0) {
process.stdout.write('.');
}
this.emit('data', data);
}, function () {
process.stdout.write('\n');
const tslintResult = tsLinter.getResult();
if (tslintResult.failures.length > 0) {
for (const failure of tslintResult.failures) {
const name = failure.getFileName();
const position = failure.getStartPosition();
const line = position.getLineAndCharacter().line;
const character = position.getLineAndCharacter().character;
console.error(`${name}:${line + 1}:${character + 1}:${failure.getFailure()}`);
2018-02-12 11:26:07 +00:00
}
errorCount += tslintResult.failures.length;
}
2015-11-24 18:09:31 +00:00
if (errorCount > 0) {
2015-11-26 08:57:29 +00:00
this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
2015-11-24 18:09:31 +00:00
} else {
this.emit('end');
}
}));
2018-02-27 10:05:11 +00:00
}
2018-02-27 10:05:11 +00:00
function createGitIndexVinyls(paths) {
const cp = require('child_process');
const repositoryPath = process.cwd();
const fns = paths.map(relativePath => () => new Promise((c, e) => {
const fullPath = path.join(repositoryPath, relativePath);
fs.stat(fullPath, (err, stat) => {
2018-02-27 10:13:47 +00:00
if (err && err.code === 'ENOENT') { // ignore deletions
return c(null);
} else if (err) {
2018-02-27 10:05:11 +00:00
return e(err);
}
cp.exec(`git show :${relativePath}`, { maxBuffer: 2000 * 1024, encoding: 'buffer' }, (err, out) => {
if (err) {
return e(err);
}
c(new VinylFile({
path: fullPath,
base: repositoryPath,
contents: out,
stat
}));
});
});
}));
2018-02-27 10:13:47 +00:00
return pall(fns, { concurrency: 4 })
.then(r => r.filter(p => !!p));
2018-02-27 10:05:11 +00:00
}
2018-02-27 10:05:11 +00:00
gulp.task('hygiene', () => hygiene());
2016-07-28 08:28:01 +00:00
// this allows us to run hygiene as a git pre-commit hook
if (require.main === module) {
2016-07-28 08:28:01 +00:00
const cp = require('child_process');
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
process.exit(1);
});
2018-03-05 11:38:25 +00:00
if (process.argv.length > 2) {
hygiene(process.argv.slice(2)).on('error', err => {
console.error();
console.error(err);
process.exit(1);
});
} else {
2016-07-28 08:28:01 +00:00
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
2016-03-04 09:44:20 +00:00
if (err) {
console.error();
console.error(err);
process.exit(1);
2018-02-27 10:05:11 +00:00
return;
2016-03-04 09:44:20 +00:00
}
2016-07-28 08:28:01 +00:00
const some = out
2016-03-04 09:44:20 +00:00
.split(/\r?\n/)
2016-07-28 08:28:01 +00:00
.filter(l => !!l);
2017-11-06 14:39:46 +00:00
if (some.length > 0) {
2018-02-27 10:05:11 +00:00
console.log('Reading git index versions...');
createGitIndexVinyls(some)
2018-03-05 11:38:25 +00:00
.then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls))
2018-02-27 10:05:11 +00:00
.on('end', () => c())
.on('error', e)))
.catch(err => {
console.error();
console.error(err);
process.exit(1);
});
2017-11-06 14:39:46 +00:00
}
});
2018-03-05 11:38:25 +00:00
}
}