ba241c0c8d
This carries a number of benefits: - It allows the front end to trivially support schemes other than "file:" (e.g. "http:") by allowing the client to supply a FileSystem implementation that handles them. - It is more consistent with the functionality of the ".packages" file (which allows packages to map to any kind of URI). - It allows the "bazel root" feature to be rewritten to use a magic scheme rather than a magic path. (This eliminates concerns about the magic path overlapping with a user's use case). Note that this feature has been renamed to "multi root" since it is sufficiently generic to be applicable to build systems other than Bazel. - It reduces the risk of forgetting to use the front end's FileSystem abstraction to access the file system, since the native file system interfaces do not accept URIs. R=danrubel@google.com Review-Url: https://codereview.chromium.org/2614063007 .
104 lines
2.9 KiB
Dart
104 lines
2.9 KiB
Dart
// 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<Uri, Uint8List> _files = {};
|
|
|
|
/// The "current directory" in the in-memory virtual file system.
|
|
///
|
|
/// This is used to convert relative URIs to absolute URIs.
|
|
///
|
|
/// Always ends in a trailing '/'.
|
|
Uri currentDirectory;
|
|
|
|
MemoryFileSystem(this.context, Uri currentDirectory)
|
|
: currentDirectory = _addTrailingSlash(currentDirectory);
|
|
|
|
@override
|
|
MemoryFileSystemEntity entityForUri(Uri uri) {
|
|
return new MemoryFileSystemEntity._(
|
|
this, currentDirectory.resolveUri(uri).normalizePath());
|
|
}
|
|
|
|
static Uri _addTrailingSlash(Uri uri) {
|
|
if (!uri.path.endsWith('/')) {
|
|
uri = uri.replace(path: uri.path + '/');
|
|
}
|
|
return uri;
|
|
}
|
|
}
|
|
|
|
/// Concrete implementation of [FileSystemEntity] for use by
|
|
/// [MemoryFileSystem].
|
|
class MemoryFileSystemEntity implements FileSystemEntity {
|
|
final MemoryFileSystem _fileSystem;
|
|
|
|
@override
|
|
final Uri uri;
|
|
|
|
MemoryFileSystemEntity._(this._fileSystem, this.uri);
|
|
|
|
@override
|
|
int get hashCode => uri.hashCode;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is MemoryFileSystemEntity &&
|
|
other.uri == uri &&
|
|
identical(other._fileSystem, _fileSystem);
|
|
|
|
@override
|
|
Future<List<int>> readAsBytes() async {
|
|
List<int> contents = _fileSystem._files[uri];
|
|
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[uri] = 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) {
|
|
// Note: the return type of UTF8.encode is List<int>, but in practice it
|
|
// always returns Uint8List. We rely on that for efficiency, so that we
|
|
// don't have to make an extra copy.
|
|
_fileSystem._files[uri] = UTF8.encode(s) as Uint8List;
|
|
}
|
|
}
|