| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 front_end.memory_file_system; | 5 library front_end.memory_file_system; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
| 10 | 10 |
| 11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 | 12 |
| 13 import 'file_system.dart'; | 13 import 'file_system.dart'; |
| 14 | 14 |
| 15 /// Concrete implementation of [FileSystem] which performs its operations on an | 15 /// Concrete implementation of [FileSystem] which performs its operations on an |
| 16 /// in-memory virtual file system. | 16 /// in-memory virtual file system. |
| 17 /// | 17 /// |
| 18 /// Not intended to be implemented or extended by clients. | 18 /// Not intended to be implemented or extended by clients. |
| 19 class MemoryFileSystem implements FileSystem { | 19 class MemoryFileSystem implements FileSystem { |
| 20 @override | 20 @override |
| 21 final p.Context context; | 21 final p.Context context; |
| 22 | 22 |
| 23 final Map<String, Uint8List> _files = {}; | 23 final Map<Uri, Uint8List> _files = {}; |
| 24 | 24 |
| 25 /// The "current directory" in the in-memory virtual file system. | 25 /// The "current directory" in the in-memory virtual file system. |
| 26 /// | 26 /// |
| 27 /// This is used to convert relative paths to absolute paths. | 27 /// This is used to convert relative paths to absolute paths. |
| 28 String currentDirectory; | 28 /// |
| 29 /// Always ends in a trailing '/'. |
| 30 Uri currentDirectory; |
| 29 | 31 |
| 30 MemoryFileSystem(this.context, this.currentDirectory); | 32 MemoryFileSystem(this.context, Uri currentDirectory) |
| 31 | 33 : currentDirectory = _addTrailingSlash(currentDirectory); |
| 32 @override | |
| 33 MemoryFileSystemEntity entityForPath(String path) => | |
| 34 new MemoryFileSystemEntity._( | |
| 35 this, context.normalize(context.join(currentDirectory, path))); | |
| 36 | 34 |
| 37 @override | 35 @override |
| 38 MemoryFileSystemEntity entityForUri(Uri uri) { | 36 MemoryFileSystemEntity entityForUri(Uri uri) { |
| 39 if (uri.scheme != 'file') throw new ArgumentError('File URI expected'); | 37 return new MemoryFileSystemEntity._( |
| 40 // Note: we don't have to verify that the URI's path is absolute, because | 38 this, currentDirectory.resolveUri(uri).normalizePath()); |
| 41 // URIs with non-empty schemes always have absolute paths. | 39 } |
| 42 return entityForPath(context.fromUri(uri)); | 40 |
| 41 static Uri _addTrailingSlash(Uri uri) { |
| 42 if (!uri.path.endsWith('/')) { |
| 43 uri = uri.replace(path: uri.path + '/'); |
| 44 } |
| 45 return uri; |
| 43 } | 46 } |
| 44 } | 47 } |
| 45 | 48 |
| 46 /// Concrete implementation of [FileSystemEntity] for use by | 49 /// Concrete implementation of [FileSystemEntity] for use by |
| 47 /// [MemoryFileSystem]. | 50 /// [MemoryFileSystem]. |
| 48 class MemoryFileSystemEntity implements FileSystemEntity { | 51 class MemoryFileSystemEntity implements FileSystemEntity { |
| 49 final MemoryFileSystem _fileSystem; | 52 final MemoryFileSystem _fileSystem; |
| 50 | 53 |
| 51 @override | 54 @override |
| 52 final String path; | 55 final Uri uri; |
| 53 | 56 |
| 54 MemoryFileSystemEntity._(this._fileSystem, this.path); | 57 MemoryFileSystemEntity._(this._fileSystem, this.uri); |
| 55 | 58 |
| 56 @override | 59 @override |
| 57 int get hashCode => path.hashCode; | 60 int get hashCode => uri.hashCode; |
| 58 | 61 |
| 59 @override | 62 @override |
| 60 bool operator ==(Object other) => | 63 bool operator ==(Object other) => |
| 61 other is MemoryFileSystemEntity && | 64 other is MemoryFileSystemEntity && |
| 62 other.path == path && | 65 other.uri == uri && |
| 63 identical(other._fileSystem, _fileSystem); | 66 identical(other._fileSystem, _fileSystem); |
| 64 | 67 |
| 65 @override | 68 @override |
| 66 Future<List<int>> readAsBytes() async { | 69 Future<List<int>> readAsBytes() async { |
| 67 List<int> contents = _fileSystem._files[path]; | 70 List<int> contents = _fileSystem._files[uri]; |
| 68 if (contents != null) { | 71 if (contents != null) { |
| 69 return contents.toList(); | 72 return contents.toList(); |
| 70 } | 73 } |
| 71 throw new Exception('File does not exist'); | 74 throw new Exception('File does not exist'); |
| 72 } | 75 } |
| 73 | 76 |
| 74 @override | 77 @override |
| 75 Future<String> readAsString() async { | 78 Future<String> readAsString() async { |
| 76 List<int> contents = await readAsBytes(); | 79 List<int> contents = await readAsBytes(); |
| 77 return UTF8.decode(contents); | 80 return UTF8.decode(contents); |
| 78 } | 81 } |
| 79 | 82 |
| 80 /// Writes the given raw bytes to this file system entity. | 83 /// Writes the given raw bytes to this file system entity. |
| 81 /// | 84 /// |
| 82 /// If no file exists, one is created. If a file exists already, it is | 85 /// If no file exists, one is created. If a file exists already, it is |
| 83 /// overwritten. | 86 /// overwritten. |
| 84 void writeAsBytesSync(List<int> bytes) { | 87 void writeAsBytesSync(List<int> bytes) { |
| 85 _fileSystem._files[path] = new Uint8List.fromList(bytes); | 88 _fileSystem._files[uri] = new Uint8List.fromList(bytes); |
| 86 } | 89 } |
| 87 | 90 |
| 88 /// Writes the given string to this file system entity. | 91 /// Writes the given string to this file system entity. |
| 89 /// | 92 /// |
| 90 /// The string is encoded as UTF-8. | 93 /// The string is encoded as UTF-8. |
| 91 /// | 94 /// |
| 92 /// If no file exists, one is created. If a file exists already, it is | 95 /// If no file exists, one is created. If a file exists already, it is |
| 93 /// overwritten. | 96 /// overwritten. |
| 94 void writeAsStringSync(String s) { | 97 void writeAsStringSync(String s) { |
| 95 // Note: the return type of UTF8.encode is List<int>, but in practice it | 98 // Note: the return type of UTF8.encode is List<int>, but in practice it |
| 96 // always returns Uint8List. We rely on that for efficiency, so that we | 99 // always returns Uint8List. We rely on that for efficiency, so that we |
| 97 // don't have to make an extra copy. | 100 // don't have to make an extra copy. |
| 98 _fileSystem._files[path] = UTF8.encode(s) as Uint8List; | 101 _fileSystem._files[uri] = UTF8.encode(s) as Uint8List; |
| 99 } | 102 } |
| 100 } | 103 } |
| OLD | NEW |