gulpfile.hygiene.js 4.0 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  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');

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

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

var indentationFilter = [
	'**',
	'!**/*.d.ts',
	'!extensions/typescript/server/**',
	'!test/assert.js',
	'!**/package.json',
A
Alex Dima 已提交
33
	'!**/npm-shrinkwrap.json',
J
Joao Moreno 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	'!**/octicons/**',
	'!**/vs/languages/sass/test/common/example.scss',
	'!**/vs/languages/less/common/parser/less.grammar.txt',
	'!**/vs/languages/css/common/buildscripts/css-schema.xml',
	'!**/vs/languages/markdown/common/raw.marked.js',
	'!**/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/**',
];

var copyrightFilterList = [
	'**',
	'!**/*.json',
	'!**/*.html',
	'!**/test/**',
	'!**/*.md',
	'!**/*.sh',
	'!**/*.txt',
	'!src/vs/editor/standalone-languages/swift.ts',
];

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');

69
var hygiene = exports.hygiene = function (some) {
J
Joao Moreno 已提交
70 71 72 73
	var errorCount = 0;

	var eol = es.through(function (file) {
		if (/\r\n?/g.test(file.contents.toString('utf8'))) {
74
			console.error(file.relative + ': Bad EOL found');
J
Joao Moreno 已提交
75 76 77 78 79 80 81 82 83 84 85
			errorCount++;
		}

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

	var indentation = es.through(function (file) {
		file.contents
			.toString('utf8')
			.split(/\r\n|\r|\n/)
			.forEach(function(line, i) {
J
Joao Moreno 已提交
86 87
				if (/^\s*$/.test(line)) {
					// empty or whitespace lines are OK
J
Joao Moreno 已提交
88 89 90 91 92
				} else if (/^[\t]*[^\s]/.test(line)) {
					// good indent
				} else if (/^[\t]* \*/.test(line)) {
					// block comment using an extra space
				} else {
93
					console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
J
Joao Moreno 已提交
94 95 96 97 98 99 100 101 102
					errorCount++;
				}
			});

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

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

J
Joao Moreno 已提交
107
		this.emit('data', file);
J
Joao Moreno 已提交
108 109
	});

110
	return gulp.src(some || all, { base: '.' })
J
Joao Moreno 已提交
111
		.pipe(filter(function (f) { return !f.stat.isDirectory(); }))
112
		.pipe(filter(eolFilter))
J
Joao Moreno 已提交
113 114 115 116 117 118 119
		.pipe(eol)
		.pipe(filter(indentationFilter))
		.pipe(indentation)
		.pipe(filter(copyrightFilterList))
		.pipe(copyrights)
		.pipe(es.through(null, function () {
			if (errorCount > 0) {
J
Joao Moreno 已提交
120
				this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
J
Joao Moreno 已提交
121 122 123 124
			} else {
				this.emit('end');
			}
		}));
125 126 127 128
};

gulp.task('hygiene', function () {
	return hygiene();
J
Joao Moreno 已提交
129
});
130 131 132 133 134 135

// 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 已提交
136 137
			console.error();
			console.error(err);
138 139 140 141 142 143 144 145
			process.exit(1);
		}

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

		hygiene(some).on('error', function (err) {
J
Joao Moreno 已提交
146 147
			console.error();
			console.error(err);
148 149 150 151
			process.exit(1);
		});
	});
}