Chromium Code Reviews| 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 | |
| 11 import 'package:barback/barback.dart'; | |
| 12 import 'package:html5lib/dom.dart'; | |
| 13 import 'package:path/path.dart' as path; | |
| 14 | |
| 15 import 'common.dart'; | |
| 16 | |
| 17 /// Builds an index.html file in each folder containing entry points, if none | |
| 18 /// already exists. This file simply lists all the entry point files. | |
| 19 class IndexPageBuilder extends AggregateTransformer { | |
| 20 final TransformOptions options; | |
| 21 | |
| 22 IndexPageBuilder(this.options); | |
| 23 | |
| 24 classifyPrimary(AssetId id) { | |
| 25 if (!options.isHtmlEntryPoint(id)) return null; | |
| 26 // Group all entry points together. | |
| 27 return ''; | |
|
Siggi Cherem (dart-lang)
2014/09/16 19:40:50
nit: it might feel less magical to use a non-empty
jakemac
2014/09/16 21:17:05
Changed to 'all_entry_points'
On 2014/09/16 19:40
| |
| 28 } | |
| 29 | |
| 30 Future apply(AggregateTransform transform) { | |
| 31 Map<String, List<String>> dirFilesMap = {}; | |
| 32 | |
| 33 return transform.primaryInputs.toList().then((assets) { | |
| 34 // Add the asset to its directory, and make sure its directory is included | |
| 35 // in all its parents. | |
| 36 for (var asset in assets) { | |
| 37 var dir = path.url.dirname(asset.id.path); | |
| 38 while (dir != '.') { | |
| 39 dirFilesMap.putIfAbsent(dir, () => []); | |
| 40 | |
| 41 var relativePath = path.url.relative(asset.id.path, from: dir); | |
| 42 var relativeDir = path.url.dirname(relativePath); | |
| 43 if (relativeDir == '.') { | |
| 44 // Current directory for the file, add the full path. | |
| 45 dirFilesMap[dir].add(relativePath); | |
| 46 } else if (!dirFilesMap[dir].contains(relativeDir)) { | |
| 47 // Subdirectory, just add the relative directory path. | |
| 48 dirFilesMap[dir].add('$relativeDir/'); | |
| 49 } | |
| 50 | |
| 51 dir = path.url.dirname(dir); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 // Create an output index.html file for each directory, if one doesn't | |
| 56 // exist already | |
| 57 var futures = []; | |
| 58 dirFilesMap.forEach((directory, files) { | |
| 59 futures.add(_createOutput(directory, files, transform)); | |
| 60 }); | |
| 61 return Future.wait(futures); | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 Future _createOutput( | |
| 66 String directory, List<String> files, AggregateTransform transform) { | |
| 67 var indexAsset = new AssetId( | |
| 68 transform.package, path.join(directory, 'index.html')); | |
| 69 | |
| 70 return transform.hasInput(indexAsset).then((exists) { | |
| 71 // Don't overwrite existing outputs! | |
| 72 if (exists) return; | |
| 73 | |
| 74 // The order of [transform.primaryInputs] is not guaranteed | |
| 75 // to be stable across multiple runs of the transformer. | |
| 76 // Therefore, alphabetically sort the files by path, putting directories | |
| 77 // last. | |
| 78 files.sort((String a, String b) { | |
| 79 var aIsDir = path.url.extension(a) == ''; | |
| 80 var bIsDir = path.url.extension(b) == ''; | |
| 81 if (aIsDir && !bIsDir) { | |
| 82 return -1; | |
| 83 } else if (!aIsDir && bIsDir) { | |
| 84 return 1; | |
| 85 } | |
| 86 return a.compareTo(b); | |
| 87 }); | |
| 88 | |
| 89 // Create the document with a list node. | |
| 90 var doc = new Document.html('<!DOCTYPE html><html><body>' | |
|
Siggi Cherem (dart-lang)
2014/09/16 19:40:50
change to just use StringBuffer? It seems there is
jakemac
2014/09/16 21:17:04
Done.
| |
| 91 '<h1>Entry points</h1><ul></ul></body></html>'); | |
|
Siggi Cherem (dart-lang)
2014/09/16 19:40:50
should we have a different title or divide the lis
jakemac
2014/09/16 21:17:05
Flattened, I think that is the easiest approach an
| |
| 92 var list = doc.body.querySelector('ul'); | |
| 93 | |
| 94 // Add all the assets to the list node. | |
| 95 list.children.addAll(files.map((file) { | |
| 96 return new Element.html('<li><a href="$file">$file</a></li>'); | |
| 97 })); | |
| 98 | |
| 99 // Output the index.html file | |
| 100 transform.addOutput(new Asset.fromString(indexAsset , doc.outerHtml)); | |
| 101 }); | |
| 102 } | |
| 103 | |
| 104 } | |
| OLD | NEW |