OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library trydart.projectServer; | 5 library trydart.projectServer; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
| 9 import 'dart:async' show |
| 10 Future; |
| 11 |
9 import 'dart:convert' show | 12 import 'dart:convert' show |
10 HtmlEscape, | 13 HtmlEscape, |
11 JSON, | 14 JSON, |
12 UTF8; | 15 UTF8; |
13 | 16 |
14 class WatchHandler { | 17 class WatchHandler { |
15 final WebSocket socket; | 18 final WebSocket socket; |
16 | 19 |
17 final Set<String> watchedFiles; | 20 final Set<String> watchedFiles; |
18 | 21 |
19 static final Set<WatchHandler> handlers = new Set<WatchHandler>(); | 22 static final Set<WatchHandler> handlers = new Set<WatchHandler>(); |
20 | 23 |
21 static const Map<int, String> fsEventNames = const <int, String>{ | 24 static const Map<int, String> fsEventNames = const <int, String>{ |
22 FileSystemEvent.CREATE: 'create', | 25 FileSystemEvent.CREATE: 'create', |
23 FileSystemEvent.DELETE: 'delete', | 26 FileSystemEvent.DELETE: 'delete', |
24 FileSystemEvent.MODIFY: 'modify', | 27 FileSystemEvent.MODIFY: 'modify', |
25 FileSystemEvent.MOVE: 'move', | 28 FileSystemEvent.MOVE: 'move', |
26 }; | 29 }; |
27 | 30 |
28 WatchHandler(this.socket, Iterable<String> watchedFiles) | 31 WatchHandler(this.socket, Iterable<String> watchedFiles) |
29 : this.watchedFiles = watchedFiles.toSet(); | 32 : this.watchedFiles = watchedFiles.toSet(); |
30 | 33 |
31 handleFileSystemEvent(FileSystemEvent event) { | 34 handleFileSystemEvent(FileSystemEvent event) { |
32 String type = event.isDirectory ? 'directory' : 'file'; | 35 if (event.isDirectory) return; |
33 String eventType = fsEventNames[event.type]; | 36 String type = fsEventNames[event.type]; |
34 if (eventType == null) eventType = 'unknown'; | 37 if (type == null) type = 'unknown'; |
35 socket.add(JSON.encode({eventType: [event.path]})); | 38 String path = new Uri.file(event.path).pathSegments.last; |
| 39 shouldIgnore(type, path).then((bool ignored) { |
| 40 if (ignored) return; |
| 41 socket.add(JSON.encode({type: [path]})); |
| 42 }); |
| 43 } |
| 44 |
| 45 Future<bool> shouldIgnore(String type, String path) { |
| 46 switch (type) { |
| 47 case 'create': |
| 48 return new Future<bool>.value(!watchedFiles.contains(path)); |
| 49 case 'delete': |
| 50 return Conversation.listProjectFiles().then((List<String> files) { |
| 51 watchedFiles |
| 52 ..retainAll(files) |
| 53 ..addAll(files); |
| 54 return watchedFiles.contains(path); |
| 55 }); |
| 56 case 'modify': |
| 57 return new Future<bool>.value(false); |
| 58 default: |
| 59 print('Unhandled fs-event for $path ($type).'); |
| 60 return new Future<bool>.value(true); |
| 61 } |
36 } | 62 } |
37 | 63 |
38 onData(_) { | 64 onData(_) { |
39 // TODO(ahe): Move POST code here? | 65 // TODO(ahe): Move POST code here? |
40 } | 66 } |
41 | 67 |
42 onDone() { | 68 onDone() { |
43 handlers.remove(this); | 69 handlers.remove(this); |
44 } | 70 } |
45 | 71 |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 Conversation.packageRoot = Uri.base.resolve(arguments[4]); | 451 Conversation.packageRoot = Uri.base.resolve(arguments[4]); |
426 } | 452 } |
427 HttpServer.bind(host, port).then((HttpServer server) { | 453 HttpServer.bind(host, port).then((HttpServer server) { |
428 print('HTTP server started on http://$host:${server.port}/'); | 454 print('HTTP server started on http://$host:${server.port}/'); |
429 server.listen(Conversation.onRequest, onError: Conversation.onError); | 455 server.listen(Conversation.onRequest, onError: Conversation.onError); |
430 }).catchError((e) { | 456 }).catchError((e) { |
431 print("HttpServer.bind error: $e"); | 457 print("HttpServer.bind error: $e"); |
432 exit(1); | 458 exit(1); |
433 }); | 459 }); |
434 } | 460 } |
OLD | NEW |