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

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

Issue 2844903002: Extend file-system abstraction with a couple methods, remove context (Closed)
Patch Set: fix test in windows bot 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
13 import 'file_system.dart'; 11 import 'file_system.dart';
14 12
15 /// Concrete implementation of [FileSystem] which performs its operations on an 13 /// Concrete implementation of [FileSystem] which performs its operations on an
16 /// in-memory virtual file system. 14 /// in-memory virtual file system.
17 /// 15 ///
18 /// Not intended to be implemented or extended by clients. 16 /// Not intended to be implemented or extended by clients.
19 class MemoryFileSystem implements FileSystem { 17 class MemoryFileSystem implements FileSystem {
20 @override 18 final Map<Uri, Uint8List> _files = {};
21 final p.Context context; 19 final Map<Uri, DateTime> _lastModified = {};
22 20
23 final Map<Uri, Uint8List> _files = {}; 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;
24 25
25 /// The "current directory" in the in-memory virtual file system. 26 /// The "current directory" in the in-memory virtual file system.
26 /// 27 ///
27 /// This is used to convert relative URIs to absolute URIs. 28 /// This is used to convert relative URIs to absolute URIs.
28 /// 29 ///
29 /// Always ends in a trailing '/'. 30 /// Always ends in a trailing '/'.
30 Uri currentDirectory; 31 Uri currentDirectory;
31 32
32 MemoryFileSystem(this.context, Uri currentDirectory) 33 MemoryFileSystem(Uri currentDirectory)
33 : currentDirectory = _addTrailingSlash(currentDirectory); 34 : currentDirectory = _addTrailingSlash(currentDirectory);
34 35
35 @override 36 @override
36 MemoryFileSystemEntity entityForUri(Uri uri) { 37 MemoryFileSystemEntity entityForUri(Uri uri) {
37 return new MemoryFileSystemEntity._( 38 return new MemoryFileSystemEntity._(
38 this, currentDirectory.resolveUri(uri).normalizePath()); 39 this, currentDirectory.resolveUri(uri).normalizePath());
39 } 40 }
40 41
41 static Uri _addTrailingSlash(Uri uri) { 42 static Uri _addTrailingSlash(Uri uri) {
42 if (!uri.path.endsWith('/')) { 43 if (!uri.path.endsWith('/')) {
(...skipping 30 matching lines...) Expand all
73 } 74 }
74 throw new Exception('File does not exist'); 75 throw new Exception('File does not exist');
75 } 76 }
76 77
77 @override 78 @override
78 Future<String> readAsString() async { 79 Future<String> readAsString() async {
79 List<int> contents = await readAsBytes(); 80 List<int> contents = await readAsBytes();
80 return UTF8.decode(contents); 81 return UTF8.decode(contents);
81 } 82 }
82 83
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
83 /// Writes the given raw bytes to this file system entity. 96 /// Writes the given raw bytes to this file system entity.
84 /// 97 ///
85 /// If no file exists, one is created. If a file exists already, it is 98 /// If no file exists, one is created. If a file exists already, it is
86 /// overwritten. 99 /// overwritten.
87 void writeAsBytesSync(List<int> bytes) { 100 void writeAsBytesSync(List<int> bytes) {
88 _fileSystem._files[uri] = new Uint8List.fromList(bytes); 101 _update(uri, new Uint8List.fromList(bytes));
89 } 102 }
90 103
91 /// Writes the given string to this file system entity. 104 /// Writes the given string to this file system entity.
92 /// 105 ///
93 /// The string is encoded as UTF-8. 106 /// The string is encoded as UTF-8.
94 /// 107 ///
95 /// If no file exists, one is created. If a file exists already, it is 108 /// If no file exists, one is created. If a file exists already, it is
96 /// overwritten. 109 /// overwritten.
97 void writeAsStringSync(String s) { 110 void writeAsStringSync(String s) {
98 // Note: the return type of UTF8.encode is List<int>, but in practice it 111 // Note: the return type of UTF8.encode is List<int>, but in practice it
99 // always returns Uint8List. We rely on that for efficiency, so that we 112 // always returns Uint8List. We rely on that for efficiency, so that we
100 // don't have to make an extra copy. 113 // don't have to make an extra copy.
101 _fileSystem._files[uri] = UTF8.encode(s) as Uint8List; 114 _update(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);
102 } 121 }
103 } 122 }
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