| 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 library memory_file_system; | 5 library memory_file_system; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | 10 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 new HashMap<String, _MemoryResource>(); | 24 new HashMap<String, _MemoryResource>(); |
| 25 final Map<String, String> _pathToContent = new HashMap<String, String>(); | 25 final Map<String, String> _pathToContent = new HashMap<String, String>(); |
| 26 final Map<String, int> _pathToTimestamp = new HashMap<String, int>(); | 26 final Map<String, int> _pathToTimestamp = new HashMap<String, int>(); |
| 27 final Map<String, List<StreamController<WatchEvent>>> _pathToWatchers = | 27 final Map<String, List<StreamController<WatchEvent>>> _pathToWatchers = |
| 28 new HashMap<String, List<StreamController<WatchEvent>>>(); | 28 new HashMap<String, List<StreamController<WatchEvent>>>(); |
| 29 int nextStamp = 0; | 29 int nextStamp = 0; |
| 30 | 30 |
| 31 @override | 31 @override |
| 32 Context get pathContext => posix; | 32 Context get pathContext => posix; |
| 33 | 33 |
| 34 /** |
| 35 * Delete the file with the given path. |
| 36 */ |
| 34 void deleteFile(String path) { | 37 void deleteFile(String path) { |
| 35 _checkFileAtPath(path); | 38 _checkFileAtPath(path); |
| 36 _pathToResource.remove(path); | 39 _pathToResource.remove(path); |
| 37 _pathToContent.remove(path); | 40 _pathToContent.remove(path); |
| 38 _pathToTimestamp.remove(path); | 41 _pathToTimestamp.remove(path); |
| 39 _notifyWatchers(path, ChangeType.REMOVE); | 42 _notifyWatchers(path, ChangeType.REMOVE); |
| 40 } | 43 } |
| 41 | 44 |
| 45 /** |
| 46 * Delete the folder with the given path |
| 47 * and recurively delete nested files and folders. |
| 48 */ |
| 49 void deleteFolder(String path) { |
| 50 _checkFolderAtPath(path); |
| 51 _MemoryFolder folder = _pathToResource[path]; |
| 52 for (Resource child in folder.getChildren()) { |
| 53 if (child is File) { |
| 54 deleteFile(child.path); |
| 55 } else if (child is Folder) { |
| 56 deleteFolder(child.path); |
| 57 } else { |
| 58 throw 'failed to delete resource: $child'; |
| 59 } |
| 60 } |
| 61 _pathToResource.remove(path); |
| 62 _pathToContent.remove(path); |
| 63 _pathToTimestamp.remove(path); |
| 64 _notifyWatchers(path, ChangeType.REMOVE); |
| 65 } |
| 66 |
| 42 @override | 67 @override |
| 43 Resource getResource(String path) { | 68 Resource getResource(String path) { |
| 44 path = posix.normalize(path); | 69 path = posix.normalize(path); |
| 45 Resource resource = _pathToResource[path]; | 70 Resource resource = _pathToResource[path]; |
| 46 if (resource == null) { | 71 if (resource == null) { |
| 47 resource = new _MemoryFile(this, path); | 72 resource = new _MemoryFile(this, path); |
| 48 } | 73 } |
| 49 return resource; | 74 return resource; |
| 50 } | 75 } |
| 51 | 76 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 File newDummyLink(String path) { | 93 File newDummyLink(String path) { |
| 69 path = posix.normalize(path); | 94 path = posix.normalize(path); |
| 70 newFolder(posix.dirname(path)); | 95 newFolder(posix.dirname(path)); |
| 71 _MemoryDummyLink link = new _MemoryDummyLink(this, path); | 96 _MemoryDummyLink link = new _MemoryDummyLink(this, path); |
| 72 _pathToResource[path] = link; | 97 _pathToResource[path] = link; |
| 73 _pathToTimestamp[path] = nextStamp++; | 98 _pathToTimestamp[path] = nextStamp++; |
| 74 _notifyWatchers(path, ChangeType.ADD); | 99 _notifyWatchers(path, ChangeType.ADD); |
| 75 return link; | 100 return link; |
| 76 } | 101 } |
| 77 | 102 |
| 78 File newFile(String path, String content) { | 103 File newFile(String path, String content, [int stamp]) { |
| 79 path = posix.normalize(path); | 104 path = posix.normalize(path); |
| 80 newFolder(posix.dirname(path)); | 105 newFolder(posix.dirname(path)); |
| 81 _MemoryFile file = new _MemoryFile(this, path); | 106 _MemoryFile file = new _MemoryFile(this, path); |
| 82 _pathToResource[path] = file; | 107 _pathToResource[path] = file; |
| 83 _pathToContent[path] = content; | 108 _pathToContent[path] = content; |
| 84 _pathToTimestamp[path] = nextStamp++; | 109 _pathToTimestamp[path] = stamp != null ? stamp : nextStamp++; |
| 85 _notifyWatchers(path, ChangeType.ADD); | 110 _notifyWatchers(path, ChangeType.ADD); |
| 86 return file; | 111 return file; |
| 87 } | 112 } |
| 88 | 113 |
| 89 Folder newFolder(String path) { | 114 Folder newFolder(String path) { |
| 90 path = posix.normalize(path); | 115 path = posix.normalize(path); |
| 91 if (!path.startsWith('/')) { | 116 if (!path.startsWith('/')) { |
| 92 throw new ArgumentError("Path must start with '/'"); | 117 throw new ArgumentError("Path must start with '/'"); |
| 93 } | 118 } |
| 94 _MemoryResource resource = _pathToResource[path]; | 119 _MemoryResource resource = _pathToResource[path]; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 111 } | 136 } |
| 112 | 137 |
| 113 void _checkFileAtPath(String path) { | 138 void _checkFileAtPath(String path) { |
| 114 _MemoryResource resource = _pathToResource[path]; | 139 _MemoryResource resource = _pathToResource[path]; |
| 115 if (resource is! _MemoryFile) { | 140 if (resource is! _MemoryFile) { |
| 116 throw new ArgumentError( | 141 throw new ArgumentError( |
| 117 'File expected at "$path" but ${resource.runtimeType} found'); | 142 'File expected at "$path" but ${resource.runtimeType} found'); |
| 118 } | 143 } |
| 119 } | 144 } |
| 120 | 145 |
| 146 void _checkFolderAtPath(String path) { |
| 147 _MemoryResource resource = _pathToResource[path]; |
| 148 if (resource is! _MemoryFolder) { |
| 149 throw new ArgumentError( |
| 150 'Folder expected at "$path" but ${resource.runtimeType} found'); |
| 151 } |
| 152 } |
| 153 |
| 121 void _notifyWatchers(String path, ChangeType changeType) { | 154 void _notifyWatchers(String path, ChangeType changeType) { |
| 122 _pathToWatchers.forEach( | 155 _pathToWatchers.forEach( |
| 123 (String watcherPath, List<StreamController<WatchEvent>> streamController
s) { | 156 (String watcherPath, List<StreamController<WatchEvent>> streamController
s) { |
| 124 if (posix.isWithin(watcherPath, path)) { | 157 if (posix.isWithin(watcherPath, path)) { |
| 125 for (StreamController<WatchEvent> streamController in streamControllers) | 158 for (StreamController<WatchEvent> streamController in streamControllers) |
| 126 { | 159 { |
| 127 streamController.add(new WatchEvent(changeType, path)); | 160 streamController.add(new WatchEvent(changeType, path)); |
| 128 } | 161 } |
| 129 } | 162 } |
| 130 }); | 163 }); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 bool operator ==(other) { | 411 bool operator ==(other) { |
| 379 if (runtimeType != other.runtimeType) { | 412 if (runtimeType != other.runtimeType) { |
| 380 return false; | 413 return false; |
| 381 } | 414 } |
| 382 return path == other.path; | 415 return path == other.path; |
| 383 } | 416 } |
| 384 | 417 |
| 385 @override | 418 @override |
| 386 String toString() => path; | 419 String toString() => path; |
| 387 } | 420 } |
| OLD | NEW |