OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:barback/barback.dart'; | 8 import 'package:barback/barback.dart'; |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 import 'package:watcher/watcher.dart'; | 10 import 'package:watcher/watcher.dart'; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 .then((server) => _adminServer = server); | 189 .then((server) => _adminServer = server); |
190 } | 190 } |
191 | 191 |
192 /// Binds a new port to serve assets from within [rootDirectory] in the | 192 /// Binds a new port to serve assets from within [rootDirectory] in the |
193 /// entrypoint package. | 193 /// entrypoint package. |
194 /// | 194 /// |
195 /// Adds and watches the sources within that directory. Returns a [Future] | 195 /// Adds and watches the sources within that directory. Returns a [Future] |
196 /// that completes to the bound server. | 196 /// that completes to the bound server. |
197 /// | 197 /// |
198 /// If [rootDirectory] is already being served, returns that existing server. | 198 /// If [rootDirectory] is already being served, returns that existing server. |
199 Future<BarbackServer> serveDirectory(String rootDirectory) { | 199 Future<BarbackServer> serveDirectory(String rootDirectory) async { |
200 // See if there is already a server bound to the directory. | 200 // See if there is already a server bound to the directory. |
201 var directory = _directories[rootDirectory]; | 201 var directory = _directories[rootDirectory]; |
202 if (directory != null) { | 202 if (directory != null) { |
203 return directory.server.then((server) { | 203 return directory.server.then((server) { |
204 log.fine('Already serving $rootDirectory on ${server.url}.'); | 204 log.fine('Already serving $rootDirectory on ${server.url}.'); |
205 return server; | 205 return server; |
206 }); | 206 }); |
207 } | 207 } |
208 | 208 |
209 // See if the new directory overlaps any existing servers. | 209 // See if the new directory overlaps any existing servers. |
(...skipping 14 matching lines...) Expand all Loading... |
224 .toSet(); | 224 .toSet(); |
225 while (boundPorts.contains(port)) { | 225 while (boundPorts.contains(port)) { |
226 port++; | 226 port++; |
227 } | 227 } |
228 } | 228 } |
229 | 229 |
230 var sourceDirectory = new SourceDirectory( | 230 var sourceDirectory = new SourceDirectory( |
231 this, rootDirectory, _hostname, port); | 231 this, rootDirectory, _hostname, port); |
232 _directories[rootDirectory] = sourceDirectory; | 232 _directories[rootDirectory] = sourceDirectory; |
233 | 233 |
234 return _provideDirectorySources(rootPackage, rootDirectory) | 234 sourceDirectory.watchSubscription = |
235 .then((subscription) { | 235 await _provideDirectorySources(rootPackage, rootDirectory); |
236 sourceDirectory.watchSubscription = subscription; | 236 return await sourceDirectory.serve(); |
237 return sourceDirectory.serve(); | |
238 }); | |
239 } | 237 } |
240 | 238 |
241 /// Binds a new port to serve assets from within the "bin" directory of | 239 /// Binds a new port to serve assets from within the "bin" directory of |
242 /// [package]. | 240 /// [package]. |
243 /// | 241 /// |
244 /// Adds the sources within that directory and then binds a server to it. | 242 /// Adds the sources within that directory and then binds a server to it. |
245 /// Unlike [serveDirectory], this works with packages that are not the | 243 /// Unlike [serveDirectory], this works with packages that are not the |
246 /// entrypoint. | 244 /// entrypoint. |
247 /// | 245 /// |
248 /// Returns a [Future] that completes to the bound server. | 246 /// Returns a [Future] that completes to the bound server. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 /// [id] is assumed to be an executable in a bin directory. The return value | 299 /// [id] is assumed to be an executable in a bin directory. The return value |
302 /// is intended for log output and may contain formatting. | 300 /// is intended for log output and may contain formatting. |
303 String _formatExecutable(AssetId id) => | 301 String _formatExecutable(AssetId id) => |
304 log.bold("${id.package}:${path.basenameWithoutExtension(id.path)}"); | 302 log.bold("${id.package}:${path.basenameWithoutExtension(id.path)}"); |
305 | 303 |
306 /// Stops the server bound to [rootDirectory]. | 304 /// Stops the server bound to [rootDirectory]. |
307 /// | 305 /// |
308 /// Also removes any source files within that directory from barback. Returns | 306 /// Also removes any source files within that directory from barback. Returns |
309 /// the URL of the unbound server, of `null` if [rootDirectory] was not | 307 /// the URL of the unbound server, of `null` if [rootDirectory] was not |
310 /// bound to a server. | 308 /// bound to a server. |
311 Future<Uri> unserveDirectory(String rootDirectory) { | 309 Future<Uri> unserveDirectory(String rootDirectory) async { |
312 log.fine("Unserving $rootDirectory."); | 310 log.fine("Unserving $rootDirectory."); |
313 var directory = _directories.remove(rootDirectory); | 311 var directory = _directories.remove(rootDirectory); |
314 if (directory == null) return new Future.value(); | 312 if (directory == null) return new Future.value(); |
315 | 313 |
316 return directory.server.then((server) { | 314 var url = (await directory.server).url; |
317 var url = server.url; | 315 await directory.close(); |
318 return directory.close().then((_) { | 316 _removeDirectorySources(rootDirectory); |
319 _removeDirectorySources(rootDirectory); | 317 return url; |
320 return url; | |
321 }); | |
322 }); | |
323 } | 318 } |
324 | 319 |
325 /// Gets the source directory that contains [assetPath] within the entrypoint | 320 /// Gets the source directory that contains [assetPath] within the entrypoint |
326 /// package. | 321 /// package. |
327 /// | 322 /// |
328 /// If [assetPath] is not contained within a source directory, this throws | 323 /// If [assetPath] is not contained within a source directory, this throws |
329 /// an exception. | 324 /// an exception. |
330 String getSourceDirectoryContaining(String assetPath) => | 325 String getSourceDirectoryContaining(String assetPath) => |
331 _directories.values | 326 _directories.values |
332 .firstWhere((dir) => path.isWithin(dir.directory, assetPath)) | 327 .firstWhere((dir) => path.isWithin(dir.directory, assetPath)) |
333 .directory; | 328 .directory; |
334 | 329 |
335 /// Return all URLs serving [assetPath] in this environment. | 330 /// Return all URLs serving [assetPath] in this environment. |
336 Future<List<Uri>> getUrlsForAssetPath(String assetPath) { | 331 Future<List<Uri>> getUrlsForAssetPath(String assetPath) async { |
337 // Check the three (mutually-exclusive) places the path could be pointing. | 332 // Check the three (mutually-exclusive) places the path could be pointing. |
338 return _lookUpPathInServerRoot(assetPath).then((urls) { | 333 var urls = await _lookUpPathInServerRoot(assetPath); |
339 if (urls.isNotEmpty) return urls; | 334 if (urls.isEmpty) urls = await _lookUpPathInPackagesDirectory(assetPath); |
340 return _lookUpPathInPackagesDirectory(assetPath); | 335 if (urls.isEmpty) urls = await _lookUpPathInDependency(assetPath); |
341 }).then((urls) { | 336 return urls; |
342 if (urls.isNotEmpty) return urls; | |
343 return _lookUpPathInDependency(assetPath); | |
344 }); | |
345 } | 337 } |
346 | 338 |
347 /// Look up [assetPath] in the root directories of servers running in the | 339 /// Look up [assetPath] in the root directories of servers running in the |
348 /// entrypoint package. | 340 /// entrypoint package. |
349 Future<List<Uri>> _lookUpPathInServerRoot(String assetPath) { | 341 Future<List<Uri>> _lookUpPathInServerRoot(String assetPath) { |
350 // Find all of the servers whose root directories contain the asset and | 342 // Find all of the servers whose root directories contain the asset and |
351 // generate appropriate URLs for each. | 343 // generate appropriate URLs for each. |
352 return Future.wait(_directories.values | 344 return Future.wait(_directories.values |
353 .where((dir) => path.isWithin(dir.directory, assetPath)) | 345 .where((dir) => path.isWithin(dir.directory, assetPath)) |
354 .map((dir) { | 346 .map((dir) { |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 String toString() => "polling"; | 779 String toString() => "polling"; |
788 } | 780 } |
789 | 781 |
790 class _NoneWatcherType implements WatcherType { | 782 class _NoneWatcherType implements WatcherType { |
791 const _NoneWatcherType(); | 783 const _NoneWatcherType(); |
792 | 784 |
793 DirectoryWatcher create(String directory) => null; | 785 DirectoryWatcher create(String directory) => null; |
794 | 786 |
795 String toString() => "none"; | 787 String toString() => "none"; |
796 } | 788 } |
OLD | NEW |