gulpfile.hygiene.js 6.1 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6
'use strict';
J
Joao Moreno 已提交
7

J
Joao Moreno 已提交
8 9 10 11 12 13 14
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 = [
15
	'*',
J
Joao Moreno 已提交
16 17 18 19
	'build/**/*',
	'extensions/**/*',
	'scripts/**/*',
	'src/**/*',
20 21 22
	'test/**/*'
];

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

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

J
Joao Moreno 已提交
66
const copyrightFilter = [
J
Joao Moreno 已提交
67
	'**',
68
	'!**/*.desktop',
J
Joao Moreno 已提交
69 70
	'!**/*.json',
	'!**/*.html',
71
	'!**/*.template',
J
Joao Moreno 已提交
72 73
	'!**/test/**',
	'!**/*.md',
74 75
	'!**/*.bat',
	'!**/*.cmd',
B
Benjamin Pasero 已提交
76
	'!resources/win32/bin/code.js',
M
Martin Aeschlimann 已提交
77
	'!**/*.xml',
J
Joao Moreno 已提交
78 79
	'!**/*.sh',
	'!**/*.txt',
D
Daniel Imms 已提交
80
	'!**/*.xpm',
81
	'!extensions/markdown/media/tomorrow.css'
J
Joao Moreno 已提交
82 83
];

J
Joao Moreno 已提交
84
const tslintFilter = [
J
Joao Moreno 已提交
85 86 87 88
	'src/**/*.ts',
	'extensions/**/*.ts',
	'!**/*.d.ts',
	'!**/typings/**',
A
Alex Dima 已提交
89
	'!src/vs/base/**/*.test.ts',
E
Erich Gamma 已提交
90 91
	'!extensions/typescript/test/colorize-fixtures/**',
	'!extensions/vscode-api-tests/testWorkspace/**',
A
Alex Dima 已提交
92 93
	'!src/vs/languages/**/*.test.ts',
	'!src/vs/workbench/**/*.test.ts',
J
Joao Moreno 已提交
94
	'!extensions/**/*.test.ts'
J
Joao Moreno 已提交
95 96
];

J
Joao Moreno 已提交
97
const copyrightHeader = [
J
Joao Moreno 已提交
98 99 100 101 102 103
	'/*---------------------------------------------------------------------------------------------',
	' *  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 已提交
104 105 106 107 108 109
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;
J
Joao Moreno 已提交
110

J
Joao Moreno 已提交
111 112 113
		console.error(`${ name }:${ line + 1}:${ character + 1 }:${ failure.failure }`);
	});
}
J
Joao Moreno 已提交
114

J
Joao Moreno 已提交
115 116
gulp.task('tslint', () => {
	const options = { summarizeFailureOutput: true };
J
Joao Moreno 已提交
117

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

J
Joao Moreno 已提交
124
const hygiene = exports.hygiene = (some, options) => {
J
fix npe  
Joao Moreno 已提交
125
	options = options || {};
J
Joao Moreno 已提交
126
	let errorCount = 0;
J
Joao Moreno 已提交
127

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

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

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

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

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

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

J
Joao Moreno 已提交
166 167 168 169 170 171 172
	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();

J
Joao Moreno 已提交
173
		if (result.failureCount > 0) {
J
Joao Moreno 已提交
174
			reportFailures(result.failures);
J
Joao Moreno 已提交
175 176
			errorCount += result.failureCount;
		}
J
Joao Moreno 已提交
177

J
Joao Moreno 已提交
178 179
		this.emit('data', file);
	});
180

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

J
Joao Moreno 已提交
200
gulp.task('hygiene', () => hygiene());
201

J
Joao Moreno 已提交
202
// this allows us to run hygiene as a git pre-commit hook
203
if (require.main === module) {
J
Joao Moreno 已提交
204 205 206 207
	const cp = require('child_process');

	cp.exec('git config core.autocrlf', (err, out) => {
		const skipEOL = out.trim() === 'true';
J
Joao Moreno 已提交
208

J
Joao Moreno 已提交
209
		cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
J
Joao Moreno 已提交
210 211 212 213 214
			if (err) {
				console.error();
				console.error(err);
				process.exit(1);
			}
215

J
Joao Moreno 已提交
216
			const some = out
J
Joao Moreno 已提交
217
				.split(/\r?\n/)
J
Joao Moreno 已提交
218
				.filter(l => !!l);
219

J
Joao Moreno 已提交
220
			hygiene(some, { skipEOL: skipEOL }).on('error', err => {
J
Joao Moreno 已提交
221 222 223 224
				console.error();
				console.error(err);
				process.exit(1);
			});
225 226 227
		});
	});
}