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

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

Issue 2471283002: Add implementations of the front end FileSystem API. (Closed)
Patch Set: Created 4 years, 1 month 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library front_end.memory_file_system;
6
7 import 'dart:async';
8 import 'dart:convert';
9 import 'dart:typed_data';
10
11 import 'package:path/path.dart' as p;
12
13 import 'file_system.dart';
14
15 /// Concrete implementation of [FileSystem] which performs its operations on an
16 /// in-memory virtual file system.
17 ///
18 /// Not intended to be implemented or extended by clients.
19 class MemoryFileSystem implements FileSystem {
20 @override
21 final p.Context context;
22
23 final Map<String, Uint8List> _files = {};
24
25 /// The "current directory" in the in-memory virtual file system.
26 ///
27 /// This is used to convert relative paths to absolute paths.
28 String currentDirectory;
29
30 MemoryFileSystem(this.context, this.currentDirectory);
31
32 @override
33 MemoryFileSystemEntity entityForPath(String path) =>
34 new MemoryFileSystemEntity._(
35 this, context.normalize(context.join(currentDirectory, path)));
36
37 @override
38 MemoryFileSystemEntity entityForUri(Uri uri) {
39 if (uri.scheme != 'file') throw new ArgumentError('File URI expected');
scheglov 2016/11/03 02:15:24 Do we want to check (or TODO) that uri is absolute
Paul Berry 2016/11/03 15:16:41 AFAICT, the scheme check is sufficient--it's impos
40 return entityForPath(context.fromUri(uri));
41 }
42 }
43
44 /// Concrete implementation of [FileSystemEntity] for use by
45 /// [MemoryFileSystem].
46 class MemoryFileSystemEntity implements FileSystemEntity {
47 final MemoryFileSystem _fileSystem;
48
49 @override
50 final String path;
51
52 MemoryFileSystemEntity._(this._fileSystem, this.path);
53
54 @override
55 int get hashCode => path.hashCode;
56
57 @override
58 bool operator ==(Object other) =>
59 other is MemoryFileSystemEntity && other.path == path;
Siggi Cherem (dart-lang) 2016/11/03 00:46:50 I'm ok not adding it, but any need to also check t
Paul Berry 2016/11/03 15:16:41 Done.
60
61 @override
62 Future<List<int>> readAsBytes() async {
63 List<int> contents = _fileSystem._files[path];
64 if (contents != null) {
65 return contents.toList();
66 }
67 throw new Exception('File does not exist');
68 }
69
70 @override
71 Future<String> readAsString() async {
72 List<int> contents = await readAsBytes();
73 return UTF8.decode(contents);
74 }
75
76 /// Writes the given raw bytes to this file system entity.
77 ///
78 /// If no file exists, one is created. If a file exists already, it is
79 /// overwritten.
80 void writeAsBytesSync(List<int> bytes) {
81 _fileSystem._files[path] = new Uint8List.fromList(bytes);
82 }
83
84 /// Writes the given string to this file system entity.
85 ///
86 /// The string is encoded as UTF-8.
87 ///
88 /// If no file exists, one is created. If a file exists already, it is
89 /// overwritten.
90 void writeAsStringSync(String s) {
91 _fileSystem._files[path] = UTF8.encode(s);
scheglov 2016/11/03 02:15:23 AFAIK UTF8 returns Uint8List at runtime, but the s
Paul Berry 2016/11/03 15:16:41 True. That means that technically this implementa
92 }
93 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698