| 
									
										
										
										
											2014-12-05 16:26:30 -08: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 16:25:34 -08:00
										 |  |  |       var srcFiles = [].slice.call(glob.sync(dir + '{/lib,/web}/**/*.dart', { | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08: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 16:24:53 -08:00
										 |  |  |         if (fileName !== tempFile && fileName.indexOf("/packages/") === -1) { | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08: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 18:09:30 -08:00
										 |  |  |       //TODO remove --package-warnings once dartanalyzer handles transitive libraries
 | 
					
						
							|  |  |  |       var stream = spawn(config.command, ['--fatal-warnings', '--package-warnings', tempFile], { | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08: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 18:09:30 -08:00
										 |  |  |       var errorCount = 0; | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |       rl.on('line', function(line) { | 
					
						
							| 
									
										
										
										
											2014-12-29 18:09:30 -08:00
										 |  |  |         //TODO remove once dartanalyzer handles transitive libraries
 | 
					
						
							|  |  |  |         //skip errors in third-party packages
 | 
					
						
							|  |  |  |         if (line.indexOf(dirName) == -1) { | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |         if (line.match(/Unused import/)) { | 
					
						
							| 
									
										
										
										
											2015-02-18 14:33:34 -08: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 15:22:06 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |         if (line.match(/\[hint\]/)) { | 
					
						
							|  |  |  |           hintCount++; | 
					
						
							| 
									
										
										
										
											2014-12-29 18:09:30 -08:00
										 |  |  |         } else { | 
					
						
							|  |  |  |           errorCount ++; | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |         console.log(dirName + ':' + line); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-12-29 18:09:30 -08:00
										 |  |  |       stream.on('close', function() { | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |         var error; | 
					
						
							| 
									
										
										
										
											2014-12-29 18:09:30 -08:00
										 |  |  |         if (errorCount > 0) { | 
					
						
							|  |  |  |           error = new Error('Dartanalyzer showed errors'); | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (hintCount > 0) { | 
					
						
							|  |  |  |           error = new Error('Dartanalyzer showed hints'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         done(error); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |