Index: pkg/polymer/lib/src/build/index_page_builder.dart |
diff --git a/pkg/polymer/lib/src/build/index_page_builder.dart b/pkg/polymer/lib/src/build/index_page_builder.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..829932f5aed1671e78ffc8caa9843d841a8481ee |
--- /dev/null |
+++ b/pkg/polymer/lib/src/build/index_page_builder.dart |
@@ -0,0 +1,104 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+/// Builds an index.html file in each folder containing entry points, if none |
+/// already exists. This file simply lists all the entry point files. |
+library polymer.src.build.index_page_builder; |
+ |
+import 'dart:async'; |
+ |
+import 'package:barback/barback.dart'; |
+import 'package:html5lib/dom.dart'; |
+import 'package:path/path.dart' as path; |
+ |
+import 'common.dart'; |
+ |
+/// Builds an index.html file in each folder containing entry points, if none |
+/// already exists. This file simply lists all the entry point files. |
+class IndexPageBuilder extends AggregateTransformer { |
+ final TransformOptions options; |
+ |
+ IndexPageBuilder(this.options); |
+ |
+ classifyPrimary(AssetId id) { |
+ if (!options.isHtmlEntryPoint(id)) return null; |
+ // Group all entry points together. |
+ 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
|
+ } |
+ |
+ Future apply(AggregateTransform transform) { |
+ Map<String, List<String>> dirFilesMap = {}; |
+ |
+ return transform.primaryInputs.toList().then((assets) { |
+ // Add the asset to its directory, and make sure its directory is included |
+ // in all its parents. |
+ for (var asset in assets) { |
+ var dir = path.url.dirname(asset.id.path); |
+ while (dir != '.') { |
+ dirFilesMap.putIfAbsent(dir, () => []); |
+ |
+ var relativePath = path.url.relative(asset.id.path, from: dir); |
+ var relativeDir = path.url.dirname(relativePath); |
+ if (relativeDir == '.') { |
+ // Current directory for the file, add the full path. |
+ dirFilesMap[dir].add(relativePath); |
+ } else if (!dirFilesMap[dir].contains(relativeDir)) { |
+ // Subdirectory, just add the relative directory path. |
+ dirFilesMap[dir].add('$relativeDir/'); |
+ } |
+ |
+ dir = path.url.dirname(dir); |
+ } |
+ } |
+ |
+ // Create an output index.html file for each directory, if one doesn't |
+ // exist already |
+ var futures = []; |
+ dirFilesMap.forEach((directory, files) { |
+ futures.add(_createOutput(directory, files, transform)); |
+ }); |
+ return Future.wait(futures); |
+ }); |
+ } |
+ |
+ Future _createOutput( |
+ String directory, List<String> files, AggregateTransform transform) { |
+ var indexAsset = new AssetId( |
+ transform.package, path.join(directory, 'index.html')); |
+ |
+ return transform.hasInput(indexAsset).then((exists) { |
+ // Don't overwrite existing outputs! |
+ if (exists) return; |
+ |
+ // The order of [transform.primaryInputs] is not guaranteed |
+ // to be stable across multiple runs of the transformer. |
+ // Therefore, alphabetically sort the files by path, putting directories |
+ // last. |
+ files.sort((String a, String b) { |
+ var aIsDir = path.url.extension(a) == ''; |
+ var bIsDir = path.url.extension(b) == ''; |
+ if (aIsDir && !bIsDir) { |
+ return -1; |
+ } else if (!aIsDir && bIsDir) { |
+ return 1; |
+ } |
+ return a.compareTo(b); |
+ }); |
+ |
+ // Create the document with a list node. |
+ 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.
|
+ '<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
|
+ var list = doc.body.querySelector('ul'); |
+ |
+ // Add all the assets to the list node. |
+ list.children.addAll(files.map((file) { |
+ return new Element.html('<li><a href="$file">$file</a></li>'); |
+ })); |
+ |
+ // Output the index.html file |
+ transform.addOutput(new Asset.fromString(indexAsset , doc.outerHtml)); |
+ }); |
+ } |
+ |
+} |