vscode/build/gulpfile.hygiene.js

228 lines
6.1 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');
const tslint = require('tslint');
const all = [
'*',
2015-11-24 18:09:31 +00:00
'build/**/*',
'extensions/**/*',
'scripts/**/*',
'src/**/*',
'test/**/*'
];
2016-07-28 08:28:01 +00:00
const eolFilter = [
'**',
'!ThirdPartyNotices.txt',
'!LICENSE.txt',
2015-11-24 18:09:31 +00:00
'!extensions/**/out/**',
'!**/node_modules/**',
'!**/fixtures/**',
2016-07-20 14:58:02 +00:00
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot}',
2016-05-31 14:29:52 +00:00
'!build/{lib,tslintRules}/**/*.js',
'!build/monaco/**',
'!build/win32/**'
2015-11-24 18:09:31 +00:00
];
2016-07-28 08:28:01 +00:00
const indentationFilter = [
2015-11-24 18:09:31 +00:00
'**',
'!ThirdPartyNotices.txt',
'!**/*.md',
'!**/*.template',
'!**/*.yml',
2015-12-10 17:38:31 +00:00
'!**/lib/**',
2015-11-24 18:09:31 +00:00
'!**/*.d.ts',
'!**/*.d.ts.recipe',
2015-11-24 18:09:31 +00:00
'!extensions/typescript/server/**',
'!test/assert.js',
'!**/package.json',
2015-11-30 15:27:47 +00:00
'!**/npm-shrinkwrap.json',
2015-11-24 18:09:31 +00:00
'!**/octicons/**',
'!**/vs/languages/sass/test/common/example.scss',
'!**/vs/languages/less/common/parser/less.grammar.txt',
'!**/vs/languages/css/common/buildscripts/css-schema.xml',
2016-01-21 15:15:42 +00:00
'!**/vs/base/common/marked/raw.marked.js',
2015-11-24 18:09:31 +00:00
'!**/vs/base/common/winjs.base.raw.js',
'!**/vs/base/node/terminateProcess.sh',
'!**/vs/nls.js',
'!**/vs/css.js',
'!**/vs/loader.js',
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
2016-04-11 13:54:58 +00:00
'!extensions/**/themes/**',
'!extensions/**/colorize-fixtures/**',
'!extensions/vscode-api-tests/testWorkspace/**'
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
'!**/test/**',
'!**/*.md',
'!**/*.bat',
'!**/*.cmd',
2016-01-05 15:32:49 +00:00
'!resources/win32/bin/code.js',
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',
'!extensions/markdown/media/tomorrow.css'
2015-11-24 18:09:31 +00:00
];
2016-07-28 08:28:01 +00:00
const tslintFilter = [
2016-01-18 08:50:41 +00:00
'src/**/*.ts',
'extensions/**/*.ts',
'!**/*.d.ts',
'!**/typings/**',
2016-02-18 09:07:33 +00:00
'!src/vs/base/**/*.test.ts',
2016-09-15 09:17:56 +00:00
'!extensions/typescript/test/colorize-fixtures/**',
'!extensions/vscode-api-tests/testWorkspace/**',
2016-02-18 09:07:33 +00:00
'!src/vs/languages/**/*.test.ts',
'!src/vs/workbench/**/*.test.ts',
2016-07-28 07:43:20 +00:00
'!extensions/**/*.test.ts'
2016-01-18 08:50:41 +00:00
];
2016-07-28 08:28:01 +00:00
const copyrightHeader = [
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.',
' *--------------------------------------------------------------------------------------------*/'
].join('\n');
2016-07-28 08:28:01 +00:00
function reportFailures(failures) {
failures.forEach(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;
2016-01-18 08:50:41 +00:00
2016-07-28 08:28:01 +00:00
console.error(`${ name }:${ line + 1}:${ character + 1 }:${ failure.failure }`);
});
}
2016-02-18 15:02:16 +00:00
2016-07-28 08:28:01 +00:00
gulp.task('tslint', () => {
const options = { summarizeFailureOutput: true };
2016-02-18 15:02:16 +00:00
2016-01-18 08:50:41 +00:00
return gulp.src(all, { base: '.' })
.pipe(filter(tslintFilter))
2016-03-07 12:48:06 +00:00
.pipe(gulptslint({ rulesDirectory: 'build/lib/tslint' }))
2016-07-28 08:28:01 +00:00
.pipe(gulptslint.report(reportFailures, options));
2016-01-18 08:50:41 +00:00
});
2016-07-28 08:28:01 +00:00
const hygiene = exports.hygiene = (some, options) => {
2016-03-04 15:26:05 +00:00
options = options || {};
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 eol = es.through(function (file) {
2015-11-24 18:09:31 +00:00
if (/\r\n?/g.test(file.contents.toString('utf8'))) {
console.error(file.relative + ': Bad EOL found');
2015-11-24 18:09:31 +00:00
errorCount++;
}
this.emit('data', file);
});
2016-07-28 08:28:01 +00:00
const indentation = es.through(function (file) {
2015-11-24 18:09:31 +00:00
file.contents
.toString('utf8')
.split(/\r\n|\r|\n/)
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) {
2015-11-24 18:09:31 +00:00
if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) {
console.error(file.relative + ': Missing or bad copyright statement');
2015-11-24 18:09:31 +00:00
errorCount++;
}
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-07-28 08:28:01 +00:00
const tsl = es.through(function(file) {
const configuration = tslint.findConfiguration(null, '.');
const options = { configuration, formatter: 'json', rulesDirectory: 'build/lib/tslint' };
const contents = file.contents.toString('utf8');
const linter = new tslint(file.relative, contents, options);
const result = linter.lint();
2016-02-18 14:24:52 +00:00
if (result.failureCount > 0) {
2016-07-28 08:28:01 +00:00
reportFailures(result.failures);
2016-02-18 14:24:52 +00:00
errorCount += result.failureCount;
}
2016-07-28 08:28:01 +00:00
2016-02-18 14:24:52 +00:00
this.emit('data', file);
});
return gulp.src(some || all, { base: '.' })
2016-07-28 08:28:01 +00:00
.pipe(filter(f => !f.stat.isDirectory()))
.pipe(filter(eolFilter))
2016-03-04 09:44:20 +00:00
.pipe(options.skipEOL ? es.through() : eol)
2015-11-24 18:09:31 +00:00
.pipe(filter(indentationFilter))
.pipe(indentation)
2016-01-18 08:50:41 +00:00
.pipe(filter(copyrightFilter))
2015-11-24 18:09:31 +00:00
.pipe(copyrights)
.pipe(filter(tslintFilter))
2016-02-18 15:59:14 +00:00
.pipe(tsl)
2015-11-24 18:09:31 +00:00
.pipe(es.through(null, function () {
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');
}
}));
};
2016-07-28 08:28:01 +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');
cp.exec('git config core.autocrlf', (err, out) => {
const skipEOL = out.trim() === 'true';
2016-03-04 09:44:20 +00:00
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);
}
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);
2016-07-28 08:28:01 +00:00
hygiene(some, { skipEOL: skipEOL }).on('error', err => {
2016-03-04 09:44:20 +00:00
console.error();
console.error(err);
process.exit(1);
});
});
});
}