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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/lib/memory_file_system.dart
diff --git a/pkg/front_end/lib/memory_file_system.dart b/pkg/front_end/lib/memory_file_system.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f3e10144d3fa63f357fabbc36b9d4890feae719d
--- /dev/null
+++ b/pkg/front_end/lib/memory_file_system.dart
@@ -0,0 +1,93 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library front_end.memory_file_system;
+
+import 'dart:async';
+import 'dart:convert';
+import 'dart:typed_data';
+
+import 'package:path/path.dart' as p;
+
+import 'file_system.dart';
+
+/// Concrete implementation of [FileSystem] which performs its operations on an
+/// in-memory virtual file system.
+///
+/// Not intended to be implemented or extended by clients.
+class MemoryFileSystem implements FileSystem {
+ @override
+ final p.Context context;
+
+ final Map<String, Uint8List> _files = {};
+
+ /// The "current directory" in the in-memory virtual file system.
+ ///
+ /// This is used to convert relative paths to absolute paths.
+ String currentDirectory;
+
+ MemoryFileSystem(this.context, this.currentDirectory);
+
+ @override
+ MemoryFileSystemEntity entityForPath(String path) =>
+ new MemoryFileSystemEntity._(
+ this, context.normalize(context.join(currentDirectory, path)));
+
+ @override
+ MemoryFileSystemEntity entityForUri(Uri uri) {
+ 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
+ return entityForPath(context.fromUri(uri));
+ }
+}
+
+/// Concrete implementation of [FileSystemEntity] for use by
+/// [MemoryFileSystem].
+class MemoryFileSystemEntity implements FileSystemEntity {
+ final MemoryFileSystem _fileSystem;
+
+ @override
+ final String path;
+
+ MemoryFileSystemEntity._(this._fileSystem, this.path);
+
+ @override
+ int get hashCode => path.hashCode;
+
+ @override
+ bool operator ==(Object other) =>
+ 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.
+
+ @override
+ Future<List<int>> readAsBytes() async {
+ List<int> contents = _fileSystem._files[path];
+ if (contents != null) {
+ return contents.toList();
+ }
+ throw new Exception('File does not exist');
+ }
+
+ @override
+ Future<String> readAsString() async {
+ List<int> contents = await readAsBytes();
+ return UTF8.decode(contents);
+ }
+
+ /// Writes the given raw bytes to this file system entity.
+ ///
+ /// If no file exists, one is created. If a file exists already, it is
+ /// overwritten.
+ void writeAsBytesSync(List<int> bytes) {
+ _fileSystem._files[path] = new Uint8List.fromList(bytes);
+ }
+
+ /// Writes the given string to this file system entity.
+ ///
+ /// The string is encoded as UTF-8.
+ ///
+ /// If no file exists, one is created. If a file exists already, it is
+ /// overwritten.
+ void writeAsStringSync(String s) {
+ _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
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698