vscode/build/gulpfile.hygiene.js

379 lines
9.8 KiB
JavaScript
Raw Normal View History

2015-11-24 19:09:31 +01: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 10:28:01 +02:00
'use strict';
2015-11-24 19:09:31 +01:00
2016-07-28 10:28:01 +02:00
const gulp = require('gulp');
const filter = require('gulp-filter');
const es = require('event-stream');
const gulptslint = require('gulp-tslint');
2017-06-20 16:21:44 +02:00
const gulpeslint = require('gulp-eslint');
2016-10-07 17:25:32 +02:00
const tsfmt = require('typescript-formatter');
2016-07-28 10:28:01 +02:00
const tslint = require('tslint');
2018-02-27 11:05:11 +01:00
const VinylFile = require('vinyl');
2017-10-10 17:53:33 +02:00
const vfs = require('vinyl-fs');
const path = require('path');
const fs = require('fs');
2018-02-27 11:05:11 +01:00
const pall = require('p-all');
2016-07-28 10:28:01 +02:00
2016-11-11 09:12:25 +01: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 09:12:25 +01:00
*
* all eol indentation copyright typescript
*/
2016-07-28 10:28:01 +02:00
const all = [
'*',
2015-11-24 19:09:31 +01:00
'build/**/*',
'extensions/**/*',
'scripts/**/*',
'src/**/*',
2018-03-05 12:38:25 +01:00
'test/**/*',
'!**/node_modules/**'
];
2018-03-05 12:38:25 +01:00
const indentationFilter = [
'**',
2018-03-05 12:38:25 +01:00
// except specific files
'!ThirdPartyNotices.txt',
'!LICENSE.txt',
2018-03-05 12:38:25 +01:00
'!src/vs/nls.js',
'!src/vs/css.js',
'!src/vs/loader.js',
2018-03-15 16:16:04 +01:00
'!src/vs/base/common/marked/marked.js',
'!src/vs/base/common/winjs.base.js',
2018-03-05 12:38:25 +01:00
'!src/vs/base/node/terminateProcess.sh',
'!src/vs/base/node/cpuUsage.sh',
2018-03-05 12:38:25 +01:00
'!test/assert.js',
// except specific folders
2017-09-04 14:41:46 +02:00
'!test/smoke/out/**',
2018-03-05 12:38:25 +01:00
'!extensions/vscode-api-tests/testWorkspace/**',
'!extensions/vscode-api-tests/testWorkspace2/**',
'!build/monaco/**',
2017-05-12 16:38:39 +02:00
'!build/win32/**',
2015-11-24 19:09:31 +01:00
2018-03-05 12:38:25 +01:00
// except multiple specific files
2015-11-24 19:09:31 +01:00
'!**/package.json',
2018-03-05 12:38:25 +01:00
'!**/yarn.lock',
2018-04-12 19:38:24 +02:00
'!**/yarn-error.log',
2018-03-05 12:38:25 +01:00
// except multiple specific folders
2015-11-24 19:09:31 +01:00
'!**/octicons/**',
2018-03-05 12:38:25 +01:00
'!**/fixtures/**',
'!**/lib/**',
'!extensions/**/out/**',
2015-11-24 19:09:31 +01:00
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
2016-04-11 15:54:58 +02:00
'!extensions/**/themes/**',
'!extensions/**/colorize-fixtures/**',
2018-03-05 12:38:25 +01: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 19:09:31 +01:00
];
2016-07-28 10:28:01 +02:00
const copyrightFilter = [
2015-11-24 19:09:31 +01:00
'**',
'!**/*.desktop',
2015-11-24 19:09:31 +01:00
'!**/*.json',
'!**/*.html',
'!**/*.template',
2015-11-24 19:09:31 +01:00
'!**/*.md',
'!**/*.bat',
'!**/*.cmd',
2016-06-12 17:54:03 +02:00
'!**/*.xml',
2015-11-24 19:09:31 +01:00
'!**/*.sh',
'!**/*.txt',
2016-03-24 22:01:51 +01:00
'!**/*.xpm',
'!**/*.opts',
'!**/*.disabled',
2018-01-26 16:16:36 +01:00
'!**/*.code-workspace',
2017-07-03 11:07:57 +02:00
'!build/**/*.init',
2017-10-04 18:53:27 +02:00
'!resources/linux/snap/snapcraft.yaml',
'!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 19:09:31 +01:00
];
2017-06-20 16:21:44 +02:00
const eslintFilter = [
'src/**/*.js',
'build/gulpfile.*.js',
2017-06-20 16:21:44 +02:00
'!src/vs/loader.js',
'!src/vs/css.js',
2017-06-20 16:21:44 +02:00
'!src/vs/nls.js',
'!src/vs/css.build.js',
'!src/vs/nls.build.js',
2018-03-15 16:16:04 +01:00
'!src/**/winjs.base.js',
'!src/**/marked.js',
2017-06-20 16:21:44 +02:00
'!**/test/**'
];
2016-07-28 10:28:01 +02:00
const tslintFilter = [
2016-01-18 09:50:41 +01:00
'src/**/*.ts',
2017-06-21 17:16:34 +02:00
'test/**/*.ts',
2016-01-18 09:50:41 +01:00
'extensions/**/*.ts',
'!**/fixtures/**',
2016-01-18 09:50:41 +01:00
'!**/typings/**',
2016-11-10 10:03:57 +01:00
'!**/node_modules/**',
2016-09-15 11:17:56 +02:00
'!extensions/typescript/test/colorize-fixtures/**',
'!extensions/vscode-api-tests/testWorkspace/**',
2018-01-26 16:16:36 +01:00
'!extensions/vscode-api-tests/testWorkspace2/**',
2017-11-07 11:53:59 +01:00
'!extensions/**/*.test.ts',
2018-03-17 16:32:55 +01:00
'!extensions/html-language-features/server/lib/jquery.d.ts'
2016-01-18 09:50:41 +01:00
];
2018-03-05 12:38:25 +01:00
const copyrightHeaderLines = [
2015-11-24 19:09:31 +01: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 12:38:25 +01:00
];
2015-11-24 19:09:31 +01:00
2017-06-20 16:21:44 +02:00
gulp.task('eslint', () => {
2017-10-31 16:11:10 +01:00
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
2017-06-20 16:21:44 +02:00
.pipe(filter(eslintFilter))
.pipe(gulpeslint('src/.eslintrc'))
.pipe(gulpeslint.formatEach('compact'))
.pipe(gulpeslint.failAfterError());
});
2016-07-28 10:28:01 +02:00
gulp.task('tslint', () => {
2017-11-13 11:12:56 +01:00
const options = { emitError: true };
2016-02-18 16:02:16 +01:00
2017-10-31 16:11:10 +01:00
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
2016-01-18 09:50:41 +01:00
.pipe(filter(tslintFilter))
2018-02-01 14:39:12 +01:00
.pipe(gulptslint.default({ rulesDirectory: 'build/lib/tslint' }))
.pipe(gulptslint.default.report(options));
2016-01-18 09:50:41 +01:00
});
2018-03-05 12:38:25 +01:00
function hygiene(some) {
2016-07-28 10:28:01 +02:00
let errorCount = 0;
2015-11-24 19:09:31 +01:00
2016-07-28 10:28:01 +02:00
const indentation = es.through(function (file) {
2018-03-05 12:38:25 +01:00
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
file.__lines = lines;
lines
.forEach((line, i) => {
if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK
} 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');
errorCount++;
}
});
2015-11-24 19:09:31 +01:00
this.emit('data', file);
});
2016-07-28 10:28:01 +02:00
const copyrights = es.through(function (file) {
2018-03-05 12:38:25 +01: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 19:09:31 +01:00
}
2015-11-25 09:47:15 +01:00
2015-11-25 09:29:48 +01:00
this.emit('data', file);
2015-11-24 19:09:31 +01:00
});
2016-10-07 17:39:26 +02:00
const formatting = es.map(function (file, cb) {
2016-10-07 17:25:32 +02:00
tsfmt.processString(file.path, file.contents.toString('utf8'), {
2018-03-06 16:33:20 +01:00
verify: false,
2016-10-07 17:25:32 +02:00
tsfmt: true,
2018-04-22 20:12:03 +02:00
// verbose: true,
2018-02-01 14:39:12 +01:00
// keep checkJS happy
editorconfig: undefined,
replace: undefined,
tsconfig: undefined,
2018-04-22 20:12:03 +02:00
tsconfigFile: undefined,
tslint: undefined,
tslintFile: undefined,
tsfmtFile: undefined,
vscode: undefined,
vscodeFile: undefined
2016-10-07 17:25:32 +02:00
}).then(result => {
2018-03-06 22:24:47 +01:00
let original = result.src.replace(/\r\n/gm, '\n');
let formatted = result.dest.replace(/\r\n/gm, '\n');
2018-03-06 16:33:20 +01:00
if (original !== formatted) {
console.error('File not formatted:', file.relative);
2016-10-07 17:25:32 +02:00
errorCount++;
}
2016-10-07 17:39:26 +02:00
cb(null, file);
2016-10-07 17:25:32 +02:00
}, err => {
2016-10-07 17:39:26 +02:00
cb(err);
2016-10-07 17:25:32 +02:00
});
});
2017-11-13 11:12:56 +01:00
const tslintConfiguration = tslint.Configuration.findConfiguration('tslint.json', '.');
const tslintOptions = { fix: false, formatter: 'json' };
const tsLinter = new tslint.Linter(tslintOptions);
2016-10-07 17:25:32 +02:00
const tsl = es.through(function (file) {
2016-07-28 10:28:01 +02:00
const contents = file.contents.toString('utf8');
tsLinter.lint(file.relative, contents, tslintConfiguration.results);
2016-02-18 15:24:52 +01:00
this.emit('data', file);
});
2018-02-27 11:05:11 +01: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 10:28:01 +02:00
.pipe(filter(f => !f.stat.isDirectory()))
2015-11-24 19:09:31 +01:00
.pipe(filter(indentationFilter))
.pipe(indentation)
2016-01-18 09:50:41 +01:00
.pipe(filter(copyrightFilter))
2017-06-20 16:21:44 +02:00
.pipe(copyrights);
const typescript = result
.pipe(filter(tslintFilter))
2016-10-07 17:25:32 +02:00
.pipe(formatting)
2017-06-20 16:21:44 +02: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 16:21:44 +02:00
return es.merge(typescript, javascript)
.pipe(es.through(function (data) {
count++;
2017-11-13 11:32:38 +01: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 12:26:07 +01:00
}
errorCount += tslintResult.failures.length;
}
2015-11-24 19:09:31 +01:00
if (errorCount > 0) {
2015-11-26 09:57:29 +01:00
this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
2015-11-24 19:09:31 +01:00
} else {
this.emit('end');
}
}));
2018-02-27 11:05:11 +01: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 11:13:47 +01:00
if (err && err.code === 'ENOENT') { // ignore deletions
return c(null);
} else if (err) {
2018-02-27 11:05:11 +01: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 11:13:47 +01:00
return pall(fns, { concurrency: 4 })
.then(r => r.filter(p => !!p));
2018-02-27 11:05:11 +01:00
}
2018-02-27 11:05:11 +01:00
gulp.task('hygiene', () => hygiene());
2016-07-28 10:28:01 +02:00
// this allows us to run hygiene as a git pre-commit hook
if (require.main === module) {
2016-07-28 10:28:01 +02: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 12:38:25 +01: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 10:28:01 +02:00
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
2016-03-04 10:44:20 +01:00
if (err) {
console.error();
console.error(err);
process.exit(1);
2018-02-27 11:05:11 +01:00
return;
2016-03-04 10:44:20 +01:00
}
2016-07-28 10:28:01 +02:00
const some = out
2016-03-04 10:44:20 +01:00
.split(/\r?\n/)
2016-07-28 10:28:01 +02:00
.filter(l => !!l);
2017-11-06 15:39:46 +01:00
if (some.length > 0) {
2018-02-27 11:05:11 +01:00
console.log('Reading git index versions...');
createGitIndexVinyls(some)
2018-03-05 12:38:25 +01:00
.then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls))
2018-02-27 11:05:11 +01:00
.on('end', () => c())
.on('error', e)))
.catch(err => {
console.error();
console.error(err);
process.exit(1);
});
2017-11-06 15:39:46 +01:00
}
});
2018-03-05 12:38:25 +01:00
}
}