2014-12-05 19:26:30 -05:00
|
|
|
var Q = require('q');
|
|
|
|
var readline = require('readline');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var path = require('path');
|
|
|
|
var glob = require('glob');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
module.exports = function(gulp, plugins, config) {
|
|
|
|
return function() {
|
|
|
|
var dartModuleFolders = [].slice.call(glob.sync(config.dest + '/*'));
|
|
|
|
var tempFile = '_analyzer.dart';
|
|
|
|
// analyze in parallel!
|
|
|
|
return Q.all(dartModuleFolders.map(function(dir) {
|
2015-02-02 19:25:34 -05:00
|
|
|
var srcFiles = [].slice.call(glob.sync(dir + '{/lib,/web}/**/*.dart', {
|
2014-12-05 19:26:30 -05:00
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var testFiles = [].slice.call(glob.sync('test/**/*_spec.dart', {
|
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var analyzeFile = ['library _analyzer;'];
|
|
|
|
srcFiles.concat(testFiles).forEach(function(fileName, index) {
|
2014-12-12 19:24:53 -05:00
|
|
|
if (fileName !== tempFile && fileName.indexOf("/packages/") === -1) {
|
2014-12-05 19:26:30 -05:00
|
|
|
analyzeFile.push('import "./'+fileName+'" as mod'+index+';');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fs.writeFileSync(path.join(dir, tempFile), analyzeFile.join('\n'));
|
|
|
|
var defer = Q.defer();
|
|
|
|
analyze(dir, defer.makeNodeResolver());
|
|
|
|
return defer.promise;
|
|
|
|
}));
|
|
|
|
|
|
|
|
function analyze(dirName, done) {
|
2014-12-29 21:09:30 -05:00
|
|
|
//TODO remove --package-warnings once dartanalyzer handles transitive libraries
|
|
|
|
var stream = spawn(config.command, ['--fatal-warnings', '--package-warnings', tempFile], {
|
2014-12-05 19:26:30 -05:00
|
|
|
// inherit stdin and stderr, but filter stdout
|
|
|
|
stdio: [process.stdin, 'pipe', process.stderr],
|
|
|
|
cwd: dirName
|
|
|
|
});
|
|
|
|
// Filter out unused imports from our generated file.
|
|
|
|
// We don't reexports from the generated file
|
|
|
|
// as this could lead to name clashes when two files
|
|
|
|
// export the same thing.
|
|
|
|
var rl = readline.createInterface({
|
|
|
|
input: stream.stdout,
|
|
|
|
output: process.stdout,
|
|
|
|
terminal: false
|
|
|
|
});
|
|
|
|
var hintCount = 0;
|
2014-12-29 21:09:30 -05:00
|
|
|
var errorCount = 0;
|
2014-12-05 19:26:30 -05:00
|
|
|
rl.on('line', function(line) {
|
2014-12-29 21:09:30 -05:00
|
|
|
//TODO remove once dartanalyzer handles transitive libraries
|
|
|
|
//skip errors in third-party packages
|
|
|
|
if (line.indexOf(dirName) == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-05 19:26:30 -05:00
|
|
|
if (line.match(/Unused import/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (line.match(/\[hint\]/)) {
|
|
|
|
hintCount++;
|
2014-12-29 21:09:30 -05:00
|
|
|
} else {
|
|
|
|
errorCount ++;
|
2014-12-05 19:26:30 -05:00
|
|
|
}
|
|
|
|
console.log(dirName + ':' + line);
|
|
|
|
});
|
2014-12-29 21:09:30 -05:00
|
|
|
stream.on('close', function() {
|
2014-12-05 19:26:30 -05:00
|
|
|
var error;
|
2014-12-29 21:09:30 -05:00
|
|
|
if (errorCount > 0) {
|
|
|
|
error = new Error('Dartanalyzer showed errors');
|
2014-12-05 19:26:30 -05:00
|
|
|
}
|
|
|
|
if (hintCount > 0) {
|
|
|
|
error = new Error('Dartanalyzer showed hints');
|
|
|
|
}
|
|
|
|
done(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|