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');
|
2015-02-20 20:44:23 -05:00
|
|
|
var util = require('./util');
|
2014-12-05 19:26:30 -05:00
|
|
|
|
|
|
|
module.exports = function(gulp, plugins, config) {
|
|
|
|
return function() {
|
|
|
|
var tempFile = '_analyzer.dart';
|
2015-02-23 20:56:47 -05:00
|
|
|
return util.forEachSubDirSequential(
|
2015-02-20 20:44:23 -05:00
|
|
|
config.dest,
|
|
|
|
function(dir) {
|
|
|
|
var srcFiles = [].slice.call(glob.sync('{/lib,/web}/**/*.dart', {
|
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var testFiles = [].slice.call(glob.sync('test/**/*_spec.dart', {
|
|
|
|
cwd: dir
|
|
|
|
}));
|
|
|
|
var analyzeFile = ['library _analyzer;'];
|
|
|
|
srcFiles.concat(testFiles).forEach(function(fileName, index) {
|
|
|
|
if (fileName !== tempFile && fileName.indexOf("/packages/") === -1) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
);
|
2014-12-05 19:26:30 -05:00
|
|
|
|
|
|
|
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/)) {
|
2015-02-18 17:33:34 -05:00
|
|
|
if (line.match(/_analyzer\.dart/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: remove this work-around once #704 is fixed
|
|
|
|
if (line.match(/\/test\/core\/compiler\/view_.*spec\.dart/)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-05 18:22:06 -05:00
|
|
|
}
|
2014-12-05 19:26:30 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|