gulpfile.hygiene.js 5.7 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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');
var path = require('path');
J
Joao Moreno 已提交
10 11
var gulptslint = require('gulp-tslint');
var tslint = require('tslint');
J
Joao Moreno 已提交
12

13
var all = [
14
	'*',
J
Joao Moreno 已提交
15 16 17 18
	'build/**/*',
	'extensions/**/*',
	'scripts/**/*',
	'src/**/*',
19 20 21 22 23
	'test/**/*'
];

var eolFilter = [
	'**',
24 25
	'!ThirdPartyNotices.txt',
	'!LICENSE.txt',
J
Joao Moreno 已提交
26 27 28
	'!extensions/**/out/**',
	'!**/node_modules/**',
	'!**/fixtures/**',
29
	'!**/*.{svg,exe,png,scpt,bat,cmd,cur,ttf,woff,eot}',
J
Joao Moreno 已提交
30 31 32 33
];

var indentationFilter = [
	'**',
34 35
	'!ThirdPartyNotices.txt',
	'!**/*.md',
36
	'!**/*.template',
37
	'!**/*.yml',
A
Alex Dima 已提交
38
	'!**/lib/**',
J
Joao Moreno 已提交
39 40 41 42
	'!**/*.d.ts',
	'!extensions/typescript/server/**',
	'!test/assert.js',
	'!**/package.json',
A
Alex Dima 已提交
43
	'!**/npm-shrinkwrap.json',
J
Joao Moreno 已提交
44 45 46 47
	'!**/octicons/**',
	'!**/vs/languages/sass/test/common/example.scss',
	'!**/vs/languages/less/common/parser/less.grammar.txt',
	'!**/vs/languages/css/common/buildscripts/css-schema.xml',
J
Johannes Rieken 已提交
48
	'!**/vs/base/common/marked/raw.marked.js',
J
Joao Moreno 已提交
49 50 51 52 53 54 55 56 57 58 59 60
	'!**/vs/base/common/winjs.base.raw.js',
	'!**/vs/base/node/terminateProcess.sh',
	'!**/vs/base/node/terminateProcess.sh',
	'!**/vs/text.js',
	'!**/vs/nls.js',
	'!**/vs/css.js',
	'!**/vs/loader.js',
	'!extensions/**/snippets/**',
	'!extensions/**/syntaxes/**',
	'!extensions/**/themes/**',
];

J
Joao Moreno 已提交
61
var copyrightFilter = [
J
Joao Moreno 已提交
62
	'**',
63
	'!**/*.desktop',
J
Joao Moreno 已提交
64 65
	'!**/*.json',
	'!**/*.html',
66
	'!**/*.template',
J
Joao Moreno 已提交
67 68
	'!**/test/**',
	'!**/*.md',
69 70
	'!**/*.bat',
	'!**/*.cmd',
B
Benjamin Pasero 已提交
71
	'!resources/win32/bin/code.js',
J
Joao Moreno 已提交
72 73 74 75 76
	'!**/*.sh',
	'!**/*.txt',
	'!src/vs/editor/standalone-languages/swift.ts',
];

J
Joao Moreno 已提交
77 78 79 80 81
var tslintFilter = [
	'src/**/*.ts',
	'extensions/**/*.ts',
	'!**/*.d.ts',
	'!**/typings/**',
A
Alex Dima 已提交
82 83 84 85
	'!src/vs/base/**/*.test.ts',
	'!src/vs/languages/**/*.test.ts',
	'!src/vs/workbench/**/*.test.ts',
	'!extensions/**/*.test.ts',
J
Joao Moreno 已提交
86 87
];

J
Joao Moreno 已提交
88 89 90 91 92 93 94
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');

J
Joao Moreno 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
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
	);
}
J
Joao Moreno 已提交
108 109

gulp.task('tslint', function () {
J
Joao Moreno 已提交
110 111 112 113 114 115
	var options = { summarizeFailureOutput: true };

	function reporter(failures) {
		failures.forEach(failureReporter);
	}

J
Joao Moreno 已提交
116 117
	return gulp.src(all, { base: '.' })
		.pipe(filter(tslintFilter))
J
Joao Moreno 已提交
118 119
		.pipe(gulptslint({ rulesDirectory: 'build/tslintRules' }))
		.pipe(gulptslint.report(reporter, options));
J
Joao Moreno 已提交
120 121
});

122
var hygiene = exports.hygiene = function (some) {
J
Joao Moreno 已提交
123
	var errorCount = 0;
J
Joao Moreno 已提交
124 125 126

	var eol = es.through(function (file) {
		if (/\r\n?/g.test(file.contents.toString('utf8'))) {
127
			console.error(file.relative + ': Bad EOL found');
J
Joao Moreno 已提交
128 129 130 131 132 133 134 135 136 137
			errorCount++;
		}

		this.emit('data', file);
	});

	var indentation = es.through(function (file) {
		file.contents
			.toString('utf8')
			.split(/\r\n|\r|\n/)
J
Joao Moreno 已提交
138
			.forEach(function (line, i) {
J
Joao Moreno 已提交
139 140
				if (/^\s*$/.test(line)) {
					// empty or whitespace lines are OK
J
Joao Moreno 已提交
141 142 143 144 145
				} else if (/^[\t]*[^\s]/.test(line)) {
					// good indent
				} else if (/^[\t]* \*/.test(line)) {
					// block comment using an extra space
				} else {
146
					console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
J
Joao Moreno 已提交
147 148 149 150 151 152 153 154 155
					errorCount++;
				}
			});

		this.emit('data', file);
	});

	var copyrights = es.through(function (file) {
		if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) {
156
			console.error(file.relative + ': Missing or bad copyright statement');
J
Joao Moreno 已提交
157 158
			errorCount++;
		}
J
Joao Moreno 已提交
159

J
Joao Moreno 已提交
160
		this.emit('data', file);
J
Joao Moreno 已提交
161 162
	});

J
Joao Moreno 已提交
163
	var tsl = es.through(function(file) {
J
Joao Moreno 已提交
164
		configuration = tslint.findConfiguration(null, '.');
J
Joao Moreno 已提交
165 166 167 168 169 170
		var options = {
			formatter: 'json',
			configuration: configuration,
			rulesDirectory: 'build/tslintRules',
		}
		var contents = file.contents.toString('utf8');
J
Joao Moreno 已提交
171
		var linter = new tslint(file.relative, contents, options);
J
Joao Moreno 已提交
172 173
		var result = linter.lint();
		if (result.failureCount > 0) {
J
Joao Moreno 已提交
174
			result.failures.forEach(failureReporter);
J
Joao Moreno 已提交
175 176 177 178
			errorCount += result.failureCount;
		}
		this.emit('data', file);
	});
179

180
	return gulp.src(some || all, { base: '.' })
J
Joao Moreno 已提交
181
		.pipe(filter(function (f) { return !f.stat.isDirectory(); }))
182
		.pipe(filter(eolFilter))
J
Joao Moreno 已提交
183 184 185
		.pipe(eol)
		.pipe(filter(indentationFilter))
		.pipe(indentation)
J
Joao Moreno 已提交
186
		.pipe(filter(copyrightFilter))
J
Joao Moreno 已提交
187
		.pipe(copyrights)
188
		.pipe(filter(tslintFilter))
J
Joao Moreno 已提交
189
		.pipe(tsl)
J
Joao Moreno 已提交
190 191
		.pipe(es.through(null, function () {
			if (errorCount > 0) {
J
Joao Moreno 已提交
192
				this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
J
Joao Moreno 已提交
193 194 195 196
			} else {
				this.emit('end');
			}
		}));
197 198 199 200
};

gulp.task('hygiene', function () {
	return hygiene();
J
Joao Moreno 已提交
201
});
202 203 204 205 206 207

// this allows us to run this as a git pre-commit hook
if (require.main === module) {
	var cp = require('child_process');
	cp.exec('git diff --cached --name-only', function (err, out) {
		if (err) {
J
Joao Moreno 已提交
208 209
			console.error();
			console.error(err);
210 211 212 213 214 215 216 217
			process.exit(1);
		}

		var some = out
			.split(/\r?\n/)
			.filter(function (l) { return !!l; });

		hygiene(some).on('error', function (err) {
J
Joao Moreno 已提交
218 219
			console.error();
			console.error(err);
220 221 222 223
			process.exit(1);
		});
	});
}