| OLD | NEW | 
|---|
| 1 // Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library pub.barback.asset_environment; | 5 library pub.barback.asset_environment; | 
| 6 | 6 | 
| 7 import 'dart:async'; | 7 import 'dart:async'; | 
| 8 import 'dart:io'; | 8 import 'dart:io'; | 
| 9 | 9 | 
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 46   /// | 46   /// | 
| 47   /// Loads all used transformers using [mode] (including dart2js if | 47   /// Loads all used transformers using [mode] (including dart2js if | 
| 48   /// [useDart2JS] is true). | 48   /// [useDart2JS] is true). | 
| 49   /// | 49   /// | 
| 50   /// This will only add the root package's "lib" directory to the environment. | 50   /// This will only add the root package's "lib" directory to the environment. | 
| 51   /// Other directories can be added to the environment using [serveDirectory]. | 51   /// Other directories can be added to the environment using [serveDirectory]. | 
| 52   /// | 52   /// | 
| 53   /// If [watcherType] is not [WatcherType.NONE] (the default), watches source | 53   /// If [watcherType] is not [WatcherType.NONE] (the default), watches source | 
| 54   /// assets for modification. | 54   /// assets for modification. | 
| 55   /// | 55   /// | 
| 56   /// If [packages] is passed, only those packages' assets will be loaded and | 56   /// If [packages] is passed, only those packages' assets are loaded and | 
| 57   /// served. | 57   /// served. | 
| 58   /// | 58   /// | 
|  | 59   /// If [entrypoints] is passed, only transformers necessary to run those | 
|  | 60   /// entrypoints are loaded. Each entrypoint is expected to refer to a Dart | 
|  | 61   /// library. | 
|  | 62   /// | 
| 59   /// Returns a [Future] that completes to the environment once the inputs, | 63   /// Returns a [Future] that completes to the environment once the inputs, | 
| 60   /// transformers, and server are loaded and ready. | 64   /// transformers, and server are loaded and ready. | 
| 61   static Future<AssetEnvironment> create(Entrypoint entrypoint, | 65   static Future<AssetEnvironment> create(Entrypoint entrypoint, | 
| 62       BarbackMode mode, {WatcherType watcherType, String hostname, int basePort, | 66       BarbackMode mode, {WatcherType watcherType, String hostname, int basePort, | 
| 63       Iterable<String> packages, bool useDart2JS: true}) { | 67       Iterable<String> packages, Iterable<AssetId> entrypoints, | 
|  | 68       bool useDart2JS: true}) { | 
| 64     if (watcherType == null) watcherType = WatcherType.NONE; | 69     if (watcherType == null) watcherType = WatcherType.NONE; | 
| 65     if (hostname == null) hostname = "localhost"; | 70     if (hostname == null) hostname = "localhost"; | 
| 66     if (basePort == null) basePort = 0; | 71     if (basePort == null) basePort = 0; | 
| 67 | 72 | 
| 68     return entrypoint.loadPackageGraph().then((graph) { | 73     return entrypoint.loadPackageGraph().then((graph) { | 
| 69       log.fine("Loaded package graph."); | 74       log.fine("Loaded package graph."); | 
| 70       graph = _adjustPackageGraph(graph, mode, packages); | 75       graph = _adjustPackageGraph(graph, mode, packages); | 
| 71       var barback = new Barback(new PubPackageProvider(graph)); | 76       var barback = new Barback(new PubPackageProvider(graph)); | 
| 72       barback.log.listen(_log); | 77       barback.log.listen(_log); | 
| 73 | 78 | 
| 74       var environment = new AssetEnvironment._(graph, barback, mode, | 79       var environment = new AssetEnvironment._(graph, barback, mode, | 
| 75           watcherType, hostname, basePort); | 80           watcherType, hostname, basePort); | 
| 76 | 81 | 
| 77       return environment._load(useDart2JS: useDart2JS) | 82       return environment._load(entrypoints: entrypoints, useDart2JS: useDart2JS) | 
| 78           .then((_) => environment); | 83           .then((_) => environment); | 
| 79     }); | 84     }); | 
| 80   } | 85   } | 
| 81 | 86 | 
| 82   /// Return a version of [graph] that's restricted to [packages] (if passed) | 87   /// Return a version of [graph] that's restricted to [packages] (if passed) | 
| 83   /// and loads cached packages (if [mode] is [BarbackMode.DEBUG]). | 88   /// and loads cached packages (if [mode] is [BarbackMode.DEBUG]). | 
| 84   static PackageGraph _adjustPackageGraph(PackageGraph graph, | 89   static PackageGraph _adjustPackageGraph(PackageGraph graph, | 
| 85       BarbackMode mode, Iterable<String> packages) { | 90       BarbackMode mode, Iterable<String> packages) { | 
| 86     if (mode != BarbackMode.DEBUG && packages == null) return graph; | 91     if (mode != BarbackMode.DEBUG && packages == null) return graph; | 
| 87     packages = (packages == null ? graph.packages.keys : packages).toSet(); | 92     packages = (packages == null ? graph.packages.keys : packages).toSet(); | 
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 438   /// Loads the assets and transformers for this environment. | 443   /// Loads the assets and transformers for this environment. | 
| 439   /// | 444   /// | 
| 440   /// This transforms and serves all library and asset files in all packages in | 445   /// This transforms and serves all library and asset files in all packages in | 
| 441   /// the environment's package graph. It loads any transformer plugins defined | 446   /// the environment's package graph. It loads any transformer plugins defined | 
| 442   /// in packages in [graph] and re-runs them as necessary when any input files | 447   /// in packages in [graph] and re-runs them as necessary when any input files | 
| 443   /// change. | 448   /// change. | 
| 444   /// | 449   /// | 
| 445   /// If [useDart2JS] is `true`, then the [Dart2JSTransformer] is implicitly | 450   /// If [useDart2JS] is `true`, then the [Dart2JSTransformer] is implicitly | 
| 446   /// added to end of the root package's transformer phases. | 451   /// added to end of the root package's transformer phases. | 
| 447   /// | 452   /// | 
|  | 453   /// If [entrypoints] is passed, only transformers necessary to run those | 
|  | 454   /// entrypoints will be loaded. | 
|  | 455   /// | 
| 448   /// Returns a [Future] that completes once all inputs and transformers are | 456   /// Returns a [Future] that completes once all inputs and transformers are | 
| 449   /// loaded. | 457   /// loaded. | 
| 450   Future _load({bool useDart2JS}) { | 458   Future _load({Iterable<AssetId> entrypoints, bool useDart2JS}) { | 
| 451     return log.progress("Initializing barback", () { | 459     return log.progress("Initializing barback", () async { | 
| 452       // If the entrypoint package manually configures the dart2js | 460       // If the entrypoint package manually configures the dart2js | 
| 453       // transformer, don't include it in the built-in transformer list. | 461       // transformer, don't include it in the built-in transformer list. | 
| 454       // | 462       // | 
| 455       // TODO(nweiz): if/when we support more built-in transformers, make | 463       // TODO(nweiz): if/when we support more built-in transformers, make | 
| 456       // this more general. | 464       // this more general. | 
| 457       var containsDart2JS = graph.entrypoint.root.pubspec.transformers | 465       var containsDart2JS = graph.entrypoint.root.pubspec.transformers | 
| 458           .any((transformers) => | 466           .any((transformers) => | 
| 459               transformers.any((config) => config.id.package == '\$dart2js')); | 467               transformers.any((config) => config.id.package == '\$dart2js')); | 
| 460 | 468 | 
| 461       if (!containsDart2JS && useDart2JS) { | 469       if (!containsDart2JS && useDart2JS) { | 
| 462         _builtInTransformers.addAll([ | 470         _builtInTransformers.addAll([ | 
| 463           new Dart2JSTransformer(this, mode), | 471           new Dart2JSTransformer(this, mode), | 
| 464           new DartForwardingTransformer(mode) | 472           new DartForwardingTransformer(mode) | 
| 465         ]); | 473         ]); | 
| 466       } | 474       } | 
| 467 | 475 | 
| 468       // Bind a server that we can use to load the transformers. | 476       // Bind a server that we can use to load the transformers. | 
| 469       var transformerServer; | 477       var transformerServer = await BarbackServer.bind(this, _hostname, 0); | 
| 470       return BarbackServer.bind(this, _hostname, 0).then((server) { |  | 
| 471         transformerServer = server; |  | 
| 472 | 478 | 
| 473         var errorStream = barback.errors.map((error) { | 479       var errorStream = barback.errors.map((error) { | 
| 474           // Even most normally non-fatal barback errors should take down pub if | 480         // Even most normally non-fatal barback errors should take down pub if | 
| 475           // they happen during the initial load process. | 481         // they happen during the initial load process. | 
| 476           if (error is! AssetLoadException) throw error; | 482         if (error is! AssetLoadException) throw error; | 
| 477 | 483 | 
| 478           log.error(log.red(error.message)); | 484         log.error(log.red(error.message)); | 
| 479           log.fine(error.stackTrace.terse); | 485         log.fine(error.stackTrace.terse); | 
| 480         }); | 486       }); | 
| 481 | 487 | 
| 482         return _withStreamErrors(() { | 488       await _withStreamErrors(() { | 
| 483           return log.progress("Loading source assets", _provideSources); | 489         return log.progress("Loading source assets", _provideSources); | 
| 484         }, [errorStream, barback.results]); | 490       }, [errorStream, barback.results]); | 
| 485       }).then((_) { |  | 
| 486         log.fine("Provided sources."); |  | 
| 487         var completer = new Completer(); |  | 
| 488 | 491 | 
| 489         var errorStream = barback.errors.map((error) { | 492       log.fine("Provided sources."); | 
| 490           // Now that we're loading transformers, errors they log shouldn't be |  | 
| 491           // fatal, since we're starting to run them on real user assets which |  | 
| 492           // may have e.g. syntax errors. If an error would cause a transformer |  | 
| 493           // to fail to load, the load failure will cause us to exit. |  | 
| 494           if (error is! TransformerException) throw error; |  | 
| 495 | 493 | 
| 496           var message = error.error.toString(); | 494       var errorStream = barback.errors.map((error) { | 
| 497           if (error.stackTrace != null) { | 495         // Now that we're loading transformers, errors they log shouldn't be | 
| 498             message += "\n" + error.stackTrace.terse.toString(); | 496         // fatal, since we're starting to run them on real user assets which | 
| 499           } | 497         // may have e.g. syntax errors. If an error would cause a transformer | 
|  | 498         // to fail to load, the load failure will cause us to exit. | 
|  | 499         if (error is! TransformerException) throw error; | 
| 500 | 500 | 
| 501           _log(new LogEntry(error.transform, error.transform.primaryId, | 501         var message = error.error.toString(); | 
| 502                   LogLevel.ERROR, message, null)); | 502         if (error.stackTrace != null) { | 
| 503         }); | 503           message += "\n" + error.stackTrace.terse.toString(); | 
|  | 504         } | 
| 504 | 505 | 
| 505         return _withStreamErrors(() { | 506         _log(new LogEntry(error.transform, error.transform.primaryId, | 
| 506           return log.progress("Loading transformers", () { | 507                 LogLevel.ERROR, message, null)); | 
| 507             return loadAllTransformers(this, transformerServer) |  | 
| 508                 .then((_) => transformerServer.close()); |  | 
| 509           }, fine: true); |  | 
| 510         }, [errorStream, barback.results, transformerServer.results]); |  | 
| 511       }); | 508       }); | 
|  | 509 | 
|  | 510       await _withStreamErrors(() async { | 
|  | 511         return log.progress("Loading transformers", () { | 
|  | 512           await loadAllTransformers(this, transformerServer, | 
|  | 513               entrypoints: entrypoints); | 
|  | 514           transformerServer.close(); | 
|  | 515         }, fine: true); | 
|  | 516       }, [errorStream, barback.results, transformerServer.results]); | 
| 512     }, fine: true); | 517     }, fine: true); | 
| 513   } | 518   } | 
| 514 | 519 | 
| 515   /// Provides the public source assets in the environment to barback. | 520   /// Provides the public source assets in the environment to barback. | 
| 516   /// | 521   /// | 
| 517   /// If [watcherType] is not [WatcherType.NONE], enables watching on them. | 522   /// If [watcherType] is not [WatcherType.NONE], enables watching on them. | 
| 518   Future _provideSources() async { | 523   Future _provideSources() async { | 
| 519     // Just include the "lib" directory from each package. We'll add the | 524     // Just include the "lib" directory from each package. We'll add the | 
| 520     // other build directories in the root package by calling | 525     // other build directories in the root package by calling | 
| 521     // [serveDirectory]. | 526     // [serveDirectory]. | 
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 783   String toString() => "polling"; | 788   String toString() => "polling"; | 
| 784 } | 789 } | 
| 785 | 790 | 
| 786 class _NoneWatcherType implements WatcherType { | 791 class _NoneWatcherType implements WatcherType { | 
| 787   const _NoneWatcherType(); | 792   const _NoneWatcherType(); | 
| 788 | 793 | 
| 789   DirectoryWatcher create(String directory) => null; | 794   DirectoryWatcher create(String directory) => null; | 
| 790 | 795 | 
| 791   String toString() => "none"; | 796   String toString() => "none"; | 
| 792 } | 797 } | 
| OLD | NEW | 
|---|