OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library barback.test.transformer.aggregate_many_to_one; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:barback/barback.dart'; | |
10 import 'package:path/path.dart' as path; | |
11 | |
12 import 'mock_aggregate.dart'; | |
13 | |
14 /// An [AggregateTransformer] that applies to all assets with a given extension. | |
15 /// For each directory containing any of these assets, it produces an output | |
16 /// file that contains the concatenation of all matched assets in that | |
17 /// directory in alphabetic order by name. | |
18 class AggregateManyToOneTransformer extends MockAggregateTransformer { | |
19 /// The extension of assets to combine. | |
20 final String extension; | |
21 | |
22 /// The basename of the output asset. | |
23 /// | |
24 /// The output asset's path will contain the directory name of the inputs as | |
25 /// well. | |
26 final String output; | |
27 | |
28 AggregateManyToOneTransformer(this.extension, this.output); | |
29 | |
30 String doClassifyPrimary(AssetId id) { | |
31 if (id.extension != ".$extension") return null; | |
32 return path.url.dirname(id.path); | |
33 } | |
34 | |
35 Future doApply(AggregateTransform transform) { | |
36 return getPrimaryInputs(transform).toList().then((assets) { | |
37 assets.sort((asset1, asset2) => asset1.id.path.compareTo(asset2.id.path)); | |
38 return Future.wait(assets.map((asset) => asset.readAsString())); | |
39 }).then((contents) { | |
40 var id = new AssetId(transform.package, | |
41 path.url.join(transform.key, output)); | |
42 transform.addOutput(new Asset.fromString(id, contents.join('\n'))); | |
43 }); | |
44 } | |
45 | |
46 String toString() => "aggregate $extension->$output"; | |
47 } | |
OLD | NEW |