| 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: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 'all_entry_points'; | |
| 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 dirFilesMap[dir].add(relativePath); | |
| 44 dir = path.url.dirname(dir); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // Create an output index.html file for each directory, if one doesn't | |
| 49 // exist already | |
| 50 var futures = []; | |
| 51 dirFilesMap.forEach((directory, files) { | |
| 52 futures.add(_createOutput(directory, files, transform)); | |
| 53 }); | |
| 54 return Future.wait(futures); | |
| 55 }); | |
| 56 } | |
| 57 | |
| 58 Future _createOutput( | |
| 59 String directory, List<String> files, AggregateTransform transform) { | |
| 60 var indexAsset = | |
| 61 new AssetId(transform.package, path.url.join(directory, 'index.html')); | |
| 62 | |
| 63 return transform.hasInput(indexAsset).then((exists) { | |
| 64 // Don't overwrite existing outputs! | |
| 65 if (exists) return; | |
| 66 | |
| 67 // Sort alphabetically by recursive path parts. | |
| 68 files.sort((String a, String b) { | |
| 69 var aParts = path.url.split(a); | |
| 70 var bParts = path.url.split(b); | |
| 71 int diff = 0; | |
| 72 int minLength = min(aParts.length, bParts.length); | |
| 73 for (int i = 0; i < minLength; i++) { | |
| 74 // Directories are sorted below files. | |
| 75 var aIsDir = i < aParts.length - 1; | |
| 76 var bIsDir = i < bParts.length - 1; | |
| 77 if (aIsDir && !bIsDir) return 1; | |
| 78 if (!aIsDir && bIsDir) return -1; | |
| 79 | |
| 80 // Raw string comparison, if not identical we return. | |
| 81 diff = aParts[i].compareTo(bParts[i]); | |
| 82 if (diff != 0) return diff; | |
| 83 } | |
| 84 // Identical files, shouldn't happen in practice. | |
| 85 return 0; | |
| 86 }); | |
| 87 | |
| 88 // Create the document with a list. | |
| 89 var doc = new StringBuffer( | |
| 90 '<!DOCTYPE html><html><body><h1>Entry points</h1><ul>'); | |
| 91 | |
| 92 // Add all the assets to the list. | |
| 93 for (var file in files) { | |
| 94 doc.write('<li><a href="$file">$file</a></li>'); | |
| 95 } | |
| 96 | |
| 97 doc.write('</ul></body></html>'); | |
| 98 | |
| 99 // Output the index.html file | |
| 100 transform.addOutput(new Asset.fromString(indexAsset, doc.toString())); | |
| 101 }); | |
| 102 } | |
| 103 } | |
| OLD | NEW |