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

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

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

var indentationFilter = [
	'**',
35 36
	'!ThirdPartyNotices.txt',
	'!**/*.md',
37
	'!**/*.template',
38
	'!**/*.yml',
A
Alex Dima 已提交
39
	'!**/lib/**',
J
Joao Moreno 已提交
40
	'!**/*.d.ts',
41
	'!**/*.d.ts.recipe',
J
Joao Moreno 已提交
42 43 44
	'!extensions/typescript/server/**',
	'!test/assert.js',
	'!**/package.json',
A
Alex Dima 已提交
45
	'!**/npm-shrinkwrap.json',
J
Joao Moreno 已提交
46 47 48 49
	'!**/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 已提交
50
	'!**/vs/base/common/marked/raw.marked.js',
J
Joao Moreno 已提交
51 52 53 54 55 56 57
	'!**/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/**',
58
	'!extensions/**/themes/**',
59 60
	'!extensions/**/colorize-fixtures/**',
	'!extensions/vscode-api-tests/testWorkspace/**'
J
Joao Moreno 已提交
61 62
];

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

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

J
Joao Moreno 已提交
91 92 93 94 95 96 97
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 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
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 已提交
111 112

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

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

J
Joao Moreno 已提交
119 120
	return gulp.src(all, { base: '.' })
		.pipe(filter(tslintFilter))
J
Joao Moreno 已提交
121
		.pipe(gulptslint({ rulesDirectory: 'build/lib/tslint' }))
J
Joao Moreno 已提交
122
		.pipe(gulptslint.report(reporter, options));
J
Joao Moreno 已提交
123 124
});

J
Joao Moreno 已提交
125
var hygiene = exports.hygiene = function (some, options) {
J
fix npe  
Joao Moreno 已提交
126
	options = options || {};
J
Joao Moreno 已提交
127
	var errorCount = 0;
J
Joao Moreno 已提交
128 129 130

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

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

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

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

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

J
Joao Moreno 已提交
164
		this.emit('data', file);
J
Joao Moreno 已提交
165 166
	});

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

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

gulp.task('hygiene', function () {
	return hygiene();
J
Joao Moreno 已提交
205
});
206 207 208 209

// this allows us to run this as a git pre-commit hook
if (require.main === module) {
	var cp = require('child_process');
J
Joao Moreno 已提交
210 211 212
	cp.exec('git config core.autocrlf', function (err, out) {
		var skipEOL = out.trim() === 'true';

213
		cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, function (err, out) {
J
Joao Moreno 已提交
214 215 216 217 218
			if (err) {
				console.error();
				console.error(err);
				process.exit(1);
			}
219

J
Joao Moreno 已提交
220 221 222
			var some = out
				.split(/\r?\n/)
				.filter(function (l) { return !!l; });
223

J
Joao Moreno 已提交
224 225 226 227 228
			hygiene(some, { skipEOL: skipEOL }).on('error', function (err) {
				console.error();
				console.error(err);
				process.exit(1);
			});
229 230 231
		});
	});
}