Skip to content

Commit

Permalink
Support browser contexts in package:file (flutter#18)
Browse files Browse the repository at this point in the history
This change uses conditional imports to support browser-based
Dart usage of this package. For non-VM users, this will export
the subset of the dart:io interface that is required by this
package, whereas VM users will simply get dart:io itself.
  • Loading branch information
tvolkert authored Dec 14, 2016
1 parent f289ad5 commit 4b48927
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 52 deletions.
1 change: 1 addition & 0 deletions .analysis_options
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
analyzer:
strong-mode: true
enableConditionalDirectives: true
6 changes: 1 addition & 5 deletions lib/file.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export 'src/interface/directory.dart';
export 'src/interface/file_system_entity.dart';
export 'src/interface/file_system.dart';
export 'src/interface/file.dart';
export 'src/interface/link.dart';
export 'src/interface.dart';
4 changes: 3 additions & 1 deletion lib/src/backends/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ library file.src.backends.local;

import 'dart:async';
import 'dart:convert';
import 'dart:io' as io;

import 'package:file/src/io.dart' as io;
import 'package:file/file.dart';

import '../io/shim.dart' as shim;

part 'local/local_directory.dart';
part 'local/local_file.dart';
part 'local/local_file_system.dart';
Expand Down
24 changes: 12 additions & 12 deletions lib/src/backends/local/local_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ class LocalFileSystem extends FileSystem {

@override
Directory directory(String path) =>
new _LocalDirectory(this, new io.Directory(path));
new _LocalDirectory(this, shim.newDirectory(path));

@override
File file(String path) => new _LocalFile(this, new io.File(path));
File file(String path) => new _LocalFile(this, shim.newFile(path));

@override
Link link(String path) => new _LocalLink(this, new io.Link(path));
Link link(String path) => new _LocalLink(this, shim.newLink(path));

@override
Directory get currentDirectory => directory(io.Directory.current.path);
Directory get currentDirectory => directory(shim.currentDirectory.path);

@override
set currentDirectory(dynamic path) => io.Directory.current = path;
set currentDirectory(dynamic path) => shim.currentDirectory = path;

@override
Future<io.FileStat> stat(String path) => io.FileStat.stat(path);
Future<io.FileStat> stat(String path) => shim.stat(path);

@override
io.FileStat statSync(String path) => io.FileStat.statSync(path);
io.FileStat statSync(String path) => shim.statSync(path);

@override
Future<bool> identical(String path1, String path2) =>
io.FileSystemEntity.identical(path1, path2);
shim.identical(path1, path2);

@override
bool identicalSync(String path1, String path2) =>
io.FileSystemEntity.identicalSync(path1, path2);
shim.identicalSync(path1, path2);

@override
bool get isWatchSupported => io.FileSystemEntity.isWatchSupported;
bool get isWatchSupported => shim.isWatchSupported;

@override
Future<io.FileSystemEntityType> type(String path, {bool followLinks: true}) =>
io.FileSystemEntity.type(path, followLinks: followLinks);
shim.type(path, followLinks);

@override
io.FileSystemEntityType typeSync(String path, {bool followLinks: true}) =>
io.FileSystemEntity.typeSync(path, followLinks: followLinks);
shim.typeSync(path, followLinks);
}
2 changes: 1 addition & 1 deletion lib/src/backends/memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ library file.src.backends.memory;

import 'dart:async';
import 'dart:convert';
import 'dart:io' as io;

import 'package:file/file.dart';
import 'package:file/src/io.dart' as io;
import 'package:path/path.dart' as p;

part 'memory/memory_directory.dart';
Expand Down
13 changes: 13 additions & 0 deletions lib/src/interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library file.src.interface;

import 'dart:async';

import 'io.dart' as io;

export 'io.dart';

part 'interface/directory.dart';
part 'interface/file.dart';
part 'interface/file_system.dart';
part 'interface/file_system_entity.dart';
part 'interface/link.dart';
4 changes: 1 addition & 3 deletions lib/src/interface/directory.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io' as io;

import 'file_system_entity.dart';
part of file.src.interface;

/// A reference to a directory on the file system.
abstract class Directory implements FileSystemEntity, io.Directory {}
4 changes: 1 addition & 3 deletions lib/src/interface/file.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io' as io;

import 'file_system_entity.dart';
part of file.src.interface;

/// A reference to a file on the file system.
abstract class File implements FileSystemEntity, io.File {}
7 changes: 1 addition & 6 deletions lib/src/interface/file_system.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import 'dart:async';
import 'dart:io' as io;

import 'directory.dart';
import 'file.dart';
import 'link.dart';
part of file.src.interface;

/// A generic representation of a file system.
///
Expand Down
4 changes: 1 addition & 3 deletions lib/src/interface/file_system_entity.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io' as io;

import 'file_system.dart';
part of file.src.interface;

/// The common super class for [File], [Directory], and [Link] objects.
abstract class FileSystemEntity implements io.FileSystemEntity {
Expand Down
4 changes: 1 addition & 3 deletions lib/src/interface/link.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io' as io;

import 'file_system_entity.dart';
part of file.src.interface;

/// A reference to a symbolic link on the file system.
abstract class Link implements FileSystemEntity, io.Link {}
25 changes: 25 additions & 0 deletions lib/src/io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// For internal use only!
///
/// This exposes the subset of the `dart:io` interfaces that are required by
/// 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'
show
Directory,
File,
FileLock,
FileMode,
FileStat,
FileSystemEntity,
FileSystemEntityType,
FileSystemEvent,
FileSystemException,
IOException,
IOSink,
Link,
OSError,
RandomAccessFile;
Loading

0 comments on commit 4b48927

Please sign in to comment.