| 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 if (parentPath != path) { | 108 if (parentPath != path) { |
| 109 newFolder(parentPath); | 109 newFolder(parentPath); |
| 110 } | 110 } |
| 111 _MemoryFolder folder = new _MemoryFolder(this, path); | 111 _MemoryFolder folder = new _MemoryFolder(this, path); |
| 112 _pathToResource[path] = folder; | 112 _pathToResource[path] = folder; |
| 113 _pathToTimestamp[path] = nextStamp++; | 113 _pathToTimestamp[path] = nextStamp++; |
| 114 return folder; | 114 return folder; |
| 115 } else if (resource is _MemoryFolder) { | 115 } else if (resource is _MemoryFolder) { |
| 116 return resource; | 116 return resource; |
| 117 } else { | 117 } else { |
| 118 String message = 'Folder expected at ' | 118 String message = |
| 119 "'$path'" | 119 'Folder expected at ' "'$path'" 'but ${resource.runtimeType} found'; |
| 120 'but ${resource.runtimeType} found'; | |
| 121 throw new ArgumentError(message); | 120 throw new ArgumentError(message); |
| 122 } | 121 } |
| 123 } | 122 } |
| 124 | 123 |
| 125 void _checkFileAtPath(String path) { | 124 void _checkFileAtPath(String path) { |
| 126 _MemoryResource resource = _pathToResource[path]; | 125 _MemoryResource resource = _pathToResource[path]; |
| 127 if (resource is! _MemoryFile) { | 126 if (resource is! _MemoryFile) { |
| 128 throw new ArgumentError( | 127 throw new ArgumentError( |
| 129 'File expected at "$path" but ${resource.runtimeType} found'); | 128 'File expected at "$path" but ${resource.runtimeType} found'); |
| 130 } | 129 } |
| 131 } | 130 } |
| 132 | 131 |
| 133 void _notifyWatchers(String path, ChangeType changeType) { | 132 void _notifyWatchers(String path, ChangeType changeType) { |
| 134 _pathToWatchers.forEach((String watcherPath, List<StreamController<WatchEven
t>> streamControllers) { | 133 _pathToWatchers.forEach( |
| 134 (String watcherPath, List<StreamController<WatchEvent>> streamController
s) { |
| 135 if (posix.isWithin(watcherPath, path)) { | 135 if (posix.isWithin(watcherPath, path)) { |
| 136 for (StreamController<WatchEvent> streamController in streamControllers)
{ | 136 for (StreamController<WatchEvent> streamController in streamControllers) |
| 137 { |
| 137 streamController.add(new WatchEvent(changeType, path)); | 138 streamController.add(new WatchEvent(changeType, path)); |
| 138 } | 139 } |
| 139 } | 140 } |
| 140 }); | 141 }); |
| 141 } | 142 } |
| 142 } | 143 } |
| 143 | 144 |
| 144 | 145 |
| 145 /** | 146 /** |
| 146 * An in-memory implementation of [File] which acts like a symbolic link to a | 147 * An in-memory implementation of [File] which acts like a symbolic link to a |
| 147 * non-existent file. | 148 * non-existent file. |
| 148 */ | 149 */ |
| 149 class _MemoryDummyLink extends _MemoryResource implements File { | 150 class _MemoryDummyLink extends _MemoryResource implements File { |
| 150 _MemoryDummyLink(MemoryResourceProvider provider, String path) : | 151 _MemoryDummyLink(MemoryResourceProvider provider, String path) : super( |
| 151 super(provider, path); | 152 provider, |
| 153 path); |
| 152 | 154 |
| 153 @override | 155 @override |
| 154 bool get exists => false; | 156 bool get exists => false; |
| 155 | 157 |
| 156 String get _content { | 158 String get _content { |
| 157 throw new MemoryResourceException(path, "File '$path' could not be read"); | 159 throw new MemoryResourceException(path, "File '$path' could not be read"); |
| 158 } | 160 } |
| 159 | 161 |
| 160 int get _timestamp => _provider._pathToTimestamp[path]; | 162 int get _timestamp => _provider._pathToTimestamp[path]; |
| 161 | 163 |
| 162 @override | 164 @override |
| 163 Source createSource([Uri uri]) { | 165 Source createSource([Uri uri]) { |
| 164 throw new MemoryResourceException(path, "File '$path' could not be read"); | 166 throw new MemoryResourceException(path, "File '$path' could not be read"); |
| 165 } | 167 } |
| 168 |
| 169 @override |
| 170 bool isOrContains(String path) { |
| 171 return path == this.path; |
| 172 } |
| 166 } | 173 } |
| 167 | 174 |
| 168 | 175 |
| 169 /** | 176 /** |
| 170 * An in-memory implementation of [File]. | 177 * An in-memory implementation of [File]. |
| 171 */ | 178 */ |
| 172 class _MemoryFile extends _MemoryResource implements File { | 179 class _MemoryFile extends _MemoryResource implements File { |
| 173 _MemoryFile(MemoryResourceProvider provider, String path) : | 180 _MemoryFile(MemoryResourceProvider provider, String path) : super( |
| 174 super(provider, path); | 181 provider, |
| 182 path); |
| 175 | 183 |
| 176 String get _content { | 184 String get _content { |
| 177 String content = _provider._pathToContent[path]; | 185 String content = _provider._pathToContent[path]; |
| 178 if (content == null) { | 186 if (content == null) { |
| 179 throw new MemoryResourceException(path, "File '$path' does not exist"); | 187 throw new MemoryResourceException(path, "File '$path' does not exist"); |
| 180 } | 188 } |
| 181 return content; | 189 return content; |
| 182 } | 190 } |
| 183 | 191 |
| 184 int get _timestamp => _provider._pathToTimestamp[path]; | 192 int get _timestamp => _provider._pathToTimestamp[path]; |
| 185 | 193 |
| 186 @override | 194 @override |
| 187 Source createSource([Uri uri]) { | 195 Source createSource([Uri uri]) { |
| 188 if (uri == null) { | 196 if (uri == null) { |
| 189 uri = posix.toUri(path); | 197 uri = posix.toUri(path); |
| 190 } | 198 } |
| 191 return new _MemoryFileSource(this, uri); | 199 return new _MemoryFileSource(this, uri); |
| 192 } | 200 } |
| 201 |
| 202 @override |
| 203 bool isOrContains(String path) { |
| 204 return path == this.path; |
| 205 } |
| 193 } | 206 } |
| 194 | 207 |
| 195 | 208 |
| 196 /** | 209 /** |
| 197 * An in-memory implementation of [Source]. | 210 * An in-memory implementation of [Source]. |
| 198 */ | 211 */ |
| 199 class _MemoryFileSource implements Source { | 212 class _MemoryFileSource implements Source { |
| 200 final _MemoryFile _file; | 213 final _MemoryFile _file; |
| 201 | 214 |
| 202 final Uri uri; | 215 final Uri uri; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 272 |
| 260 @override | 273 @override |
| 261 String toString() => _file.toString(); | 274 String toString() => _file.toString(); |
| 262 } | 275 } |
| 263 | 276 |
| 264 | 277 |
| 265 /** | 278 /** |
| 266 * An in-memory implementation of [Folder]. | 279 * An in-memory implementation of [Folder]. |
| 267 */ | 280 */ |
| 268 class _MemoryFolder extends _MemoryResource implements Folder { | 281 class _MemoryFolder extends _MemoryResource implements Folder { |
| 269 _MemoryFolder(MemoryResourceProvider provider, String path) : | 282 _MemoryFolder(MemoryResourceProvider provider, String path) : super( |
| 270 super(provider, path); | 283 provider, |
| 284 path); |
| 271 @override | 285 @override |
| 272 Stream<WatchEvent> get changes { | 286 Stream<WatchEvent> get changes { |
| 273 StreamController<WatchEvent> streamController = new StreamController<WatchEv
ent>(); | 287 StreamController<WatchEvent> streamController = |
| 288 new StreamController<WatchEvent>(); |
| 274 if (!_provider._pathToWatchers.containsKey(path)) { | 289 if (!_provider._pathToWatchers.containsKey(path)) { |
| 275 _provider._pathToWatchers[path] = <StreamController<WatchEvent>>[]; | 290 _provider._pathToWatchers[path] = <StreamController<WatchEvent>>[]; |
| 276 } | 291 } |
| 277 _provider._pathToWatchers[path].add(streamController); | 292 _provider._pathToWatchers[path].add(streamController); |
| 278 streamController.done.then((_) { | 293 streamController.done.then((_) { |
| 279 _provider._pathToWatchers[path].remove(streamController); | 294 _provider._pathToWatchers[path].remove(streamController); |
| 280 if (_provider._pathToWatchers[path].isEmpty) { | 295 if (_provider._pathToWatchers[path].isEmpty) { |
| 281 _provider._pathToWatchers.remove(path); | 296 _provider._pathToWatchers.remove(path); |
| 282 } | 297 } |
| 283 }); | 298 }); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 310 @override | 325 @override |
| 311 List<Resource> getChildren() { | 326 List<Resource> getChildren() { |
| 312 List<Resource> children = <Resource>[]; | 327 List<Resource> children = <Resource>[]; |
| 313 _provider._pathToResource.forEach((resourcePath, resource) { | 328 _provider._pathToResource.forEach((resourcePath, resource) { |
| 314 if (posix.dirname(resourcePath) == path) { | 329 if (posix.dirname(resourcePath) == path) { |
| 315 children.add(resource); | 330 children.add(resource); |
| 316 } | 331 } |
| 317 }); | 332 }); |
| 318 return children; | 333 return children; |
| 319 } | 334 } |
| 335 |
| 336 @override |
| 337 bool isOrContains(String path) { |
| 338 if (path == this.path) { |
| 339 return true; |
| 340 } |
| 341 return contains(path); |
| 342 } |
| 320 } | 343 } |
| 321 | 344 |
| 322 | 345 |
| 323 /** | 346 /** |
| 324 * An in-memory implementation of [Resource]. | 347 * An in-memory implementation of [Resource]. |
| 325 */ | 348 */ |
| 326 abstract class _MemoryResource implements Resource { | 349 abstract class _MemoryResource implements Resource { |
| 327 final MemoryResourceProvider _provider; | 350 final MemoryResourceProvider _provider; |
| 328 final String path; | 351 final String path; |
| 329 | 352 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 351 bool operator ==(other) { | 374 bool operator ==(other) { |
| 352 if (runtimeType != other.runtimeType) { | 375 if (runtimeType != other.runtimeType) { |
| 353 return false; | 376 return false; |
| 354 } | 377 } |
| 355 return path == other.path; | 378 return path == other.path; |
| 356 } | 379 } |
| 357 | 380 |
| 358 @override | 381 @override |
| 359 String toString() => path; | 382 String toString() => path; |
| 360 } | 383 } |
| OLD | NEW |