Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: pkg/front_end/lib/memory_file_system.dart

Issue 2852463004: Revert "Extend file-system abstraction with a couple methods, remove context." (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/front_end/lib/kernel_generator.dart ('k') | pkg/front_end/lib/physical_file_system.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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;
12
11 import 'file_system.dart'; 13 import 'file_system.dart';
12 14
13 /// Concrete implementation of [FileSystem] which performs its operations on an 15 /// Concrete implementation of [FileSystem] which performs its operations on an
14 /// in-memory virtual file system. 16 /// in-memory virtual file system.
15 /// 17 ///
16 /// Not intended to be implemented or extended by clients. 18 /// Not intended to be implemented or extended by clients.
17 class MemoryFileSystem implements FileSystem { 19 class MemoryFileSystem implements FileSystem {
20 @override
21 final p.Context context;
22
18 final Map<Uri, Uint8List> _files = {}; 23 final Map<Uri, Uint8List> _files = {};
19 final Map<Uri, DateTime> _lastModified = {};
20
21 // Counter used to create mock last-modification timestamps. The memory
22 // file-system is mainly used for testing, so we use mock timestamps to avoid
23 // introducing non-determinism.
24 int _lastUpdate = 0;
25 24
26 /// The "current directory" in the in-memory virtual file system. 25 /// The "current directory" in the in-memory virtual file system.
27 /// 26 ///
28 /// This is used to convert relative URIs to absolute URIs. 27 /// This is used to convert relative URIs to absolute URIs.
29 /// 28 ///
30 /// Always ends in a trailing '/'. 29 /// Always ends in a trailing '/'.
31 Uri currentDirectory; 30 Uri currentDirectory;
32 31
33 MemoryFileSystem(Uri currentDirectory) 32 MemoryFileSystem(this.context, Uri currentDirectory)
34 : currentDirectory = _addTrailingSlash(currentDirectory); 33 : currentDirectory = _addTrailingSlash(currentDirectory);
35 34
36 @override 35 @override
37 MemoryFileSystemEntity entityForUri(Uri uri) { 36 MemoryFileSystemEntity entityForUri(Uri uri) {
38 return new MemoryFileSystemEntity._( 37 return new MemoryFileSystemEntity._(
39 this, currentDirectory.resolveUri(uri).normalizePath()); 38 this, currentDirectory.resolveUri(uri).normalizePath());
40 } 39 }
41 40
42 static Uri _addTrailingSlash(Uri uri) { 41 static Uri _addTrailingSlash(Uri uri) {
43 if (!uri.path.endsWith('/')) { 42 if (!uri.path.endsWith('/')) {
(...skipping 30 matching lines...) Expand all
74 } 73 }
75 throw new Exception('File does not exist'); 74 throw new Exception('File does not exist');
76 } 75 }
77 76
78 @override 77 @override
79 Future<String> readAsString() async { 78 Future<String> readAsString() async {
80 List<int> contents = await readAsBytes(); 79 List<int> contents = await readAsBytes();
81 return UTF8.decode(contents); 80 return UTF8.decode(contents);
82 } 81 }
83 82
84 @override
85 Future<bool> exists() async => _fileSystem._files[uri] != null;
86
87 @override
88 Future<DateTime> lastModified() async {
89 var lastModified = _fileSystem._lastModified[uri];
90 if (lastModified == null) {
91 throw new Exception('File does not exist');
92 }
93 return lastModified;
94 }
95
96 /// Writes the given raw bytes to this file system entity. 83 /// Writes the given raw bytes to this file system entity.
97 /// 84 ///
98 /// 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
99 /// overwritten. 86 /// overwritten.
100 void writeAsBytesSync(List<int> bytes) { 87 void writeAsBytesSync(List<int> bytes) {
101 _update(uri, new Uint8List.fromList(bytes)); 88 _fileSystem._files[uri] = new Uint8List.fromList(bytes);
102 } 89 }
103 90
104 /// Writes the given string to this file system entity. 91 /// Writes the given string to this file system entity.
105 /// 92 ///
106 /// The string is encoded as UTF-8. 93 /// The string is encoded as UTF-8.
107 /// 94 ///
108 /// 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
109 /// overwritten. 96 /// overwritten.
110 void writeAsStringSync(String s) { 97 void writeAsStringSync(String s) {
111 // 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
112 // 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
113 // don't have to make an extra copy. 100 // don't have to make an extra copy.
114 _update(uri, UTF8.encode(s) as Uint8List); 101 _fileSystem._files[uri] = UTF8.encode(s) as Uint8List;
115 }
116
117 void _update(Uri uri, Uint8List data) {
118 _fileSystem._files[uri] = data;
119 _fileSystem._lastModified[uri] =
120 new DateTime.fromMicrosecondsSinceEpoch(++_fileSystem._lastUpdate);
121 } 102 }
122 } 103 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/kernel_generator.dart ('k') | pkg/front_end/lib/physical_file_system.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698