vscode/build/gulpfile.hygiene.js

421 lines
11 KiB
JavaScript
Raw Normal View History

2015-11-24 18:09:31 +00:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
2016-07-28 08:28:01 +00:00
'use strict';
2015-11-24 18:09:31 +00:00
2016-07-28 08:28:01 +00:00
const gulp = require('gulp');
const filter = require('gulp-filter');
const es = require('event-stream');
2017-06-20 14:21:44 +00:00
const gulpeslint = require('gulp-eslint');
2016-10-07 15:25:32 +00:00
const tsfmt = require('typescript-formatter');
2018-02-27 10:05:11 +00:00
const VinylFile = require('vinyl');
2017-10-10 15:53:33 +00:00
const vfs = require('vinyl-fs');
const path = require('path');
const fs = require('fs');
2018-02-27 10:05:11 +00:00
const pall = require('p-all');
2019-09-11 14:49:49 +00:00
const task = require('./lib/task');
2016-07-28 08:28:01 +00:00
2016-11-11 08:12:25 +00:00
/**
* Hygiene works by creating cascading subsets of all our files and
* passing them through a sequence of checks. Here are the current subsets,
* named according to the checks performed on them. Each subset contains
* the following one, as described in mathematical notation:
2016-11-11 08:12:25 +00:00
*
* all eol indentation copyright typescript
*/
2016-07-28 08:28:01 +00:00
const all = [
'*',
2015-11-24 18:09:31 +00:00
'build/**/*',
'extensions/**/*',
'scripts/**/*',
'src/**/*',
2018-03-05 11:38:25 +00:00
'test/**/*',
2020-02-07 14:54:04 +00:00
'!test/**/out/**',
2018-03-05 11:38:25 +00:00
'!**/node_modules/**'
];
2018-03-05 11:38:25 +00:00
const indentationFilter = [
'**',
2018-03-05 11:38:25 +00:00
// except specific files
2020-07-27 06:58:32 +00:00
'!**/ThirdPartyNotices.txt',
'!**/LICENSE.{txt,rtf}',
2019-03-19 14:37:05 +00:00
'!LICENSES.chromium.html',
2018-11-08 20:59:23 +00:00
'!**/LICENSE',
2018-03-05 11:38:25 +00:00
'!src/vs/nls.js',
2018-09-03 15:37:27 +00:00
'!src/vs/nls.build.js',
2018-03-05 11:38:25 +00:00
'!src/vs/css.js',
2018-09-03 15:37:27 +00:00
'!src/vs/css.build.js',
2018-03-05 11:38:25 +00:00
'!src/vs/loader.js',
'!src/vs/base/common/insane/insane.js',
2018-03-15 15:16:04 +00:00
'!src/vs/base/common/marked/marked.js',
2018-03-05 11:38:25 +00:00
'!src/vs/base/node/terminateProcess.sh',
'!src/vs/base/node/cpuUsage.sh',
2020-02-07 12:43:00 +00:00
'!test/unit/assert.js',
2018-03-05 11:38:25 +00:00
// except specific folders
2019-09-04 16:15:44 +00:00
'!test/automation/out/**',
2017-09-04 12:41:46 +00:00
'!test/smoke/out/**',
'!extensions/typescript-language-features/test-workspace/**',
2018-03-05 11:38:25 +00:00
'!extensions/vscode-api-tests/testWorkspace/**',
'!extensions/vscode-api-tests/testWorkspace2/**',
'!build/monaco/**',
2017-05-12 14:38:39 +00:00
'!build/win32/**',
2015-11-24 18:09:31 +00:00
2018-03-05 11:38:25 +00:00
// except multiple specific files
2015-11-24 18:09:31 +00:00
'!**/package.json',
2018-03-05 11:38:25 +00:00
'!**/yarn.lock',
2018-04-12 17:38:24 +00:00
'!**/yarn-error.log',
2018-03-05 11:38:25 +00:00
// except multiple specific folders
2019-09-17 16:05:42 +00:00
'!**/codicon/**',
2018-03-05 11:38:25 +00:00
'!**/fixtures/**',
'!**/lib/**',
'!extensions/**/out/**',
2015-11-24 18:09:31 +00:00
'!extensions/**/snippets/**',
'!extensions/**/syntaxes/**',
2016-04-11 13:54:58 +00:00
'!extensions/**/themes/**',
'!extensions/**/colorize-fixtures/**',
2018-03-05 11:38:25 +00:00
// except specific file types
'!src/vs/*/**/*.d.ts',
'!src/typings/**/*.d.ts',
'!extensions/**/*.d.ts',
2020-02-06 15:17:57 +00:00
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot,md,ps1,template,yaml,yml,d.ts.recipe,ico,icns,plist}',
'!build/{lib,download,darwin}/**/*.js',
2018-03-05 11:38:25 +00:00
'!build/**/*.sh',
2018-11-19 09:46:19 +00:00
'!build/azure-pipelines/**/*.js',
'!build/azure-pipelines/**/*.config',
'!**/Dockerfile',
2019-04-10 19:43:49 +00:00
'!**/Dockerfile.*',
2018-11-08 20:59:23 +00:00
'!**/*.Dockerfile',
'!**/*.dockerfile',
'!extensions/markdown-language-features/media/*.js'
2015-11-24 18:09:31 +00:00
];
2016-07-28 08:28:01 +00:00
const copyrightFilter = [
2015-11-24 18:09:31 +00:00
'**',
'!**/*.desktop',
2015-11-24 18:09:31 +00:00
'!**/*.json',
'!**/*.html',
'!**/*.template',
2015-11-24 18:09:31 +00:00
'!**/*.md',
'!**/*.bat',
'!**/*.cmd',
'!**/*.ico',
'!**/*.icns',
2016-06-12 15:54:03 +00:00
'!**/*.xml',
2015-11-24 18:09:31 +00:00
'!**/*.sh',
'!**/*.txt',
2016-03-24 21:01:51 +00:00
'!**/*.xpm',
'!**/*.opts',
'!**/*.disabled',
2018-01-26 15:16:36 +00:00
'!**/*.code-workspace',
2019-10-25 07:19:35 +00:00
'!**/*.js.map',
2017-07-03 09:07:57 +00:00
'!build/**/*.init',
2017-10-04 16:53:27 +00:00
'!resources/linux/snap/snapcraft.yaml',
2017-12-20 09:31:43 +00:00
'!resources/linux/snap/electron-launch',
'!resources/win32/bin/code.js',
'!resources/web/code-web.js',
'!resources/completions/**',
'!extensions/markdown-language-features/media/highlight.css',
'!extensions/html-language-features/server/src/modes/typescript/*',
2019-07-15 21:59:47 +00:00
'!extensions/*/server/bin/*',
'!src/vs/editor/test/node/classification/typescript-test.ts'
2015-11-24 18:09:31 +00:00
];
2020-01-03 08:01:41 +00:00
const jsHygieneFilter = [
2017-06-20 14:21:44 +00:00
'src/**/*.js',
'build/gulpfile.*.js',
2017-06-20 14:21:44 +00:00
'!src/vs/loader.js',
'!src/vs/css.js',
2017-06-20 14:21:44 +00:00
'!src/vs/nls.js',
'!src/vs/css.build.js',
'!src/vs/nls.build.js',
'!src/**/insane.js',
2018-03-15 15:16:04 +00:00
'!src/**/marked.js',
2017-06-20 14:21:44 +00:00
'!**/test/**'
];
2020-01-03 08:01:41 +00:00
const tsHygieneFilter = [
'src/**/*.ts',
'test/**/*.ts',
'extensions/**/*.ts',
'!**/fixtures/**',
2016-01-18 08:50:41 +00:00
'!**/typings/**',
2016-11-10 09:03:57 +00:00
'!**/node_modules/**',
'!extensions/typescript-basics/test/colorize-fixtures/**',
2016-09-15 09:17:56 +00:00
'!extensions/vscode-api-tests/testWorkspace/**',
2018-01-26 15:16:36 +00:00
'!extensions/vscode-api-tests/testWorkspace2/**',
2017-11-07 10:53:59 +00:00
'!extensions/**/*.test.ts',
2018-03-17 15:32:55 +00:00
'!extensions/html-language-features/server/lib/jquery.d.ts'
2016-01-18 08:50:41 +00:00
];
2018-03-05 11:38:25 +00:00
const copyrightHeaderLines = [
2015-11-24 18:09:31 +00:00
'/*---------------------------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Licensed under the MIT License. See License.txt in the project root for license information.',
' *--------------------------------------------------------------------------------------------*/'
2018-03-05 11:38:25 +00:00
];
2015-11-24 18:09:31 +00:00
2017-06-20 14:21:44 +00:00
gulp.task('eslint', () => {
2017-10-31 15:11:10 +00:00
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
2020-01-03 08:01:41 +00:00
.pipe(filter(jsHygieneFilter.concat(tsHygieneFilter)))
2019-12-30 09:29:28 +00:00
.pipe(gulpeslint({
configFile: '.eslintrc.json',
rulePaths: ['./build/lib/eslint']
}))
2017-06-20 14:21:44 +00:00
.pipe(gulpeslint.formatEach('compact'))
2019-12-31 10:59:27 +00:00
.pipe(gulpeslint.results(results => {
if (results.warningCount > 0 || results.errorCount > 0) {
throw new Error('eslint failed with warnings and/or errors');
}
}));
2017-06-20 14:21:44 +00:00
});
2016-02-18 15:02:16 +00:00
2019-09-11 14:49:49 +00:00
function checkPackageJSON(actualPath) {
const actual = require(path.join(__dirname, '..', actualPath));
const rootPackageJSON = require('../package.json');
for (let depName in actual.dependencies) {
const depVersion = actual.dependencies[depName];
const rootDepVersion = rootPackageJSON.dependencies[depName];
if (!rootDepVersion) {
// missing in root is allowed
continue;
}
if (depVersion !== rootDepVersion) {
this.emit('error', `The dependency ${depName} in '${actualPath}' (${depVersion}) is different than in the root package.json (${rootDepVersion})`);
}
}
}
const checkPackageJSONTask = task.define('check-package-json', () => {
return gulp.src('package.json')
.pipe(es.through(function () {
2019-09-11 14:49:49 +00:00
checkPackageJSON.call(this, 'remote/package.json');
checkPackageJSON.call(this, 'remote/web/package.json');
}));
});
gulp.task(checkPackageJSONTask);
2018-03-05 11:38:25 +00:00
function hygiene(some) {
2016-07-28 08:28:01 +00:00
let errorCount = 0;
2015-11-24 18:09:31 +00:00
const productJson = es.through(function (file) {
const product = JSON.parse(file.contents.toString('utf8'));
if (product.extensionsGallery) {
console.error('product.json: Contains "extensionsGallery"');
errorCount++;
}
this.emit('data', file);
});
2016-07-28 08:28:01 +00:00
const indentation = es.through(function (file) {
2018-03-05 11:38:25 +00:00
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
file.__lines = lines;
lines
2016-07-28 08:28:01 +00:00
.forEach((line, i) => {
2015-11-25 08:47:15 +00:00
if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK
2015-11-24 18:09:31 +00:00
} else if (/^[\t]*[^\s]/.test(line)) {
// good indent
} else if (/^[\t]* \*/.test(line)) {
// block comment using an extra space
} else {
console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
2015-11-24 18:09:31 +00:00
errorCount++;
}
});
this.emit('data', file);
});
2016-07-28 08:28:01 +00:00
const copyrights = es.through(function (file) {
2018-03-05 11:38:25 +00:00
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;
}
2015-11-24 18:09:31 +00:00
}
2015-11-25 08:47:15 +00:00
2015-11-25 08:29:48 +00:00
this.emit('data', file);
2015-11-24 18:09:31 +00:00
});
2016-10-07 15:39:26 +00:00
const formatting = es.map(function (file, cb) {
2016-10-07 15:25:32 +00:00
tsfmt.processString(file.path, file.contents.toString('utf8'), {
2018-03-06 15:33:20 +00:00
verify: false,
2016-10-07 15:25:32 +00:00
tsfmt: true,
2018-04-22 18:12:03 +00:00
// verbose: true,
2018-02-01 13:39:12 +00:00
// keep checkJS happy
editorconfig: undefined,
replace: undefined,
tsconfig: undefined,
2018-04-22 18:12:03 +00:00
tsconfigFile: undefined,
tsfmtFile: undefined,
vscode: undefined,
vscodeFile: undefined
2016-10-07 15:25:32 +00:00
}).then(result => {
2018-03-06 21:24:47 +00:00
let original = result.src.replace(/\r\n/gm, '\n');
let formatted = result.dest.replace(/\r\n/gm, '\n');
2018-03-06 15:33:20 +00:00
if (original !== formatted) {
2019-12-31 10:59:27 +00:00
console.error('File not formatted. Run the \'Format Document\' command to fix it:', file.relative);
2016-10-07 15:25:32 +00:00
errorCount++;
}
2016-10-07 15:39:26 +00:00
cb(null, file);
2016-10-07 15:25:32 +00:00
}, err => {
2016-10-07 15:39:26 +00:00
cb(err);
2016-10-07 15:25:32 +00:00
});
});
2018-02-27 10:05:11 +00:00
let input;
if (Array.isArray(some) || typeof some === 'string' || !some) {
const options = { base: '.', follow: true, allowEmpty: true };
if (some) {
2019-11-28 20:16:27 +00:00
input = vfs.src(some, options).pipe(filter(all)); // split this up to not unnecessarily filter all a second time
} else {
input = vfs.src(all, options);
}
2018-02-27 10:05:11 +00:00
} else {
input = some;
}
const productJsonFilter = filter('product.json', { restore: true });
2018-02-27 10:05:11 +00:00
const result = input
2016-07-28 08:28:01 +00:00
.pipe(filter(f => !f.stat.isDirectory()))
.pipe(productJsonFilter)
2019-06-26 20:57:28 +00:00
.pipe(process.env['BUILD_SOURCEVERSION'] ? es.through() : productJson)
.pipe(productJsonFilter.restore)
2015-11-24 18:09:31 +00:00
.pipe(filter(indentationFilter))
.pipe(indentation)
2016-01-18 08:50:41 +00:00
.pipe(filter(copyrightFilter))
2017-06-20 14:21:44 +00:00
.pipe(copyrights);
2020-01-02 19:10:05 +00:00
const typescript = result
2020-01-03 08:01:41 +00:00
.pipe(filter(tsHygieneFilter))
.pipe(formatting);
2017-06-20 14:21:44 +00:00
const javascript = result
2020-01-03 08:01:41 +00:00
.pipe(filter(jsHygieneFilter.concat(tsHygieneFilter)))
.pipe(gulpeslint({
configFile: '.eslintrc.json',
rulePaths: ['./build/lib/eslint']
}))
2017-06-20 14:21:44 +00:00
.pipe(gulpeslint.formatEach('compact'))
2019-12-31 10:59:27 +00:00
.pipe(gulpeslint.results(results => {
errorCount += results.warningCount;
errorCount += results.errorCount;
}));
2017-06-20 14:21:44 +00:00
let count = 0;
2017-06-20 14:21:44 +00:00
return es.merge(typescript, javascript)
.pipe(es.through(function (data) {
count++;
2017-11-13 10:32:38 +00:00
if (process.env['TRAVIS'] && count % 10 === 0) {
process.stdout.write('.');
}
this.emit('data', data);
}, function () {
process.stdout.write('\n');
2015-11-24 18:09:31 +00:00
if (errorCount > 0) {
2015-11-26 08:57:29 +00:00
this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
2015-11-24 18:09:31 +00:00
} else {
this.emit('end');
}
}));
2018-02-27 10:05:11 +00:00
}
2018-02-27 10:05:11 +00:00
function createGitIndexVinyls(paths) {
const cp = require('child_process');
const repositoryPath = process.cwd();
const fns = paths.map(relativePath => () => new Promise((c, e) => {
const fullPath = path.join(repositoryPath, relativePath);
fs.stat(fullPath, (err, stat) => {
2018-02-27 10:13:47 +00:00
if (err && err.code === 'ENOENT') { // ignore deletions
return c(null);
} else if (err) {
2018-02-27 10:05:11 +00:00
return e(err);
}
cp.exec(`git show :${relativePath}`, { maxBuffer: 2000 * 1024, encoding: 'buffer' }, (err, out) => {
if (err) {
return e(err);
}
c(new VinylFile({
path: fullPath,
base: repositoryPath,
contents: out,
stat
}));
});
});
}));
2018-02-27 10:13:47 +00:00
return pall(fns, { concurrency: 4 })
.then(r => r.filter(p => !!p));
2018-02-27 10:05:11 +00:00
}
2019-09-11 14:49:49 +00:00
gulp.task('hygiene', task.series(checkPackageJSONTask, () => hygiene()));
2016-07-28 08:28:01 +00:00
// this allows us to run hygiene as a git pre-commit hook
if (require.main === module) {
2016-07-28 08:28:01 +00:00
const cp = require('child_process');
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
process.exit(1);
});
2018-03-05 11:38:25 +00:00
if (process.argv.length > 2) {
hygiene(process.argv.slice(2)).on('error', err => {
console.error();
console.error(err);
process.exit(1);
});
} else {
2016-07-28 08:28:01 +00:00
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
2016-03-04 09:44:20 +00:00
if (err) {
console.error();
console.error(err);
process.exit(1);
2018-02-27 10:05:11 +00:00
return;
2016-03-04 09:44:20 +00:00
}
2016-07-28 08:28:01 +00:00
const some = out
2016-03-04 09:44:20 +00:00
.split(/\r?\n/)
2016-07-28 08:28:01 +00:00
.filter(l => !!l);
2017-11-06 14:39:46 +00:00
if (some.length > 0) {
2018-02-27 10:05:11 +00:00
console.log('Reading git index versions...');
createGitIndexVinyls(some)
.then(vinyls => new Promise((c, e) => hygiene(es.readArray(vinyls))
2018-02-27 10:05:11 +00:00
.on('end', () => c())
.on('error', e)))
.catch(err => {
console.error();
console.error(err);
process.exit(1);
});
2017-11-06 14:39:46 +00:00
}
});
2018-03-05 11:38:25 +00:00
}
}