| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Definitions used to run the polymer linter and deploy tools without using | 6 * Definitions used to run the polymer linter and deploy tools without using |
| 7 * pub serve or pub deploy. | 7 * pub serve or pub deploy. |
| 8 */ | 8 */ |
| 9 library polymer.src.build.runner; | 9 library polymer.src.build.runner; |
| 10 | 10 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 /** Whether to run transformers on the test folder. */ | 36 /** Whether to run transformers on the test folder. */ |
| 37 final bool transformTests; | 37 final bool transformTests; |
| 38 | 38 |
| 39 /** Whether to apply transformers on polymer dependencies. */ | 39 /** Whether to apply transformers on polymer dependencies. */ |
| 40 final bool transformPolymerDependencies; | 40 final bool transformPolymerDependencies; |
| 41 | 41 |
| 42 /** Directory where to generate code, if any. */ | 42 /** Directory where to generate code, if any. */ |
| 43 final String outDir; | 43 final String outDir; |
| 44 | 44 |
| 45 /** |
| 46 * Whether to print error messages using a json-format that tools, such as the |
| 47 * Dart Editor, can process. |
| 48 */ |
| 49 final String machineFormat; |
| 50 |
| 45 BarbackOptions(this.phases, this.outDir, {currentPackage, packageDirs, | 51 BarbackOptions(this.phases, this.outDir, {currentPackage, packageDirs, |
| 46 this.transformTests: false, this.transformPolymerDependencies: false}) | 52 this.transformTests: false, this.transformPolymerDependencies: false, |
| 53 this.machineFormat: false}) |
| 47 : currentPackage = (currentPackage != null | 54 : currentPackage = (currentPackage != null |
| 48 ? currentPackage : readCurrentPackageFromPubspec()), | 55 ? currentPackage : readCurrentPackageFromPubspec()), |
| 49 packageDirs = (packageDirs != null | 56 packageDirs = (packageDirs != null |
| 50 ? packageDirs : _readPackageDirsFromPub(currentPackage)); | 57 ? packageDirs : _readPackageDirsFromPub(currentPackage)); |
| 51 | 58 |
| 52 } | 59 } |
| 53 | 60 |
| 54 /** | 61 /** |
| 55 * Creates a barback system as specified by [options] and runs it. Returns a | 62 * Creates a barback system as specified by [options] and runs it. Returns a |
| 56 * future that contains the list of assets generated after barback runs to | 63 * future that contains the list of assets generated after barback runs to |
| 57 * completion. | 64 * completion. |
| 58 */ | 65 */ |
| 59 Future<AssetSet> runBarback(BarbackOptions options) { | 66 Future<AssetSet> runBarback(BarbackOptions options) { |
| 60 var barback = new Barback(new _PolymerPackageProvider(options.packageDirs)); | 67 var barback = new Barback(new _PolymerPackageProvider(options.packageDirs)); |
| 61 _initBarback(barback, options); | 68 _initBarback(barback, options); |
| 62 _attachListeners(barback); | 69 _attachListeners(barback, options); |
| 63 if (options.outDir == null) return barback.getAllAssets(); | 70 if (options.outDir == null) return barback.getAllAssets(); |
| 64 return _emitAllFiles(barback, options); | 71 return _emitAllFiles(barback, options); |
| 65 } | 72 } |
| 66 | 73 |
| 67 /** Extract the current package from the pubspec.yaml file. */ | 74 /** Extract the current package from the pubspec.yaml file. */ |
| 68 String readCurrentPackageFromPubspec([String dir]) { | 75 String readCurrentPackageFromPubspec([String dir]) { |
| 69 var pubspec = new File( | 76 var pubspec = new File( |
| 70 dir == null ? 'pubspec.yaml' : path.join(dir, 'pubspec.yaml')); | 77 dir == null ? 'pubspec.yaml' : path.join(dir, 'pubspec.yaml')); |
| 71 if (!pubspec.existsSync()) { | 78 if (!pubspec.existsSync()) { |
| 72 print('error: pubspec.yaml file not found, please run this script from ' | 79 print('error: pubspec.yaml file not found, please run this script from ' |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 171 } |
| 165 | 172 |
| 166 // In case of the current package, include also 'web'. | 173 // In case of the current package, include also 'web'. |
| 167 addAssets(options.currentPackage, 'web'); | 174 addAssets(options.currentPackage, 'web'); |
| 168 if (options.transformTests) addAssets(options.currentPackage, 'test'); | 175 if (options.transformTests) addAssets(options.currentPackage, 'test'); |
| 169 | 176 |
| 170 barback.updateSources(assets); | 177 barback.updateSources(assets); |
| 171 } | 178 } |
| 172 | 179 |
| 173 /** Attach error listeners on [barback] so we can report errors. */ | 180 /** Attach error listeners on [barback] so we can report errors. */ |
| 174 void _attachListeners(Barback barback) { | 181 void _attachListeners(Barback barback, BarbackOptions options) { |
| 175 // Listen for errors and results | 182 // Listen for errors and results |
| 176 barback.errors.listen((e) { | 183 barback.errors.listen((e) { |
| 177 var trace = null; | 184 var trace = null; |
| 178 if (e is Error) trace = e.stackTrace; | 185 if (e is Error) trace = e.stackTrace; |
| 179 if (trace != null) { | 186 if (trace != null) { |
| 180 print(Trace.format(trace)); | 187 print(Trace.format(trace)); |
| 181 } | 188 } |
| 182 print('error running barback: $e'); | 189 print('error running barback: $e'); |
| 183 exit(1); | 190 exit(1); |
| 184 }); | 191 }); |
| 185 | 192 |
| 186 barback.results.listen((result) { | 193 barback.results.listen((result) { |
| 187 if (!result.succeeded) { | 194 if (!result.succeeded) { |
| 188 print("build failed with errors: ${result.errors}"); | 195 print("build failed with errors: ${result.errors}"); |
| 189 exit(1); | 196 exit(1); |
| 190 } | 197 } |
| 191 }); | 198 }); |
| 199 |
| 200 barback.log.listen((entry) { |
| 201 if (options.machineFormat) { |
| 202 print(_jsonFormatter(entry)); |
| 203 } else { |
| 204 print(_consoleFormatter(entry)); |
| 205 } |
| 206 }); |
| 192 } | 207 } |
| 193 | 208 |
| 194 /** | 209 /** |
| 195 * Emits all outputs of [barback] and copies files that we didn't process (like | 210 * Emits all outputs of [barback] and copies files that we didn't process (like |
| 196 * polymer's libraries). | 211 * polymer's libraries). |
| 197 */ | 212 */ |
| 198 Future _emitAllFiles(Barback barback, BarbackOptions options) { | 213 Future _emitAllFiles(Barback barback, BarbackOptions options) { |
| 199 return barback.getAllAssets().then((assets) { | 214 return barback.getAllAssets().then((assets) { |
| 200 // Delete existing output folder before we generate anything | 215 // Delete existing output folder before we generate anything |
| 201 var dir = new Directory(options.outDir); | 216 var dir = new Directory(options.outDir); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 Future _copyFile(String inpath, String outpath) { | 319 Future _copyFile(String inpath, String outpath) { |
| 305 _ensureDir(path.dirname(outpath)); | 320 _ensureDir(path.dirname(outpath)); |
| 306 return new File(inpath).openRead().pipe(new File(outpath).openWrite()); | 321 return new File(inpath).openRead().pipe(new File(outpath).openWrite()); |
| 307 } | 322 } |
| 308 | 323 |
| 309 /** Write contents of an [asset] into a file at [filepath]. */ | 324 /** Write contents of an [asset] into a file at [filepath]. */ |
| 310 Future _writeAsset(String filepath, Asset asset) { | 325 Future _writeAsset(String filepath, Asset asset) { |
| 311 _ensureDir(path.dirname(filepath)); | 326 _ensureDir(path.dirname(filepath)); |
| 312 return asset.read().pipe(new File(filepath).openWrite()); | 327 return asset.read().pipe(new File(filepath).openWrite()); |
| 313 } | 328 } |
| 329 |
| 330 String _kindFromEntry(LogEntry entry) { |
| 331 var level = entry.level; |
| 332 return level == LogLevel.ERROR ? 'error' |
| 333 : (level == LogLevel.WARNING ? 'warning' : 'info'); |
| 334 } |
| 335 |
| 336 /** |
| 337 * Formatter that generates messages using a format that can be parsed |
| 338 * by tools, such as the Dart Editor, for reporting error messages. |
| 339 */ |
| 340 String _jsonFormatter(LogEntry entry) { |
| 341 var kind = _kindFromEntry(entry); |
| 342 var span = entry.span; |
| 343 return JSON.encode((span == null) |
| 344 ? [{'method': kind, 'params': {'message': entry.message}}] |
| 345 : [{'method': kind, |
| 346 'params': { |
| 347 'file': span.sourceUrl, |
| 348 'message': entry.message, |
| 349 'line': span.start.line + 1, |
| 350 'charStart': span.start.offset, |
| 351 'charEnd': span.end.offset, |
| 352 }}]); |
| 353 } |
| 354 |
| 355 /** |
| 356 * Formatter that generates messages that are easy to read on the console (used |
| 357 * by default). |
| 358 */ |
| 359 String _consoleFormatter(LogEntry entry) { |
| 360 var kind = _kindFromEntry(entry); |
| 361 var useColors = stdioType(stdout) == StdioType.TERMINAL; |
| 362 var levelColor = (kind == 'error') ? _RED_COLOR : _MAGENTA_COLOR; |
| 363 var output = new StringBuffer(); |
| 364 if (useColors) output.write(levelColor); |
| 365 output..write(kind)..write(' '); |
| 366 if (useColors) output.write(_NO_COLOR); |
| 367 if (entry.span == null) { |
| 368 output.write(entry.message); |
| 369 } else { |
| 370 output.write(entry.span.getLocationMessage(entry.message, |
| 371 useColors: useColors, |
| 372 color: levelColor)); |
| 373 } |
| 374 return output.toString(); |
| 375 } |
| 376 |
| 377 const String _RED_COLOR = '\u001b[31m'; |
| 378 const String _MAGENTA_COLOR = '\u001b[35m'; |
| 379 const String _NO_COLOR = '\u001b[0m'; |
| OLD | NEW |