| 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 library pub.barback.sources; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:barback/barback.dart'; | |
| 10 import 'package:path/path.dart' as path; | |
| 11 import 'package:watcher/watcher.dart'; | |
| 12 | |
| 13 import '../entrypoint.dart'; | |
| 14 import '../io.dart'; | |
| 15 import '../package.dart'; | |
| 16 import '../package_graph.dart'; | |
| 17 | |
| 18 /// Adds all of the source assets in the provided packages to barback and | |
| 19 /// then watches the public directories for changes. | |
| 20 /// | |
| 21 /// [watcherFactory] should return a [DirectoryWatcher] watching the given | |
| 22 /// directory for changes. | |
| 23 /// | |
| 24 /// Returns a Future that completes when the sources are loaded and the watchers | |
| 25 /// are active. | |
| 26 Future watchSources(PackageGraph graph, Barback barback, | |
| 27 WatcherType watcherType) { | |
| 28 return Future.wait(graph.packages.values.map((package) { | |
| 29 // If this package comes from a cached source, its contents won't change so | |
| 30 // we don't need to monitor it. `packageId` will be null for the application | |
| 31 // package, since that's not locked. | |
| 32 var packageId = graph.lockFile.packages[package.name]; | |
| 33 if (packageId != null && | |
| 34 graph.entrypoint.cache.sources[packageId.source].shouldCache) { | |
| 35 barback.updateSources(_listAssets(graph.entrypoint, package)); | |
| 36 return new Future.value(); | |
| 37 } | |
| 38 | |
| 39 // Watch the visible package directories for changes. | |
| 40 return Future.wait(_getPublicDirectories(graph.entrypoint, package) | |
| 41 .map((name) { | |
| 42 var subdirectory = path.join(package.dir, name); | |
| 43 if (!dirExists(subdirectory)) return new Future.value(); | |
| 44 | |
| 45 // TODO(nweiz): close these watchers when [barback] is closed. | |
| 46 var watcher = watcherType.create(subdirectory); | |
| 47 watcher.events.listen((event) { | |
| 48 // Don't watch files symlinked into these directories. | |
| 49 // TODO(rnystrom): If pub gets rid of symlinks, remove this. | |
| 50 var parts = path.split(event.path); | |
| 51 if (parts.contains("packages") || parts.contains("assets")) return; | |
| 52 | |
| 53 // Skip ".js" files that were (most likely) compiled from nearby ".dart" | |
| 54 // files. These are created by the Editor's "Run as JavaScript" command | |
| 55 // and are written directly into the package's directory. When pub's | |
| 56 // dart2js transformer then tries to create the same file name, we get | |
| 57 // a build error. To avoid that, just don't consider that file to be a | |
| 58 // source. | |
| 59 // TODO(rnystrom): Remove this when the Editor no longer generates .js | |
| 60 // files. See #15859. | |
| 61 if (event.path.endsWith(".dart.js")) return; | |
| 62 | |
| 63 var id = new AssetId(package.name, | |
| 64 path.relative(event.path, from: package.dir)); | |
| 65 if (event.type == ChangeType.REMOVE) { | |
| 66 barback.removeSources([id]); | |
| 67 } else { | |
| 68 barback.updateSources([id]); | |
| 69 } | |
| 70 }); | |
| 71 return watcher.ready; | |
| 72 })).then((_) { | |
| 73 barback.updateSources(_listAssets(graph.entrypoint, package)); | |
| 74 }); | |
| 75 })); | |
| 76 } | |
| 77 | |
| 78 /// Adds all of the source assets in the provided packages to barback. | |
| 79 void loadSources(PackageGraph graph, Barback barback) { | |
| 80 for (var package in graph.packages.values) { | |
| 81 barback.updateSources(_listAssets(graph.entrypoint, package)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 /// Lists all of the visible files in [package]. | |
| 86 /// | |
| 87 /// This is the recursive contents of the "asset" and "lib" directories (if | |
| 88 /// present). If [package] is the entrypoint package, it also includes the | |
| 89 /// contents of "web". | |
| 90 List<AssetId> _listAssets(Entrypoint entrypoint, Package package) { | |
| 91 var files = <AssetId>[]; | |
| 92 | |
| 93 for (var dirPath in _getPublicDirectories(entrypoint, package)) { | |
| 94 var dir = path.join(package.dir, dirPath); | |
| 95 if (!dirExists(dir)) continue; | |
| 96 for (var entry in listDir(dir, recursive: true)) { | |
| 97 // Ignore "packages" symlinks if there. | |
| 98 if (path.split(entry).contains("packages")) continue; | |
| 99 | |
| 100 // Skip directories. | |
| 101 if (!fileExists(entry)) continue; | |
| 102 | |
| 103 // Skip ".js" files that were (most likely) compiled from nearby ".dart" | |
| 104 // files. These are created by the Editor's "Run as JavaScript" command | |
| 105 // and are written directly into the package's directory. When pub's | |
| 106 // dart2js transformer then tries to create the same file name, we get | |
| 107 // a build error. To avoid that, just don't consider that file to be a | |
| 108 // source. | |
| 109 // TODO(rnystrom): Remove this when the Editor no longer generates .js | |
| 110 // files. See #15859. | |
| 111 if (entry.endsWith(".dart.js")) continue; | |
| 112 | |
| 113 var id = new AssetId(package.name, | |
| 114 path.relative(entry, from: package.dir)); | |
| 115 files.add(id); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 return files; | |
| 120 } | |
| 121 | |
| 122 /// Gets the names of the top-level directories in [package] whose contents | |
| 123 /// should be provided as source assets. | |
| 124 Iterable<String> _getPublicDirectories(Entrypoint entrypoint, Package package) { | |
| 125 var directories = ["asset", "lib"]; | |
| 126 if (package.name == entrypoint.root.name) directories.add("web"); | |
| 127 return directories; | |
| 128 } | |
| 129 | |
| 130 /// An enum describing different modes of constructing a [DirectoryWatcher]. | |
| 131 abstract class WatcherType { | |
| 132 /// A watcher that automatically chooses its type based on the operating | |
| 133 /// system. | |
| 134 static const AUTO = const _AutoWatcherType(); | |
| 135 | |
| 136 /// A watcher that always polls the filesystem for changes. | |
| 137 static const POLLING = const _PollingWatcherType(); | |
| 138 | |
| 139 /// No directory watcher at all. | |
| 140 static const NONE = const _NoneWatcherType(); | |
| 141 | |
| 142 /// Creates a new DirectoryWatcher. | |
| 143 DirectoryWatcher create(String directory); | |
| 144 | |
| 145 String toString(); | |
| 146 } | |
| 147 | |
| 148 class _AutoWatcherType implements WatcherType { | |
| 149 const _AutoWatcherType(); | |
| 150 | |
| 151 DirectoryWatcher create(String directory) => | |
| 152 new DirectoryWatcher(directory); | |
| 153 | |
| 154 String toString() => "auto"; | |
| 155 } | |
| 156 | |
| 157 class _PollingWatcherType implements WatcherType { | |
| 158 const _PollingWatcherType(); | |
| 159 | |
| 160 DirectoryWatcher create(String directory) => | |
| 161 new PollingDirectoryWatcher(directory); | |
| 162 | |
| 163 String toString() => "polling"; | |
| 164 } | |
| 165 | |
| 166 class _NoneWatcherType implements WatcherType { | |
| 167 const _NoneWatcherType(); | |
| 168 | |
| 169 DirectoryWatcher create(String directory) => null; | |
| 170 | |
| 171 String toString() => "none"; | |
| 172 } | |
| OLD | NEW |