feat(build): auto format the generated dart code.

Fixes #480
Closes #504
This commit is contained in:
Tobias Bosch 2015-01-30 11:03:11 -08:00
parent 52d88457c8
commit b1e76c550e
2 changed files with 60 additions and 1 deletions

View File

@ -13,6 +13,7 @@ var pubbuild = require('./tools/build/pubbuild');
var dartanalyzer = require('./tools/build/dartanalyzer');
var jsserve = require('./tools/build/jsserve');
var pubserve = require('./tools/build/pubserve');
var rundartpackage = require('./tools/build/rundartpackage');
var karma = require('karma').server;
var minimist = require('minimist');
@ -166,6 +167,10 @@ var CONFIG = {
},
pubspec: {
src: 'modules/*/pubspec.yaml'
},
formatDart: {
packageName: 'dart_style',
args: ['dart_style:format', '-w', 'dist/dart']
}
};
@ -310,6 +315,15 @@ gulp.task('build/pubbuild.dart', pubbuild(gulp, gulpPlugins, {
command: DART_SDK.PUB
}));
// ------------
// format dart
gulp.task('build/format.dart', rundartpackage(gulp, gulpPlugins, {
pub: DART_SDK.PUB,
packageName: CONFIG.formatDart.packageName,
args: CONFIG.formatDart.args
}));
// ------------------
// web servers
gulp.task('serve.js.dev', jsserve(gulp, gulpPlugins, {
@ -430,7 +444,8 @@ gulp.task('build.dart', function() {
['build/deps.js.dart2js', 'build/transpile.dart', 'build/html.dart'],
'build/pubspec.dart',
'build/pubbuild.dart',
'build/analyze.dart'
'build/analyze.dart',
'build/format.dart'
);
});

View File

@ -0,0 +1,44 @@
var util = require('./util');
var Q = require('q');
var spawn = require('child_process').spawn;
var readline = require('readline');
module.exports = function(gulp, plugins, config) {
return function() {
return isInstalled().then(function(installed) {
if (!installed) {
return util.processToPromise(spawn(config.pub,
['global', 'activate', config.packageName, '--no-executables'], {
stdio: 'inherit'
}));
}
}).then(function() {
return util.processToPromise(spawn(config.pub, ['global', 'run'].concat(config.args), {
stdio: 'inherit'
}));
});
};
function isInstalled() {
var subProcess = spawn(config.pub, ['global', 'list'], {
// inherit stdin and stderr, but filter stdout
stdio: [process.stdin, 'pipe', process.stderr]
});
var rl = readline.createInterface({
input: subProcess.stdout,
output: process.stdout,
terminal: false
});
var found = false;
rl.on('line', function(line) {
if (line.indexOf(config.packageName) !== -1) {
found = true;
}
console.log(line);
});
return util.processToPromise(subProcess).then( function() {
return found;
});
}
};