vscode/build/gulpfile.hygiene.js

229 lines
6 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.
*--------------------------------------------------------------------------------------------*/
var gulp = require('gulp');
var filter = require('gulp-filter');
var es = require('event-stream');
2016-02-18 15:02:16 +00:00
var gulptslint = require('gulp-tslint');
var tslint = require('tslint');
2015-11-24 18:09:31 +00:00
var all = [
'*',
2015-11-24 18:09:31 +00:00
'build/**/*',
'extensions/**/*',
'scripts/**/*',
'src/**/*',
'test/**/*'
];
var eolFilter = [
'**',
'!ThirdPartyNotices.txt',
'!LICENSE.txt',
2015-11-24 18:09:31 +00:00
'!extensions/**/out/**',
'!**/node_modules/**',
'!**/fixtures/**',
'!**/*.{svg,exe,png,scpt,bat,cmd,cur,ttf,woff,eot}',
2016-03-07 11:52:52 +00:00
'!build/{lib,tslintRules}/**/*.js'
2015-11-24 18:09:31 +00:00
];
var indentationFilter = [
'**',
'!ThirdPartyNotices.txt',
'!**/*.md',
'!**/*.template',
'!**/*.yml',
2015-12-10 17:38:31 +00:00
'!**/lib/**',
2015-11-24 18:09:31 +00:00
'!**/*.d.ts',
'!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/text.js',
'!**/vs/nls.js',
'!**/vs/css.js',
'!**/vs/loader.js',
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
2016-03-07 11:52:52 +00:00
'!extensions/**/themes/**'
2015-11-24 18:09:31 +00:00
];
2016-01-18 08:50:41 +00:00
var 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',
2015-11-24 18:09:31 +00:00
'!**/*.sh',
'!**/*.txt',
2016-03-24 21:01:51 +00:00
'!**/*.xpm',
2015-11-24 18:09:31 +00:00
'!src/vs/editor/standalone-languages/swift.ts',
];
2016-01-18 08:50:41 +00:00
var tslintFilter = [
'src/**/*.ts',
'extensions/**/*.ts',
'!**/*.d.ts',
'!**/typings/**',
2016-02-18 09:07:33 +00:00
'!src/vs/base/**/*.test.ts',
'!src/vs/languages/**/*.test.ts',
'!src/vs/workbench/**/*.test.ts',
'!extensions/**/*.test.ts',
2016-01-18 08:50:41 +00:00
];
2015-11-24 18:09:31 +00:00
var copyrightHeader = [
'/*---------------------------------------------------------------------------------------------',
' * 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-02-18 15:02:16 +00:00
function failureReporter(failure) {
var name = failure.name || failure.fileName;
var position = failure.startPosition;
var line = position.lineAndCharacter ? position.lineAndCharacter.line : position.line;
var character = position.lineAndCharacter ? position.lineAndCharacter.character : position.character;
console.error(
name
+ ':' + (line + 1)
+ ':' + (character + 1)
+ ': ' + failure.failure
);
}
2016-01-18 08:50:41 +00:00
gulp.task('tslint', function () {
2016-02-18 15:02:16 +00:00
var options = { summarizeFailureOutput: true };
function reporter(failures) {
failures.forEach(failureReporter);
}
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-02-18 15:02:16 +00:00
.pipe(gulptslint.report(reporter, options));
2016-01-18 08:50:41 +00:00
});
2016-03-04 09:44:20 +00:00
var hygiene = exports.hygiene = function (some, options) {
2016-03-04 15:26:05 +00:00
options = options || {};
2016-02-18 14:24:52 +00:00
var errorCount = 0;
2015-11-24 18:09:31 +00:00
var eol = es.through(function (file) {
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);
});
var indentation = es.through(function (file) {
file.contents
.toString('utf8')
.split(/\r\n|\r|\n/)
2016-01-18 08:50:41 +00:00
.forEach(function (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);
});
var copyrights = es.through(function (file) {
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-02-18 15:59:14 +00:00
var tsl = es.through(function(file) {
2016-03-04 06:08:50 +00:00
var configuration = tslint.findConfiguration(null, '.');
2016-02-18 14:24:52 +00:00
var options = {
formatter: 'json',
configuration: configuration,
2016-03-07 12:48:06 +00:00
rulesDirectory: 'build/lib/tslint',
};
2016-02-18 14:24:52 +00:00
var contents = file.contents.toString('utf8');
2016-02-18 15:02:16 +00:00
var linter = new tslint(file.relative, contents, options);
2016-02-18 14:24:52 +00:00
var result = linter.lint();
if (result.failureCount > 0) {
2016-02-18 15:02:16 +00:00
result.failures.forEach(failureReporter);
2016-02-18 14:24:52 +00:00
errorCount += result.failureCount;
}
this.emit('data', file);
});
return gulp.src(some || all, { base: '.' })
2015-11-24 18:09:31 +00:00
.pipe(filter(function (f) { return !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');
}
}));
};
gulp.task('hygiene', function () {
return hygiene();
2015-11-24 18:09:31 +00:00
});
// this allows us to run this as a git pre-commit hook
if (require.main === module) {
var cp = require('child_process');
2016-03-04 09:44:20 +00:00
cp.exec('git config core.autocrlf', function (err, out) {
var skipEOL = out.trim() === 'true';
2016-03-07 12:00:56 +00:00
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, function (err, out) {
2016-03-04 09:44:20 +00:00
if (err) {
console.error();
console.error(err);
process.exit(1);
}
2016-03-04 09:44:20 +00:00
var some = out
.split(/\r?\n/)
.filter(function (l) { return !!l; });
2016-03-04 09:44:20 +00:00
hygiene(some, { skipEOL: skipEOL }).on('error', function (err) {
console.error();
console.error(err);
process.exit(1);
});
});
});
}