Skip to content

Commit 4b48927

Browse files
authored
Support browser contexts in package:file (flutter#18)
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.
1 parent f289ad5 commit 4b48927

17 files changed

+370
-52
lines changed

.analysis_options

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
analyzer:
22
strong-mode: true
3+
enableConditionalDirectives: true

lib/file.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
export 'src/interface/directory.dart';
2-
export 'src/interface/file_system_entity.dart';
3-
export 'src/interface/file_system.dart';
4-
export 'src/interface/file.dart';
5-
export 'src/interface/link.dart';
1+
export 'src/interface.dart';

lib/src/backends/local.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ library file.src.backends.local;
22

33
import 'dart:async';
44
import 'dart:convert';
5-
import 'dart:io' as io;
65

6+
import 'package:file/src/io.dart' as io;
77
import 'package:file/file.dart';
88

9+
import '../io/shim.dart' as shim;
10+
911
part 'local/local_directory.dart';
1012
part 'local/local_file.dart';
1113
part 'local/local_file_system.dart';

lib/src/backends/local/local_file_system.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,42 @@ class LocalFileSystem extends FileSystem {
99

1010
@override
1111
Directory directory(String path) =>
12-
new _LocalDirectory(this, new io.Directory(path));
12+
new _LocalDirectory(this, shim.newDirectory(path));
1313

1414
@override
15-
File file(String path) => new _LocalFile(this, new io.File(path));
15+
File file(String path) => new _LocalFile(this, shim.newFile(path));
1616

1717
@override
18-
Link link(String path) => new _LocalLink(this, new io.Link(path));
18+
Link link(String path) => new _LocalLink(this, shim.newLink(path));
1919

2020
@override
21-
Directory get currentDirectory => directory(io.Directory.current.path);
21+
Directory get currentDirectory => directory(shim.currentDirectory.path);
2222

2323
@override
24-
set currentDirectory(dynamic path) => io.Directory.current = path;
24+
set currentDirectory(dynamic path) => shim.currentDirectory = path;
2525

2626
@override
27-
Future<io.FileStat> stat(String path) => io.FileStat.stat(path);
27+
Future<io.FileStat> stat(String path) => shim.stat(path);
2828

2929
@override
30-
io.FileStat statSync(String path) => io.FileStat.statSync(path);
30+
io.FileStat statSync(String path) => shim.statSync(path);
3131

3232
@override
3333
Future<bool> identical(String path1, String path2) =>
34-
io.FileSystemEntity.identical(path1, path2);
34+
shim.identical(path1, path2);
3535

3636
@override
3737
bool identicalSync(String path1, String path2) =>
38-
io.FileSystemEntity.identicalSync(path1, path2);
38+
shim.identicalSync(path1, path2);
3939

4040
@override
41-
bool get isWatchSupported => io.FileSystemEntity.isWatchSupported;
41+
bool get isWatchSupported => shim.isWatchSupported;
4242

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

4747
@override
4848
io.FileSystemEntityType typeSync(String path, {bool followLinks: true}) =>
49-
io.FileSystemEntity.typeSync(path, followLinks: followLinks);
49+
shim.typeSync(path, followLinks);
5050
}

lib/src/backends/memory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ library file.src.backends.memory;
22

33
import 'dart:async';
44
import 'dart:convert';
5-
import 'dart:io' as io;
65

76
import 'package:file/file.dart';
7+
import 'package:file/src/io.dart' as io;
88
import 'package:path/path.dart' as p;
99

1010
part 'memory/memory_directory.dart';

lib/src/interface.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
library file.src.interface;
2+
3+
import 'dart:async';
4+
5+
import 'io.dart' as io;
6+
7+
export 'io.dart';
8+
9+
part 'interface/directory.dart';
10+
part 'interface/file.dart';
11+
part 'interface/file_system.dart';
12+
part 'interface/file_system_entity.dart';
13+
part 'interface/link.dart';

lib/src/interface/directory.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'dart:io' as io;
2-
3-
import 'file_system_entity.dart';
1+
part of file.src.interface;
42

53
/// A reference to a directory on the file system.
64
abstract class Directory implements FileSystemEntity, io.Directory {}

lib/src/interface/file.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'dart:io' as io;
2-
3-
import 'file_system_entity.dart';
1+
part of file.src.interface;
42

53
/// A reference to a file on the file system.
64
abstract class File implements FileSystemEntity, io.File {}

lib/src/interface/file_system.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import 'dart:async';
2-
import 'dart:io' as io;
3-
4-
import 'directory.dart';
5-
import 'file.dart';
6-
import 'link.dart';
1+
part of file.src.interface;
72

83
/// A generic representation of a file system.
94
///

lib/src/interface/file_system_entity.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'dart:io' as io;
2-
3-
import 'file_system.dart';
1+
part of file.src.interface;
42

53
/// The common super class for [File], [Directory], and [Link] objects.
64
abstract class FileSystemEntity implements io.FileSystemEntity {

lib/src/interface/link.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import 'dart:io' as io;
2-
3-
import 'file_system_entity.dart';
1+
part of file.src.interface;
42

53
/// A reference to a symbolic link on the file system.
64
abstract class Link implements FileSystemEntity, io.Link {}

lib/src/io.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// For internal use only!
2+
///
3+
/// This exposes the subset of the `dart:io` interfaces that are required by
4+
/// the `file` package. The `file` package re-exports these interfaces (or in
5+
/// some cases, implementations of these interfaces by the same name), so this
6+
/// file need not be exposes publicly and exists for internal use only.
7+
///
8+
/// For VM users, this exports the actual `dart:io` interfaces; for browser
9+
/// contexts, this exports locally-defined versions of those same interfaces.
10+
export 'io/interface.dart' if (dart.library.io) 'dart:io'
11+
show
12+
Directory,
13+
File,
14+
FileLock,
15+
FileMode,
16+
FileStat,
17+
FileSystemEntity,
18+
FileSystemEntityType,
19+
FileSystemEvent,
20+
FileSystemException,
21+
IOException,
22+
IOSink,
23+
Link,
24+
OSError,
25+
RandomAccessFile;

0 commit comments

Comments
 (0)