gulpfile.hygiene.js 5.1 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
var tslint = require('gulp-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}',
J
Joao Moreno 已提交
29 30 31 32
];

var indentationFilter = [
	'**',
33 34 35
	'!ThirdPartyNotices.txt',
	'!**/*.md',
	'!**/*.yml',
A
Alex Dima 已提交
36
	'!**/lib/**',
J
Joao Moreno 已提交
37 38 39 40
	'!**/*.d.ts',
	'!extensions/typescript/server/**',
	'!test/assert.js',
	'!**/package.json',
A
Alex Dima 已提交
41
	'!**/npm-shrinkwrap.json',
J
Joao Moreno 已提交
42 43 44 45
	'!**/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 已提交
46
	'!**/vs/base/common/marked/raw.marked.js',
J
Joao Moreno 已提交
47 48 49 50 51 52 53 54 55 56 57 58
	'!**/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 已提交
59
var copyrightFilter = [
J
Joao Moreno 已提交
60 61 62 63 64
	'**',
	'!**/*.json',
	'!**/*.html',
	'!**/test/**',
	'!**/*.md',
65 66
	'!**/*.bat',
	'!**/*.cmd',
B
Benjamin Pasero 已提交
67
	'!resources/win32/bin/code.js',
J
Joao Moreno 已提交
68 69 70 71 72
	'!**/*.sh',
	'!**/*.txt',
	'!src/vs/editor/standalone-languages/swift.ts',
];

J
Joao Moreno 已提交
73 74 75 76 77 78 79 80 81
var tslintFilter = [
	'src/**/*.ts',
	'extensions/**/*.ts',
	'!**/*.d.ts',
	'!**/typings/**',
	'!**/*.test.ts',
	'!src/vs/editor/standalone-languages/test/**'
];

J
Joao Moreno 已提交
82 83 84 85 86 87 88
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 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * Reports tslint erros in the format:
 * src/helloWorld.c:5:3: warning: implicit declaration of function ‘prinft’
 */
var lintReporter = function (output, file, options) {
	var relativeBase = file.base.substring(file.cwd.length + 1).replace('\\', '/');
	output.forEach(function (e) {
		var message = relativeBase + e.name + ':' + (e.startPosition.line + 1) + ':' + (e.startPosition.character + 1) + ': ' + e.failure;
		console.log('[tslint] ' + message);
	});
};

gulp.task('tslint', function () {
	return gulp.src(all, { base: '.' })
		.pipe(filter(tslintFilter))
		.pipe(tslint({ rulesDirectory: 'node_modules/tslint-microsoft-contrib' }))
		.pipe(tslint.report(lintReporter, {
			summarizeFailureOutput: false,
			emitError: false
		}));
});

111
var hygiene = exports.hygiene = function (some) {
J
Joao Moreno 已提交
112 113 114 115
	var errorCount = 0;

	var eol = es.through(function (file) {
		if (/\r\n?/g.test(file.contents.toString('utf8'))) {
116
			console.error(file.relative + ': Bad EOL found');
J
Joao Moreno 已提交
117 118 119 120 121 122 123 124 125 126
			errorCount++;
		}

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

	var indentation = es.through(function (file) {
		file.contents
			.toString('utf8')
			.split(/\r\n|\r|\n/)
J
Joao Moreno 已提交
127
			.forEach(function (line, i) {
J
Joao Moreno 已提交
128 129
				if (/^\s*$/.test(line)) {
					// empty or whitespace lines are OK
J
Joao Moreno 已提交
130 131 132 133 134
				} else if (/^[\t]*[^\s]/.test(line)) {
					// good indent
				} else if (/^[\t]* \*/.test(line)) {
					// block comment using an extra space
				} else {
135
					console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
J
Joao Moreno 已提交
136 137 138 139 140 141 142 143 144
					errorCount++;
				}
			});

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

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

J
Joao Moreno 已提交
149
		this.emit('data', file);
J
Joao Moreno 已提交
150 151
	});

152
	return gulp.src(some || all, { base: '.' })
J
Joao Moreno 已提交
153
		.pipe(filter(function (f) { return !f.stat.isDirectory(); }))
154
		.pipe(filter(eolFilter))
J
Joao Moreno 已提交
155 156 157
		.pipe(eol)
		.pipe(filter(indentationFilter))
		.pipe(indentation)
J
Joao Moreno 已提交
158
		.pipe(filter(copyrightFilter))
J
Joao Moreno 已提交
159 160 161
		.pipe(copyrights)
		.pipe(es.through(null, function () {
			if (errorCount > 0) {
J
Joao Moreno 已提交
162
				this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
J
Joao Moreno 已提交
163 164 165 166
			} else {
				this.emit('end');
			}
		}));
167 168 169 170
};

gulp.task('hygiene', function () {
	return hygiene();
J
Joao Moreno 已提交
171
});
172 173 174 175 176 177

// 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 已提交
178 179
			console.error();
			console.error(err);
180 181 182 183 184 185 186 187
			process.exit(1);
		}

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

		hygiene(some).on('error', function (err) {
J
Joao Moreno 已提交
188 189
			console.error();
			console.error(err);
190 191 192 193
			process.exit(1);
		});
	});
}