| 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; |
| 11 import 'package:analyzer/src/generated/source_io.dart'; | 11 import 'package:analyzer/src/generated/source_io.dart'; |
| 12 import 'package:path/path.dart'; | 12 import 'package:path/path.dart'; |
| 13 import 'package:watcher/watcher.dart'; | 13 import 'package:watcher/watcher.dart'; |
| 14 | 14 |
| 15 import 'file_system.dart'; | 15 import 'file_system.dart'; |
| 16 | 16 |
| 17 | |
| 18 /** | 17 /** |
| 19 * An in-memory implementation of [ResourceProvider]. | 18 * An in-memory implementation of [ResourceProvider]. |
| 20 * Use `/` as a path separator. | 19 * Use `/` as a path separator. |
| 21 */ | 20 */ |
| 22 class MemoryResourceProvider implements ResourceProvider { | 21 class MemoryResourceProvider implements ResourceProvider { |
| 23 final Map<String, _MemoryResource> _pathToResource = | 22 final Map<String, _MemoryResource> _pathToResource = |
| 24 new HashMap<String, _MemoryResource>(); | 23 new HashMap<String, _MemoryResource>(); |
| 25 final Map<String, String> _pathToContent = new HashMap<String, String>(); | 24 final Map<String, String> _pathToContent = new HashMap<String, String>(); |
| 26 final Map<String, int> _pathToTimestamp = new HashMap<String, int>(); | 25 final Map<String, int> _pathToTimestamp = new HashMap<String, int>(); |
| 27 final Map<String, List<StreamController<WatchEvent>>> _pathToWatchers = | 26 final Map<String, List<StreamController<WatchEvent>>> _pathToWatchers = |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 155 |
| 157 void _checkFolderAtPath(String path) { | 156 void _checkFolderAtPath(String path) { |
| 158 _MemoryResource resource = _pathToResource[path]; | 157 _MemoryResource resource = _pathToResource[path]; |
| 159 if (resource is! _MemoryFolder) { | 158 if (resource is! _MemoryFolder) { |
| 160 throw new ArgumentError( | 159 throw new ArgumentError( |
| 161 'Folder expected at "$path" but ${resource.runtimeType} found'); | 160 'Folder expected at "$path" but ${resource.runtimeType} found'); |
| 162 } | 161 } |
| 163 } | 162 } |
| 164 | 163 |
| 165 void _notifyWatchers(String path, ChangeType changeType) { | 164 void _notifyWatchers(String path, ChangeType changeType) { |
| 166 _pathToWatchers.forEach( | 165 _pathToWatchers.forEach((String watcherPath, |
| 167 (String watcherPath, List<StreamController<WatchEvent>> streamController
s) { | 166 List<StreamController<WatchEvent>> streamControllers) { |
| 168 if (posix.isWithin(watcherPath, path)) { | 167 if (posix.isWithin(watcherPath, path)) { |
| 169 for (StreamController<WatchEvent> streamController in streamControllers) | 168 for (StreamController<WatchEvent> streamController |
| 170 { | 169 in streamControllers) { |
| 171 streamController.add(new WatchEvent(changeType, path)); | 170 streamController.add(new WatchEvent(changeType, path)); |
| 172 } | 171 } |
| 173 } | 172 } |
| 174 }); | 173 }); |
| 175 } | 174 } |
| 176 } | 175 } |
| 177 | 176 |
| 178 | |
| 179 /** | 177 /** |
| 180 * An in-memory implementation of [File] which acts like a symbolic link to a | 178 * An in-memory implementation of [File] which acts like a symbolic link to a |
| 181 * non-existent file. | 179 * non-existent file. |
| 182 */ | 180 */ |
| 183 class _MemoryDummyLink extends _MemoryResource implements File { | 181 class _MemoryDummyLink extends _MemoryResource implements File { |
| 184 _MemoryDummyLink(MemoryResourceProvider provider, String path) | 182 _MemoryDummyLink(MemoryResourceProvider provider, String path) |
| 185 : super(provider, path); | 183 : super(provider, path); |
| 186 | 184 |
| 187 @override | 185 @override |
| 188 bool get exists => false; | 186 bool get exists => false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 203 Source createSource([Uri uri]) { | 201 Source createSource([Uri uri]) { |
| 204 throw new FileSystemException(path, 'File could not be read'); | 202 throw new FileSystemException(path, 'File could not be read'); |
| 205 } | 203 } |
| 206 | 204 |
| 207 @override | 205 @override |
| 208 bool isOrContains(String path) { | 206 bool isOrContains(String path) { |
| 209 return path == this.path; | 207 return path == this.path; |
| 210 } | 208 } |
| 211 } | 209 } |
| 212 | 210 |
| 213 | |
| 214 /** | 211 /** |
| 215 * An in-memory implementation of [File]. | 212 * An in-memory implementation of [File]. |
| 216 */ | 213 */ |
| 217 class _MemoryFile extends _MemoryResource implements File { | 214 class _MemoryFile extends _MemoryResource implements File { |
| 218 _MemoryFile(MemoryResourceProvider provider, String path) | 215 _MemoryFile(MemoryResourceProvider provider, String path) |
| 219 : super(provider, path); | 216 : super(provider, path); |
| 220 | 217 |
| 221 @override | 218 @override |
| 222 bool get exists => _provider._pathToResource[path] is _MemoryFile; | 219 bool get exists => _provider._pathToResource[path] is _MemoryFile; |
| 223 | 220 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 244 } | 241 } |
| 245 return new _MemoryFileSource(this, uri); | 242 return new _MemoryFileSource(this, uri); |
| 246 } | 243 } |
| 247 | 244 |
| 248 @override | 245 @override |
| 249 bool isOrContains(String path) { | 246 bool isOrContains(String path) { |
| 250 return path == this.path; | 247 return path == this.path; |
| 251 } | 248 } |
| 252 } | 249 } |
| 253 | 250 |
| 254 | |
| 255 /** | 251 /** |
| 256 * An in-memory implementation of [Source]. | 252 * An in-memory implementation of [Source]. |
| 257 */ | 253 */ |
| 258 class _MemoryFileSource extends Source { | 254 class _MemoryFileSource extends Source { |
| 259 final _MemoryFile _file; | 255 final _MemoryFile _file; |
| 260 | 256 |
| 261 final Uri uri; | 257 final Uri uri; |
| 262 | 258 |
| 263 _MemoryFileSource(this._file, this.uri); | 259 _MemoryFileSource(this._file, this.uri); |
| 264 | 260 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 315 |
| 320 @override | 316 @override |
| 321 Uri resolveRelativeUri(Uri relativeUri) { | 317 Uri resolveRelativeUri(Uri relativeUri) { |
| 322 return uri.resolveUri(relativeUri); | 318 return uri.resolveUri(relativeUri); |
| 323 } | 319 } |
| 324 | 320 |
| 325 @override | 321 @override |
| 326 String toString() => _file.toString(); | 322 String toString() => _file.toString(); |
| 327 } | 323 } |
| 328 | 324 |
| 329 | |
| 330 /** | 325 /** |
| 331 * An in-memory implementation of [Folder]. | 326 * An in-memory implementation of [Folder]. |
| 332 */ | 327 */ |
| 333 class _MemoryFolder extends _MemoryResource implements Folder { | 328 class _MemoryFolder extends _MemoryResource implements Folder { |
| 334 _MemoryFolder(MemoryResourceProvider provider, String path) | 329 _MemoryFolder(MemoryResourceProvider provider, String path) |
| 335 : super(provider, path); | 330 : super(provider, path); |
| 336 | 331 |
| 337 @override | 332 @override |
| 338 Stream<WatchEvent> get changes { | 333 Stream<WatchEvent> get changes { |
| 339 StreamController<WatchEvent> streamController = | 334 StreamController<WatchEvent> streamController = |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 | 395 |
| 401 @override | 396 @override |
| 402 bool isOrContains(String path) { | 397 bool isOrContains(String path) { |
| 403 if (path == this.path) { | 398 if (path == this.path) { |
| 404 return true; | 399 return true; |
| 405 } | 400 } |
| 406 return contains(path); | 401 return contains(path); |
| 407 } | 402 } |
| 408 } | 403 } |
| 409 | 404 |
| 410 | |
| 411 /** | 405 /** |
| 412 * An in-memory implementation of [Resource]. | 406 * An in-memory implementation of [Resource]. |
| 413 */ | 407 */ |
| 414 abstract class _MemoryResource implements Resource { | 408 abstract class _MemoryResource implements Resource { |
| 415 final MemoryResourceProvider _provider; | 409 final MemoryResourceProvider _provider; |
| 416 final String path; | 410 final String path; |
| 417 | 411 |
| 418 _MemoryResource(this._provider, this.path); | 412 _MemoryResource(this._provider, this.path); |
| 419 | 413 |
| 420 @override | 414 @override |
| (...skipping 15 matching lines...) Expand all Loading... |
| 436 bool operator ==(other) { | 430 bool operator ==(other) { |
| 437 if (runtimeType != other.runtimeType) { | 431 if (runtimeType != other.runtimeType) { |
| 438 return false; | 432 return false; |
| 439 } | 433 } |
| 440 return path == other.path; | 434 return path == other.path; |
| 441 } | 435 } |
| 442 | 436 |
| 443 @override | 437 @override |
| 444 String toString() => path; | 438 String toString() => path; |
| 445 } | 439 } |
| OLD | NEW |