From c09a43041d9e5e59cc3b7c82b013b69effb8c1eb Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Tue, 20 Mar 2018 12:18:33 -0700 Subject: [PATCH] Import dart:io directly (#77) --- CHANGELOG.md | 9 + lib/src/backends/local.dart | 2 - lib/src/backends/local/local_file_system.dart | 26 +- lib/src/io.dart | 5 +- lib/src/io/interface.dart | 553 ------------------ lib/src/io/shim.dart | 10 - lib/src/io/shim_dart_io.dart | 49 -- lib/src/io/shim_internal.dart | 49 -- pubspec.yaml | 4 +- 9 files changed, 25 insertions(+), 682 deletions(-) delete mode 100644 lib/src/io/interface.dart delete mode 100644 lib/src/io/shim.dart delete mode 100644 lib/src/io/shim_dart_io.dart delete mode 100644 lib/src/io/shim_internal.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 161ef5d65269c..268adc96163a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +#### 3.0.0 + +* Import `dart:io` unconditionally. More recent Dart SDK revisions allow + `dart:io` to be imported in a browser context, though if methods are actually + invoked, they will fail. This matches well with `package:file`, where users + can use the `memory` library and get in-memory implementations of the + `dart:io` interfaces. +* Bump minimum Dart SDK to `1.24.0` + #### 2.3.7 * Fix Dart 2 error. diff --git a/lib/src/backends/local.dart b/lib/src/backends/local.dart index b5ef40595264d..b67dd1fc1223a 100644 --- a/lib/src/backends/local.dart +++ b/lib/src/backends/local.dart @@ -12,8 +12,6 @@ import 'package:file/src/io.dart' as io; import 'package:file/file.dart'; import 'package:path/path.dart' as p; -import '../io/shim.dart' as shim; - part 'local/local_directory.dart'; part 'local/local_file.dart'; part 'local/local_file_system.dart'; diff --git a/lib/src/backends/local/local_file_system.dart b/lib/src/backends/local/local_file_system.dart index cca53646045d7..b26d1079c78a0 100644 --- a/lib/src/backends/local/local_file_system.dart +++ b/lib/src/backends/local/local_file_system.dart @@ -14,13 +14,13 @@ class LocalFileSystem extends FileSystem { @override Directory directory(dynamic path) => - new _LocalDirectory(this, shim.newDirectory(getPath(path))); + new _LocalDirectory(this, new io.Directory(getPath(path))); @override - File file(dynamic path) => new _LocalFile(this, shim.newFile(getPath(path))); + File file(dynamic path) => new _LocalFile(this, new io.File(getPath(path))); @override - Link link(dynamic path) => new _LocalLink(this, shim.newLink(getPath(path))); + Link link(dynamic path) => new _LocalLink(this, new io.Link(getPath(path))); @override p.Context get path => new p.Context(); @@ -30,36 +30,36 @@ class LocalFileSystem extends FileSystem { /// platform-dependent, and may be set by an environment variable. @override Directory get systemTempDirectory => - new _LocalDirectory(this, shim.systemTemp()); + new _LocalDirectory(this, io.Directory.systemTemp); @override - Directory get currentDirectory => directory(shim.currentDirectory.path); + Directory get currentDirectory => directory(io.Directory.current.path); @override - set currentDirectory(dynamic path) => shim.currentDirectory = path; + set currentDirectory(dynamic path) => io.Directory.current = path; @override - Future stat(String path) => shim.stat(path); + Future stat(String path) => io.FileStat.stat(path); @override - io.FileStat statSync(String path) => shim.statSync(path); + io.FileStat statSync(String path) => io.FileStat.statSync(path); @override Future identical(String path1, String path2) => - shim.identical(path1, path2); + io.FileSystemEntity.identical(path1, path2); @override bool identicalSync(String path1, String path2) => - shim.identicalSync(path1, path2); + io.FileSystemEntity.identicalSync(path1, path2); @override - bool get isWatchSupported => shim.isWatchSupported; + bool get isWatchSupported => io.FileSystemEntity.isWatchSupported; @override Future type(String path, {bool followLinks: true}) => - shim.type(path, followLinks); + io.FileSystemEntity.type(path, followLinks: followLinks); @override io.FileSystemEntityType typeSync(String path, {bool followLinks: true}) => - shim.typeSync(path, followLinks); + io.FileSystemEntity.typeSync(path, followLinks: followLinks); } diff --git a/lib/src/io.dart b/lib/src/io.dart index bd430a4052659..9d57e786991b1 100644 --- a/lib/src/io.dart +++ b/lib/src/io.dart @@ -8,10 +8,7 @@ /// the `file` package. The `file` package re-exports these interfaces (or in /// some cases, implementations of these interfaces by the same name), so this /// file need not be exposes publicly and exists for internal use only. -/// -/// For VM users, this exports the actual `dart:io` interfaces; for browser -/// contexts, this exports locally-defined versions of those same interfaces. -export 'io/interface.dart' if (dart.library.io) 'dart:io' +export 'dart:io' show Directory, File, diff --git a/lib/src/io/interface.dart b/lib/src/io/interface.dart deleted file mode 100644 index 78bb8995bf651..0000000000000 --- a/lib/src/io/interface.dart +++ /dev/null @@ -1,553 +0,0 @@ -// Copyright (c) 2017, 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. - -import 'dart:async'; -import 'dart:convert'; - -// ignore: public_member_api_docs -abstract class Directory implements FileSystemEntity { - // ignore: public_member_api_docs - Future create({bool recursive: false}); - - // ignore: public_member_api_docs - void createSync({bool recursive: false}); - - // ignore: public_member_api_docs - Future createTemp([String prefix]); - - // ignore: public_member_api_docs - Directory createTempSync([String prefix]); - - @override - Future rename(String newPath); - - @override - Directory renameSync(String newPath); - - @override - Directory get absolute; - - // ignore: public_member_api_docs - Stream list( - {bool recursive: false, bool followLinks: true}); - - // ignore: public_member_api_docs - List listSync( - {bool recursive: false, bool followLinks: true}); -} - -// ignore: public_member_api_docs -abstract class File implements FileSystemEntity { - // ignore: public_member_api_docs - Future create({bool recursive: false}); - - // ignore: public_member_api_docs - void createSync({bool recursive: false}); - - @override - Future rename(String newPath); - - @override - File renameSync(String newPath); - - // ignore: public_member_api_docs - Future copy(String newPath); - - // ignore: public_member_api_docs - File copySync(String newPath); - - // ignore: public_member_api_docs - Future length(); - - // ignore: public_member_api_docs - int lengthSync(); - - @override - File get absolute; - - // ignore: public_member_api_docs - Future lastAccessed(); - - // ignore: public_member_api_docs - DateTime lastAccessedSync(); - - // ignore: public_member_api_docs - Future setLastAccessed(DateTime time); - - // ignore: public_member_api_docs - void setLastAccessedSync(DateTime time); - - // ignore: public_member_api_docs - Future lastModified(); - - // ignore: public_member_api_docs - DateTime lastModifiedSync(); - - // ignore: public_member_api_docs - Future setLastModified(DateTime time); - - // ignore: public_member_api_docs - void setLastModifiedSync(DateTime time); - - // ignore: public_member_api_docs - Future open({FileMode mode: FileMode.READ}); - - // ignore: public_member_api_docs - RandomAccessFile openSync({FileMode mode: FileMode.READ}); - - // ignore: public_member_api_docs - Stream> openRead([int start, int end]); - - // ignore: public_member_api_docs - IOSink openWrite({FileMode mode: FileMode.WRITE, Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - Future> readAsBytes(); - - // ignore: public_member_api_docs - List readAsBytesSync(); - - // ignore: public_member_api_docs - Future readAsString({Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - String readAsStringSync({Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - Future> readAsLines({Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - List readAsLinesSync({Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - Future writeAsBytes(List bytes, - {FileMode mode: FileMode.WRITE, bool flush: false}); - - // ignore: public_member_api_docs - void writeAsBytesSync(List bytes, - {FileMode mode: FileMode.WRITE, bool flush: false}); - - // ignore: public_member_api_docs - Future writeAsString(String contents, - {FileMode mode: FileMode.WRITE, - Encoding encoding: UTF8, - bool flush: false}); - - // ignore: public_member_api_docs - void writeAsStringSync(String contents, - {FileMode mode: FileMode.WRITE, - Encoding encoding: UTF8, - bool flush: false}); -} - -// ignore: public_member_api_docs -enum FileLock { - // ignore: constant_identifier_names, public_member_api_docs - SHARED, - - // ignore: constant_identifier_names, public_member_api_docs - EXCLUSIVE, - - // ignore: constant_identifier_names, public_member_api_docs - BLOCKING_SHARED, - - // ignore: constant_identifier_names, public_member_api_docs - BLOCKING_EXCLUSIVE, -} - -// ignore: public_member_api_docs -class FileMode { - // ignore: constant_identifier_names, public_member_api_docs - static const FileMode READ = const FileMode._internal(0); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileMode WRITE = const FileMode._internal(1); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileMode APPEND = const FileMode._internal(2); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileMode WRITE_ONLY = const FileMode._internal(3); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileMode WRITE_ONLY_APPEND = const FileMode._internal(4); - - final int _mode; // ignore: unused_field - - const FileMode._internal(this._mode); -} - -// ignore: public_member_api_docs -abstract class FileStat { - // ignore: public_member_api_docs - DateTime get changed; - - // ignore: public_member_api_docs - DateTime get modified; - - // ignore: public_member_api_docs - DateTime get accessed; - - // ignore: public_member_api_docs - FileSystemEntityType get type; - - // ignore: public_member_api_docs - int get mode; - - // ignore: public_member_api_docs - int get size; - - // ignore: public_member_api_docs - String modeString(); -} - -// ignore: public_member_api_docs -abstract class FileSystemEntity { - // ignore: public_member_api_docs - String get path; - - // ignore: public_member_api_docs - Uri get uri; - - // ignore: public_member_api_docs - Future exists(); - - // ignore: public_member_api_docs - bool existsSync(); - - // ignore: public_member_api_docs - Future rename(String newPath); - - // ignore: public_member_api_docs - FileSystemEntity renameSync(String newPath); - - // ignore: public_member_api_docs - Future resolveSymbolicLinks(); - - // ignore: public_member_api_docs - String resolveSymbolicLinksSync(); - - // ignore: public_member_api_docs - Future stat(); - - // ignore: public_member_api_docs - FileStat statSync(); - - // ignore: public_member_api_docs - Future delete({bool recursive: false}); - - // ignore: public_member_api_docs - void deleteSync({bool recursive: false}); - - // ignore: public_member_api_docs - Stream watch( - {int events: FileSystemEvent.ALL, bool recursive: false}); - - // ignore: public_member_api_docs - bool get isAbsolute; - - // ignore: public_member_api_docs - FileSystemEntity get absolute; - - // ignore: public_member_api_docs - Directory get parent; -} - -// ignore: public_member_api_docs -class FileSystemEntityType { - // ignore: constant_identifier_names, public_member_api_docs - static const FileSystemEntityType FILE = - const FileSystemEntityType._internal(0); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileSystemEntityType DIRECTORY = - const FileSystemEntityType._internal(1); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileSystemEntityType LINK = - const FileSystemEntityType._internal(2); - - // ignore: constant_identifier_names, public_member_api_docs - static const FileSystemEntityType NOT_FOUND = - const FileSystemEntityType._internal(3); - - final int _type; - const FileSystemEntityType._internal(this._type); - - // ignore: public_member_api_docs - @override - String toString() => - const ['FILE', 'DIRECTORY', 'LINK', 'NOT_FOUND'][_type]; -} - -// ignore: public_member_api_docs -abstract class FileSystemEvent { - // ignore: constant_identifier_names, public_member_api_docs - static const int CREATE = 1 << 0; - - // ignore: constant_identifier_names, public_member_api_docs - static const int MODIFY = 1 << 1; - - // ignore: constant_identifier_names, public_member_api_docs - static const int DELETE = 1 << 2; - - // ignore: constant_identifier_names, public_member_api_docs - static const int MOVE = 1 << 3; - - // ignore: constant_identifier_names, public_member_api_docs - static const int ALL = CREATE | MODIFY | DELETE | MOVE; - - // ignore: public_member_api_docs - int get type; - - // ignore: public_member_api_docs - String get path; - - // ignore: public_member_api_docs - bool get isDirectory; -} - -// ignore: public_member_api_docs -class FileSystemException implements IOException { - // ignore: public_member_api_docs - const FileSystemException([this.message = "", this.path = "", this.osError]); - - // ignore: public_member_api_docs - final String message; - - // ignore: public_member_api_docs - final String path; - - // ignore: public_member_api_docs - final OSError osError; - - // ignore: public_member_api_docs - @override - String toString() { - StringBuffer sb = new StringBuffer(); - sb.write("FileSystemException"); - if (message.isNotEmpty) { - sb.write(": $message"); - if (path != null) { - sb.write(", path = '$path'"); - } - if (osError != null) { - sb.write(" ($osError)"); - } - } else if (osError != null) { - sb.write(": $osError"); - if (path != null) { - sb.write(", path = '$path'"); - } - } else if (path != null) { - sb.write(": $path"); - } - return sb.toString(); - } -} - -// ignore: public_member_api_docs -abstract class IOException implements Exception {} - -// ignore: public_member_api_docs -abstract class IOSink implements StreamSink>, StringSink { - // ignore: public_member_api_docs - Encoding encoding; - - // ignore: public_member_api_docs - @override - void add(List data); - - // ignore: public_member_api_docs - @override - void write(Object obj); - - // ignore: public_member_api_docs - @override - void writeAll(Iterable objects, [String separator = ""]); - - // ignore: public_member_api_docs - @override - void writeln([Object obj = ""]); - - // ignore: public_member_api_docs - @override - void writeCharCode(int charCode); - - // ignore: public_member_api_docs - @override - void addError(dynamic error, [StackTrace stackTrace]); - - // ignore: public_member_api_docs - @override - Future addStream(Stream> stream); - - // ignore: public_member_api_docs - Future flush(); - - // ignore: public_member_api_docs - @override - Future close(); - - // ignore: public_member_api_docs - @override - Future get done; -} - -// ignore: public_member_api_docs -abstract class Link implements FileSystemEntity { - // ignore: public_member_api_docs - Future create(String target, {bool recursive: false}); - - // ignore: public_member_api_docs - void createSync(String target, {bool recursive: false}); - - // ignore: public_member_api_docs - void updateSync(String target); - - // ignore: public_member_api_docs - Future update(String target); - - // ignore: public_member_api_docs - @override - Future rename(String newPath); - - // ignore: public_member_api_docs - @override - Link renameSync(String newPath); - - // ignore: public_member_api_docs - @override - Link get absolute; - - // ignore: public_member_api_docs - Future target(); - - // ignore: public_member_api_docs - String targetSync(); -} - -// ignore: public_member_api_docs -class OSError { - // ignore: public_member_api_docs - static const int noErrorCode = -1; - - // ignore: public_member_api_docs - const OSError([this.message = "", this.errorCode = noErrorCode]); - - // ignore: public_member_api_docs - final String message; - - // ignore: public_member_api_docs - final int errorCode; - - @override - String toString() { - StringBuffer sb = new StringBuffer(); - sb.write("OS Error"); - if (message.isNotEmpty) { - sb..write(": ")..write(message); - if (errorCode != noErrorCode) { - sb..write(", errno = ")..write(errorCode.toString()); - } - } else if (errorCode != noErrorCode) { - sb..write(": errno = ")..write(errorCode.toString()); - } - return sb.toString(); - } -} - -// ignore: public_member_api_docs -abstract class RandomAccessFile { - // ignore: public_member_api_docs - Future close(); - - // ignore: public_member_api_docs - void closeSync(); - - // ignore: public_member_api_docs - Future readByte(); - - // ignore: public_member_api_docs - int readByteSync(); - - // ignore: public_member_api_docs - Future> read(int bytes); - - // ignore: public_member_api_docs - List readSync(int bytes); - - // ignore: public_member_api_docs - Future readInto(List buffer, [int start = 0, int end]); - - // ignore: public_member_api_docs - int readIntoSync(List buffer, [int start = 0, int end]); - - // ignore: public_member_api_docs - Future writeByte(int value); - - // ignore: public_member_api_docs - int writeByteSync(int value); - - // ignore: public_member_api_docs - Future writeFrom(List buffer, - [int start = 0, int end]); - - // ignore: public_member_api_docs - void writeFromSync(List buffer, [int start = 0, int end]); - - // ignore: public_member_api_docs - Future writeString(String string, - {Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - void writeStringSync(String string, {Encoding encoding: UTF8}); - - // ignore: public_member_api_docs - Future position(); - - // ignore: public_member_api_docs - int positionSync(); - - // ignore: public_member_api_docs - Future setPosition(int position); - - // ignore: public_member_api_docs - void setPositionSync(int position); - - // ignore: public_member_api_docs - Future truncate(int length); - - // ignore: public_member_api_docs - void truncateSync(int length); - - // ignore: public_member_api_docs - Future length(); - - // ignore: public_member_api_docs - int lengthSync(); - - // ignore: public_member_api_docs - Future flush(); - - // ignore: public_member_api_docs - void flushSync(); - - // ignore: public_member_api_docs - Future lock( - [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]); - - // ignore: public_member_api_docs - void lockSync( - [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]); - - // ignore: public_member_api_docs - Future unlock([int start = 0, int end = -1]); - - // ignore: public_member_api_docs - void unlockSync([int start = 0, int end = -1]); - - // ignore: public_member_api_docs - String get path; -} diff --git a/lib/src/io/shim.dart b/lib/src/io/shim.dart deleted file mode 100644 index 5434d833db186..0000000000000 --- a/lib/src/io/shim.dart +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) 2017, 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. - -/// For internal use only! -/// -/// This exposes the handful of static methods required by the local backend. -/// For browser contexts, these methods will all throw `UnsupportedError`. -/// For VM contexts, they all delegate to the underlying methods in `dart:io`. -export 'shim_internal.dart' if (dart.library.io) 'shim_dart_io.dart'; diff --git a/lib/src/io/shim_dart_io.dart b/lib/src/io/shim_dart_io.dart deleted file mode 100644 index 0c1914a6737eb..0000000000000 --- a/lib/src/io/shim_dart_io.dart +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2017, 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. - -import 'dart:async'; -import 'dart:io' as io; - -/// Creates a new [io.Directory] with the specified [path]. -io.Directory newDirectory(String path) => new io.Directory(path); - -/// Creates a new [io.File] with the specified [path]. -io.File newFile(String path) => new io.File(path); - -/// Creates a new [io.Link] with the specified [path]. -io.Link newLink(String path) => new io.Link(path); - -/// Wraps [io.Directory.systemTemp]. -io.Directory systemTemp() => io.Directory.systemTemp; - -/// Wraps [io.Directory.current]. -io.Directory get currentDirectory => io.Directory.current; - -/// Wraps [io.Directory.current=]. -set currentDirectory(dynamic path) => io.Directory.current = path; - -/// Wraps [io.FileStat.stat]. -Future stat(String path) => io.FileStat.stat(path); - -/// Wraps [io.FileStat.statSync]. -io.FileStat statSync(String path) => io.FileStat.statSync(path); - -/// Wraps [io.FileSystemEntity.identical]. -Future identical(String path1, String path2) => - io.FileSystemEntity.identical(path1, path2); - -/// Wraps [io.FileSystemEntity.identicalSync]. -bool identicalSync(String path1, String path2) => - io.FileSystemEntity.identicalSync(path1, path2); - -/// Wraps [io.FileSystemEntity.isWatchSupported]. -bool get isWatchSupported => io.FileSystemEntity.isWatchSupported; - -/// Wraps [io.FileSystemEntity.type]. -Future type(String path, bool followLinks) => - io.FileSystemEntity.type(path, followLinks: followLinks); - -/// Wraps [io.FileSystemEntity.typeSync]. -io.FileSystemEntityType typeSync(String path, bool followLinks) => - io.FileSystemEntity.typeSync(path, followLinks: followLinks); diff --git a/lib/src/io/shim_internal.dart b/lib/src/io/shim_internal.dart deleted file mode 100644 index 920c7c760c787..0000000000000 --- a/lib/src/io/shim_internal.dart +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2017, 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. - -import 'dart:async'; - -import 'package:file/src/io.dart' as io; - -const String _requiresIOMsg = 'This operation requires the use of dart:io'; -dynamic _requiresIO() => throw new UnsupportedError(_requiresIOMsg); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.Directory newDirectory(_) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.File newFile(_) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.Link newLink(_) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.Directory systemTemp() => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.Directory get currentDirectory => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -set currentDirectory(dynamic _) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -Future stat(String _) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.FileStat statSync(String _) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -Future identical(String _, String __) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -bool identicalSync(String _, String __) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -bool get isWatchSupported => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -Future type(String _, bool __) => _requiresIO(); - -/// Throws [UnsupportedError]; browsers cannot use the `local` library. -io.FileSystemEntityType typeSync(String _, bool __) => _requiresIO(); diff --git a/pubspec.yaml b/pubspec.yaml index 38efe5e5d6681..bbb01b82ba80c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: file -version: 2.3.7 +version: 3.0.0 authors: - Matan Lurey - Yegor Jbanov @@ -16,4 +16,4 @@ dev_dependencies: test: ^0.12.18 environment: - sdk: '>=1.19.0 <2.0.0' + sdk: '>=1.24.0 <2.0.0'