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 /// Builds an index.html file in each folder containing entry points, if none | |
6 /// already exists. This file simply lists all the entry point files. | |
7 library polymer.src.build.index_page_builder; | |
8 | |
9 import 'dart:async'; | |
10 import 'dart:math'; | |
11 | |
12 import 'package:barback/barback.dart'; | |
13 import 'package:html5lib/dom.dart'; | |
Siggi Cherem (dart-lang)
2014/09/17 20:32:54
you might not need this import anymore
jakemac
2014/09/17 22:14:01
Done.
| |
14 import 'package:path/path.dart' as path; | |
15 | |
16 import 'common.dart'; | |
17 | |
18 /// Builds an index.html file in each folder containing entry points, if none | |
19 /// already exists. This file simply lists all the entry point files. | |
20 class IndexPageBuilder extends AggregateTransformer { | |
21 final TransformOptions options; | |
22 | |
23 IndexPageBuilder(this.options); | |
24 | |
25 classifyPrimary(AssetId id) { | |
26 if (!options.isHtmlEntryPoint(id)) return null; | |
27 // Group all entry points together. | |
28 return 'all_entry_points'; | |
29 } | |
30 | |
31 Future apply(AggregateTransform transform) { | |
32 Map<String, List<String>> dirFilesMap = {}; | |
33 | |
34 return transform.primaryInputs.toList().then((assets) { | |
35 // Add the asset to its directory, and make sure its directory is included | |
36 // in all its parents. | |
37 for (var asset in assets) { | |
38 var dir = path.url.dirname(asset.id.path); | |
39 while (dir != '.') { | |
40 dirFilesMap.putIfAbsent(dir, () => []); | |
41 | |
42 var relativePath = path.url.relative(asset.id.path, from: dir); | |
43 var relativeDir = path.url.dirname(relativePath); | |
44 dirFilesMap[dir].add(relativePath); | |
45 dir = path.url.dirname(dir); | |
46 } | |
47 } | |
48 | |
49 // Create an output index.html file for each directory, if one doesn't | |
50 // exist already | |
51 var futures = []; | |
52 dirFilesMap.forEach((directory, files) { | |
53 futures.add(_createOutput(directory, files, transform)); | |
54 }); | |
55 return Future.wait(futures); | |
56 }); | |
57 } | |
58 | |
59 Future _createOutput( | |
60 String directory, List<String> files, AggregateTransform transform) { | |
61 var indexAsset = new AssetId( | |
62 transform.package, path.join(directory, 'index.html')); | |
63 | |
64 return transform.hasInput(indexAsset).then((exists) { | |
65 // Don't overwrite existing outputs! | |
66 if (exists) return; | |
67 | |
68 // Sort alphabetically by recursive path parts. | |
69 files.sort((String a, String b) { | |
70 var aParts = path.split(a); | |
71 var bParts = path.split(b); | |
72 int diff = 0; | |
73 int minLength = min(aParts.length, bParts.length); | |
74 for (int i = 0; i < minLength; i++) { | |
Siggi Cherem (dart-lang)
2014/09/17 20:32:54
now that there is no directory entries, but just p
jakemac
2014/09/17 22:14:01
Talked in person, its more complicated than this t
| |
75 // Directories are sorted below files. | |
76 var aIsDir = path.url.extension(aParts[i]) == ''; | |
77 var bIsDir = path.url.extension(bParts[i]) == ''; | |
78 if (aIsDir && !bIsDir) { | |
79 return 1; | |
80 } else if (!aIsDir && bIsDir) { | |
81 return -1; | |
82 } | |
83 | |
84 // Raw string comparison, if not identical we return. | |
85 diff = aParts[i].compareTo(bParts[i]); | |
86 if (diff != 0) return diff; | |
87 } | |
88 // Identical files, shouldn't happen in practice. | |
89 return 0; | |
90 }); | |
91 | |
92 // Create the document with a list. | |
93 var doc = new StringBuffer( | |
94 '<!DOCTYPE html><html><body><h1>Entry points</h1><ul>'); | |
95 | |
96 // Add all the assets to the list. | |
97 for (var file in files) { | |
98 doc.write('<li><a href="$file">$file</a></li>'); | |
99 }; | |
100 | |
101 doc.write('</ul></body></html>'); | |
102 | |
103 // Output the index.html file | |
104 transform.addOutput(new Asset.fromString(indexAsset , doc.toString())); | |
105 }); | |
106 } | |
107 | |
108 } | |
OLD | NEW |