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