Skip to content

Commit

Permalink
update hygiene script
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Mar 14, 2018
1 parent ec65684 commit 4670dbf
Showing 1 changed file with 65 additions and 72 deletions.
137 changes: 65 additions & 72 deletions build/gulpfile.hygiene.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,56 @@ const all = [
'extensions/**/*',
'scripts/**/*',
'src/**/*',
'test/**/*'
'test/**/*',
'!**/node_modules/**'
];

const eolFilter = [
const indentationFilter = [
'**',

// except specific files
'!ThirdPartyNotices.txt',
'!LICENSE.txt',
'!extensions/**/out/**',
'!src/vs/nls.js',
'!src/vs/css.js',
'!src/vs/loader.js',
'!src/vs/base/common/marked/raw.marked.js',
'!src/vs/base/common/winjs.base.raw.js',
'!src/vs/base/node/terminateProcess.sh',
'!src/vs/base/node/ps-win.ps1',
'!test/assert.js',

// except specific folders
'!test/smoke/out/**',
'!**/node_modules/**',
'!**/fixtures/**',
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot}',
'!build/{lib,tslintRules}/**/*.js',
'!extensions/vscode-api-tests/testWorkspace/**',
'!extensions/vscode-api-tests/testWorkspace2/**',
'!build/monaco/**',
'!build/win32/**',
'!build/**/*.sh',
'!build/tfs/**/*.js',
'!**/Dockerfile'
];

const indentationFilter = [
'**',
'!ThirdPartyNotices.txt',
'!**/*.md',
'!**/*.ps1',
'!**/*.template',
'!**/*.yaml',
'!**/*.yml',
'!**/yarn.lock',
'!**/lib/**',
'!extensions/**/*.d.ts',
'!src/typings/**/*.d.ts',
'!src/vs/*/**/*.d.ts',
'!**/*.d.ts.recipe',
'!test/assert.js',
// except multiple specific files
'!**/package.json',
'!**/yarn.lock',

// except multiple specific folders
'!**/octicons/**',
'!**/vs/base/common/marked/raw.marked.js',
'!**/vs/base/common/winjs.base.raw.js',
'!**/vs/base/node/terminateProcess.sh',
'!**/vs/base/node/ps-win.ps1',
'!**/vs/nls.js',
'!**/vs/css.js',
'!**/vs/loader.js',
'!**/fixtures/**',
'!**/lib/**',
'!extensions/**/out/**',
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
'!extensions/**/themes/**',
'!extensions/**/colorize-fixtures/**',
'!extensions/vscode-api-tests/testWorkspace/**',
'!extensions/vscode-api-tests/testWorkspace2/**'

// except specific file types
'!src/vs/*/**/*.d.ts',
'!src/typings/**/*.d.ts',
'!extensions/**/*.d.ts',
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot,md,ps1,template,yaml,yml,d.ts.recipe}',
'!build/{lib,tslintRules}/**/*.js',
'!build/**/*.sh',
'!build/tfs/**/*.js',
'!**/Dockerfile',
'!extensions/markdown/media/*.js'
];

const copyrightFilter = [
Expand Down Expand Up @@ -135,12 +135,12 @@ const tslintFilter = [
'!extensions/html/server/lib/jquery.d.ts'
];

const copyrightHeader = [
const copyrightHeaderLines = [
'/*---------------------------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Licensed under the MIT License. See License.txt in the project root for license information.',
' *--------------------------------------------------------------------------------------------*/'
].join('\n');
];

gulp.task('eslint', () => {
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
Expand All @@ -159,23 +159,14 @@ gulp.task('tslint', () => {
.pipe(gulptslint.default.report(options));
});

function hygiene(some, options) {
options = options || {};
function hygiene(some) {
let errorCount = 0;

const eol = es.through(function (file) {
if (/\r\n?/g.test(file.contents.toString('utf8'))) {
console.error(file.relative + ': Bad EOL found');
errorCount++;
}

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

const indentation = es.through(function (file) {
file.contents
.toString('utf8')
.split(/\r\n|\r|\n/)
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
file.__lines = lines;

lines
.forEach((line, i) => {
if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK
Expand All @@ -193,17 +184,22 @@ function hygiene(some, options) {
});

const copyrights = es.through(function (file) {
if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) {
console.error(file.relative + ': Missing or bad copyright statement');
errorCount++;
const lines = file.__lines;

for (let i = 0; i < copyrightHeaderLines.length; i++) {
if (lines[i] !== copyrightHeaderLines[i]) {
console.error(file.relative + ': Missing or bad copyright statement');
errorCount++;
break;
}
}

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

const formatting = es.map(function (file, cb) {
tsfmt.processString(file.path, file.contents.toString('utf8'), {
verify: true,
verify: false,
tsfmt: true,
// verbose: true
// keep checkJS happy
Expand All @@ -212,8 +208,11 @@ function hygiene(some, options) {
tsconfig: undefined,
tslint: undefined
}).then(result => {
if (result.error) {
console.error(result.message);
let original = result.src.replace(/\r\n/gm, '\n');
let formatted = result.dest.replace(/\r\n/gm, '\n');

if (original !== formatted) {
console.error('File not formatted:', file.relative);
errorCount++;
}
cb(null, file);
Expand Down Expand Up @@ -243,8 +242,6 @@ function hygiene(some, options) {

const result = input
.pipe(filter(f => !f.stat.isDirectory()))
.pipe(filter(eolFilter))
.pipe(options.skipEOL ? es.through() : eol)
.pipe(filter(indentationFilter))
.pipe(indentation)
.pipe(filter(copyrightFilter))
Expand Down Expand Up @@ -337,17 +334,13 @@ if (require.main === module) {
process.exit(1);
});

cp.exec('git config core.autocrlf', (err, out) => {
const skipEOL = out.trim() === 'true';

if (process.argv.length > 2) {
return hygiene(process.argv.slice(2), { skipEOL: skipEOL }).on('error', err => {
console.error();
console.error(err);
process.exit(1);
});
}

if (process.argv.length > 2) {
hygiene(process.argv.slice(2)).on('error', err => {
console.error();
console.error(err);
process.exit(1);
});
} else {
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
if (err) {
console.error();
Expand All @@ -364,7 +357,7 @@ if (require.main === module) {
console.log('Reading git index versions...');

createGitIndexVinyls(some)
.then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls), { skipEOL: skipEOL })
.then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls))
.on('end', () => c())
.on('error', e)))
.catch(err => {
Expand All @@ -374,5 +367,5 @@ if (require.main === module) {
});
}
});
});
}
}

0 comments on commit 4670dbf

Please sign in to comment.