Chromium Code Reviews| 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 library barback.transformer; | 5 library barback.transformer; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset.dart'; | 9 import 'asset.dart'; |
| 10 import 'transform.dart'; | 10 import 'transform.dart'; |
| 11 import 'utils.dart'; | |
| 11 | 12 |
| 12 /// A [Transformer] represents a processor that takes in one or more input | 13 /// A [Transformer] represents a processor that takes in one or more input |
| 13 /// assets and uses them to generate one or more output assets. | 14 /// assets and uses them to generate one or more output assets. |
| 14 /// | 15 /// |
| 15 /// Dart2js, a SASS->CSS processor, a CSS spriter, and a tool to concatenate | 16 /// Dart2js, a SASS->CSS processor, a CSS spriter, and a tool to concatenate |
| 16 /// files are all examples of transformers. To define your own transformation | 17 /// files are all examples of transformers. To define your own transformation |
| 17 /// step, extend (or implement) this class. | 18 /// step, extend (or implement) this class. |
| 18 /// | 19 /// |
| 19 /// If possible, transformers should implement [DeclaringTransformer] as well to | 20 /// If possible, transformers should implement [DeclaringTransformer] as well to |
| 20 /// help barback optimize the package graph. | 21 /// help barback optimize the package graph. |
| 21 abstract class Transformer { | 22 abstract class Transformer { |
| 22 /// Override this to return a space-separated list of file extensions | 23 /// Override this to return a space-separated list of file extensions |
| 23 /// (with leading `.`) that are allowed for the primary inputs to this | 24 /// (with leading `.`) that are allowed for the primary inputs to this |
|
Bob Nystrom
2014/03/12 23:25:28
Remove the "(with leading `.`)" part.
nweiz
2014/03/12 23:30:48
Done.
| |
| 24 /// transformer. | 25 /// transformer. |
| 25 /// | 26 /// |
| 27 /// Each extension must begin with a leading `.`. | |
| 28 /// | |
| 26 /// If you don't override [isPrimary] yourself, it defaults to allowing any | 29 /// If you don't override [isPrimary] yourself, it defaults to allowing any |
| 27 /// asset whose extension matches one of the ones returned by this. If you | 30 /// asset whose extension matches one of the ones returned by this. If you |
| 28 /// don't override [isPrimary] *or* this, it allows all files. | 31 /// don't override [isPrimary] *or* this, it allows all files. |
| 29 String get allowedExtensions => null; | 32 String get allowedExtensions => null; |
| 30 | 33 |
| 34 Transformer() { | |
| 35 if (allowedExtensions == null) return; | |
| 36 | |
| 37 var invalidExtensions = allowedExtensions.split(" ") | |
| 38 .where((extension) => !extension.startsWith(".")) | |
| 39 .map((extension) => '"$extension"'); | |
| 40 if (invalidExtensions.isEmpty) return; | |
| 41 | |
| 42 throw new FormatException('Each extension in $this.allowedExtensions ' | |
| 43 'must begin with a ".", but ${toSentence(invalidExtensions)} ' | |
| 44 '${pluralize("doesn't", invalidExtensions.length, plural: "don't")}.'); | |
| 45 } | |
| 46 | |
| 31 /// Returns `true` if [input] can be a primary input for this transformer. | 47 /// Returns `true` if [input] can be a primary input for this transformer. |
| 32 /// | 48 /// |
| 33 /// While a transformer can read from multiple input files, one must be the | 49 /// While a transformer can read from multiple input files, one must be the |
| 34 /// "primary" input. This asset determines whether the transformation should | 50 /// "primary" input. This asset determines whether the transformation should |
| 35 /// be run at all. If the primary input is removed, the transformer will no | 51 /// be run at all. If the primary input is removed, the transformer will no |
| 36 /// longer be run. | 52 /// longer be run. |
| 37 /// | 53 /// |
| 38 /// A concrete example is dart2js. When you run dart2js, it will traverse | 54 /// A concrete example is dart2js. When you run dart2js, it will traverse |
| 39 /// all of the imports in your Dart source files and use the contents of all | 55 /// all of the imports in your Dart source files and use the contents of all |
| 40 /// of those to generate the final JS. However you still run dart2js "on" a | 56 /// of those to generate the final JS. However you still run dart2js "on" a |
| 41 /// single file: the entrypoint Dart file that has your `main()` method. | 57 /// single file: the entrypoint Dart file that has your `main()` method. |
| 42 /// This entrypoint file would be the primary input. | 58 /// This entrypoint file would be the primary input. |
| 43 /// | 59 /// |
| 44 /// If this is not overridden, defaults to allow any asset whose extension | 60 /// If this is not overridden, defaults to allow any asset whose extension |
| 45 /// matches one of the ones returned by [allowedExtensions]. If *that* is | 61 /// matches one of the ones returned by [allowedExtensions]. If *that* is |
| 46 /// not overridden, allows all assets. | 62 /// not overridden, allows all assets. |
| 47 Future<bool> isPrimary(Asset input) { | 63 Future<bool> isPrimary(Asset input) { |
| 48 // Allow all files if [primaryExtensions] is not overridden. | 64 // Allow all files if [primaryExtensions] is not overridden. |
| 49 if (allowedExtensions == null) return new Future.value(true); | 65 if (allowedExtensions == null) return new Future.value(true); |
| 50 | 66 |
| 51 for (var extension in allowedExtensions.split(" ")) { | 67 for (var extension in allowedExtensions.split(" ")) { |
| 52 if (input.id.extension == extension) return new Future.value(true); | 68 if (input.id.path.endsWith(extension)) return new Future.value(true); |
| 53 } | 69 } |
| 54 | 70 |
| 55 return new Future.value(false); | 71 return new Future.value(false); |
| 56 } | 72 } |
| 57 | 73 |
| 58 /// Run this transformer on on the primary input specified by [transform]. | 74 /// Run this transformer on on the primary input specified by [transform]. |
| 59 /// | 75 /// |
| 60 /// The [transform] is used by the [Transformer] for two purposes (in | 76 /// The [transform] is used by the [Transformer] for two purposes (in |
| 61 /// addition to accessing the primary input). It can call `getInput()` to | 77 /// addition to accessing the primary input). It can call `getInput()` to |
| 62 /// request additional input assets. It also calls `addOutput()` to provide | 78 /// request additional input assets. It also calls `addOutput()` to provide |
| 63 /// generated assets back to the system. Either can be called multiple times, | 79 /// generated assets back to the system. Either can be called multiple times, |
| 64 /// in any order. | 80 /// in any order. |
| 65 /// | 81 /// |
| 66 /// In other words, a Transformer's job is to find all inputs for a | 82 /// In other words, a Transformer's job is to find all inputs for a |
| 67 /// transform, starting at the primary input, then generate all output assets | 83 /// transform, starting at the primary input, then generate all output assets |
| 68 /// and yield them back to the transform. | 84 /// and yield them back to the transform. |
| 69 Future apply(Transform transform); | 85 Future apply(Transform transform); |
| 70 | 86 |
| 71 String toString() => runtimeType.toString().replaceAll("Transformer", ""); | 87 String toString() => runtimeType.toString().replaceAll("Transformer", ""); |
| 72 } | 88 } |
| OLD | NEW |