feat(build): enforce formatting of some files.

Our style guide includes formatting conventions. Instead of wasting time in reviewing PRs discussing things like indenting, and to avoid later deltas to fix bad formatting in earlier commits, we want to enforce these in the build.
The intent in this change is to fail the build as quickly as possible in travis, so those sending a PR immediately know they should run clang-format and update their commit. When running locally, we want users to know about formatting, but they may not want to act on it immediately, until they are done working. For this reason, it is only a warning outside of the continuous build.
This is done by having a check-format task which should run on most local builds, and an enforce-format task only run by travis.
This commit is contained in:
Alex Eagle 2015-04-11 13:29:32 -07:00
parent a3decad4c2
commit daf0f472b3
8 changed files with 46 additions and 34 deletions

View File

@ -6,9 +6,9 @@ var path = require('path');
// Transpile everything in 'modules'... // Transpile everything in 'modules'...
var modulesTree = new Funnel('modules', { var modulesTree = new Funnel('modules', {
include: ['**/*.js', '**/*.ts', '**/*.dart'], // .dart file available means don't translate. include: ['**/*.js', '**/*.ts', '**/*.dart'], // .dart file available means don't translate.
exclude: ['rtts_assert/**/*'], // ... except for the rtts_asserts (don't apply to Dart). exclude: ['rtts_assert/**/*'], // ... except for the rtts_asserts (don't apply to Dart).
destDir: '/', // Remove the 'modules' prefix. destDir: '/', // Remove the 'modules' prefix.
}); });
// Transpile to dart. // Transpile to dart.

View File

@ -504,9 +504,22 @@ gulp.task('build/format.dart', rundartpackage(gulp, gulpPlugins, {
args: CONFIG.formatDart.args args: CONFIG.formatDart.args
})); }));
function doCheckFormat() {
return gulp.src(['Brocfile*.js', 'modules/**/*.ts', 'tools/**/*.ts', '!**/typings/**/*.d.ts'])
.pipe(format.checkFormat('file'));
}
gulp.task('check-format', function() { gulp.task('check-format', function() {
return gulp.src(['Brocfile*.js', 'modules/**/*.ts', '!**/typings/**/*.d.ts']) return doCheckFormat().on('warning', function(e) {
.pipe(format.checkFormat('file')); console.log("NOTE: this will be promoted to an ERROR in the continuous build");
});
});
gulp.task('enforce-format', function() {
return doCheckFormat().on('warning', function(e) {
console.log("ERROR: Some files need formatting");
process.exit(1);
});
}); });
// ------------ // ------------
@ -752,6 +765,7 @@ gulp.task('build.js.dev', function(done) {
runSequence( runSequence(
'broccoli.js.dev', 'broccoli.js.dev',
'build/checkCircularDependencies', 'build/checkCircularDependencies',
'check-format',
done done
); );
}); });

View File

@ -8,4 +8,4 @@ SCRIPT_DIR=$(dirname $0)
source $SCRIPT_DIR/env_dart.sh source $SCRIPT_DIR/env_dart.sh
cd $SCRIPT_DIR/../.. cd $SCRIPT_DIR/../..
./node_modules/.bin/gulp build.js docs ./node_modules/.bin/gulp enforce-format build.js docs

View File

@ -9,7 +9,7 @@ import path = require('path');
import ts2dart = require('ts2dart'); import ts2dart = require('ts2dart');
type Set = { type Set = {
[s:string]: boolean [s: string]: boolean
}; };
class TypeScriptToDartTranspiler extends Writer { class TypeScriptToDartTranspiler extends Writer {

View File

@ -1,8 +1,6 @@
/// <reference path="../typings/es6-promise/es6-promise.d.ts" /> /// <reference path="../typings/es6-promise/es6-promise.d.ts" />
declare class Writer { declare class Writer { write(readTree: (tree) => Promise<string>, destDir: string): Promise<any>; }
write(readTree: (tree) => Promise<string>, destDir: string): Promise<any>;
}
export = Writer; export = Writer;

View File

@ -37,7 +37,7 @@ class TraceurFilter extends Writer {
// TODO: we should fix the sourceMappingURL written by Traceur instead of overriding // TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
// (but we might switch to typescript first) // (but we might switch to typescript first)
var mapFilepath = filepath.replace(/\.\w+$/, '') + this.destSourceMapExtension; var mapFilepath = filepath.replace(/\.\w+$/, '') + this.destSourceMapExtension;
result.js = result.js + `\n//# sourceMappingURL=./${path.basename(mapFilepath)}`; result.js = result.js + '\n //# sourceMappingURL=./' + path.basename(mapFilepath);
var destFilepath = filepath.replace(/\.\w+$/, this.destExtension); var destFilepath = filepath.replace(/\.\w+$/, this.destExtension);
var destFile = path.join(destDir, destFilepath); var destFile = path.join(destDir, destFilepath);

View File

@ -8,7 +8,7 @@ export interface TranspilerOptions {
basePath?: string; basePath?: string;
} }
export class Transpiler{ export class Transpiler {
constructor(options: TranspilerOptions); constructor(options: TranspilerOptions);
transpile(fileNames: string[], outdir?: string); transpile(fileNames: string[], outdir?: string);
} }

View File

@ -22,34 +22,34 @@ class TSCompiler extends Writer {
} }
options.target = (<any>ts).ScriptTarget[options.target]; options.target = (<any>ts).ScriptTarget[options.target];
return readTree(this.inputTree) return readTree(this.inputTree)
.then(srcDir => { .then(srcDir => {
var files = walkSync(srcDir) var files = walkSync(srcDir)
.filter(filepath => path.extname(filepath).toLowerCase() === '.ts') .filter(filepath => path.extname(filepath).toLowerCase() === '.ts')
.map(filepath => path.resolve(srcDir, filepath)); .map(filepath => path.resolve(srcDir, filepath));
if (files.length > 0) { if (files.length > 0) {
var program = ts.createProgram(files, options); var program = ts.createProgram(files, options);
var emitResult = program.emit(); var emitResult = program.emit();
var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
var errMsg = ''; var errMsg = '';
allDiagnostics.forEach(diagnostic => { allDiagnostics.forEach(diagnostic => {
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if (!diagnostic.file) { if (!diagnostic.file) {
errMsg += `\n${message}`; errMsg += `\n${message}`;
return; return;
}
var {line, character} =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
errMsg += `\n${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
});
if (emitResult.emitSkipped) {
throw new Error(errMsg);
} }
var {line, character} =
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
errMsg += `\n${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
});
if (emitResult.emitSkipped) {
throw new Error(errMsg);
} }
} });
});
} }
} }
module.exports = TSCompiler; module.exports = TSCompiler;