Skip to content

Commit 57a4a6c

Browse files
authored
Add FileSystem.path. (flutter#14)
Remove `FileSystem.pathSeparator`, since it can now be expressed as `fileSystem.path.separator`. Fixes flutter#9
1 parent e4e5372 commit 57a4a6c

14 files changed

+35
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#### 1.0.2
2+
3+
* Improved `toString` implementations in file system entity classes
4+
* Added `ForwardingFileSystem` and associated forwarding classes to the
5+
main `file` library.
6+
* Removed `FileSystem.pathSeparator`, and added a more comprehensive
7+
`FileSystem.path` property.
8+
19
#### 1.0.1
210

311
* Added `FileSystem.systemTempDirectory`

lib/src/backends/chroot/chroot_file_system.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class ChrootFileSystem extends FileSystem {
6969
Link link(path) => new _ChrootLink(this, common.getPath(path));
7070

7171
@override
72-
String get pathSeparator => delegate.pathSeparator;
72+
p.Context get path =>
73+
new p.Context(style: delegate.path.style, current: _cwd);
7374

7475
/// Gets the system temp directory. This directory will be created on-demand
7576
/// in the local root of the file system. Once created, its location is fixed

lib/src/backends/local.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:async';
99
import 'package:file/src/forwarding.dart';
1010
import 'package:file/src/io.dart' as io;
1111
import 'package:file/file.dart';
12+
import 'package:path/path.dart' as p;
1213

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

lib/src/backends/local/local_file_system.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class LocalFileSystem extends FileSystem {
2222
Link link(path) => new _LocalLink(this, shim.newLink(path));
2323

2424
@override
25-
String get pathSeparator => shim.pathSeparator;
25+
p.Context get path => new p.Context();
2626

2727
/// Gets the directory provided by the operating system for creating temporary
2828
/// files and directories in. The location of the system temp directory is

lib/src/backends/memory/memory_directory.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
4949
@override
5050
Directory createTempSync([String prefix]) {
5151
prefix = (prefix ?? '') + 'rand';
52-
String fullPath = fileSystem._context.join(path, prefix);
53-
String dirname = fileSystem._context.dirname(fullPath);
54-
String basename = fileSystem._context.basename(fullPath);
52+
String fullPath = fileSystem.path.join(path, prefix);
53+
String dirname = fileSystem.path.dirname(fullPath);
54+
String basename = fileSystem.path.basename(fullPath);
5555
_DirectoryNode node = fileSystem._findNode(dirname);
5656
_checkExists(node, () => dirname);
5757
_checkIsDir(node, () => dirname);
@@ -62,7 +62,7 @@ class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
6262
_DirectoryNode tempDir = new _DirectoryNode(node);
6363
node.children[name()] = tempDir;
6464
return new _MemoryDirectory(
65-
fileSystem, fileSystem._context.join(dirname, name()));
65+
fileSystem, fileSystem.path.join(dirname, name()));
6666
}
6767

6868
@override
@@ -113,7 +113,7 @@ class _MemoryDirectory extends _MemoryFileSystemEntity implements Directory {
113113
_PendingListTask task = tasks.removeLast();
114114
task.dir.children.forEach((String name, _Node child) {
115115
Set<_LinkNode> breadcrumbs = new Set<_LinkNode>.from(task.breadcrumbs);
116-
String childPath = fileSystem._context.join(task.path, name);
116+
String childPath = fileSystem.path.join(task.path, name);
117117
while (followLinks && _isLink(child) && breadcrumbs.add(child)) {
118118
_Node referent = (child as _LinkNode).referentOrNull;
119119
if (referent != null) {

lib/src/backends/memory/memory_file_system.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class MemoryFileSystem extends FileSystem {
6565
Link link(path) => new _MemoryLink(this, common.getPath(path));
6666

6767
@override
68-
String get pathSeparator => _separator;
68+
p.Context get path => new p.Context(style: p.Style.posix, current: _cwd);
6969

7070
/// Gets the system temp directory. This directory will be created on-demand
7171
/// in the root of the file system. Once created, its location is fixed for
@@ -147,9 +147,6 @@ class MemoryFileSystem extends FileSystem {
147147
return node.type;
148148
}
149149

150-
/// Gets the path context for this file system given the current working dir.
151-
p.Context get _context => new p.Context(style: p.Style.posix, current: _cwd);
152-
153150
/// Gets the node backing for the current working directory. Note that this
154151
/// can return null if the directory has been deleted or moved from under our
155152
/// feet.

lib/src/backends/memory/memory_file_system_entity.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ abstract class _MemoryFileSystemEntity implements FileSystemEntity {
2222
_MemoryFileSystemEntity(this.fileSystem, this.path);
2323

2424
/// Gets the part of this entity's path before the last separator.
25-
String get dirname => fileSystem._context.dirname(path);
25+
String get dirname => fileSystem.path.dirname(path);
2626

2727
/// Gets the part of this entity's path after the last separator.
28-
String get basename => fileSystem._context.basename(path);
28+
String get basename => fileSystem.path.basename(path);
2929

3030
/// Returns the expected type of this entity, which may differ from the type
3131
/// of the node that's found at the path specified by this entity.
@@ -95,7 +95,7 @@ abstract class _MemoryFileSystemEntity implements FileSystemEntity {
9595
if (!_isAbsolute(resolved)) {
9696
resolved = fileSystem._cwd + _separator + resolved;
9797
}
98-
return fileSystem._context.normalize(resolved);
98+
return fileSystem.path.normalize(resolved);
9999
}
100100

101101
@override
@@ -127,7 +127,7 @@ abstract class _MemoryFileSystemEntity implements FileSystemEntity {
127127
FileSystemEntity get absolute {
128128
String absolutePath = path;
129129
if (!_isAbsolute(absolutePath)) {
130-
absolutePath = fileSystem._context.join(fileSystem._cwd, absolutePath);
130+
absolutePath = fileSystem.path.join(fileSystem._cwd, absolutePath);
131131
}
132132
return _clone(absolutePath);
133133
}

lib/src/forwarding.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'dart:convert';
1010
import 'package:file/src/io.dart' as io;
1111
import 'package:file/file.dart';
1212
import 'package:meta/meta.dart';
13+
import 'package:path/path.dart' as p;
1314

1415
part 'forwarding/forwarding_directory.dart';
1516
part 'forwarding/forwarding_file.dart';

lib/src/forwarding/forwarding_file_system.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class ForwardingFileSystem extends FileSystem {
2424
Link link(path) => delegate.link(path);
2525

2626
@override
27-
String get pathSeparator => delegate.pathSeparator;
27+
p.Context get path => delegate.path;
2828

2929
@override
3030
Directory get systemTempDirectory => delegate.systemTempDirectory;

lib/src/interface.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ library file.src.interface;
77
import 'dart:async';
88
import 'dart:convert';
99

10+
import 'package:path/path.dart' as path;
11+
1012
import 'io.dart' as io;
1113

1214
export 'io.dart';

lib/src/interface/file_system.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ abstract class FileSystem {
2828
/// [path] can be either a [`String`], a [`Uri`], or a [`FileSystemEntity`].
2929
Link link(path);
3030

31-
/// Gets the path separator used by this file system to separate components
32-
/// in file paths.
33-
String get pathSeparator;
31+
/// An object for manipulating paths in this file system.
32+
path.Context get path;
3433

3534
/// Gets the system temp directory.
3635
///

lib/src/io/shim_dart_io.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:file/src/common.dart' as common;
1111
io.Directory newDirectory(path) => new io.Directory(common.getPath(path));
1212
io.File newFile(path) => new io.File(common.getPath(path));
1313
io.Link newLink(path) => new io.Link(common.getPath(path));
14-
String get pathSeparator => io.Platform.pathSeparator;
1514
io.Directory systemTemp() => io.Directory.systemTemp;
1615
io.Directory get currentDirectory => io.Directory.current;
1716
set currentDirectory(dynamic path) => io.Directory.current = path;

lib/src/io/shim_internal.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ dynamic _requiresIO() => throw new UnsupportedError(_requiresIOMsg);
1212
io.Directory newDirectory(_) => _requiresIO();
1313
io.File newFile(_) => _requiresIO();
1414
io.Link newLink(_) => _requiresIO();
15-
String get pathSeparator => _requiresIO();
1615
io.Directory systemTemp() => _requiresIO();
1716
io.Directory get currentDirectory => _requiresIO();
1817
set currentDirectory(dynamic _) => _requiresIO();

test/common_tests.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,13 @@ void runCommonTests(
124124
});
125125
});
126126

127-
group('pathSeparator', () {
128-
test('isAmongExpectedValues', () {
129-
expect(fs.pathSeparator, anyOf('/', r'\'));
127+
group('path', () {
128+
test('hasCorrectCurrentWorkingDirectory', () {
129+
expect(fs.path.current, fs.currentDirectory.path);
130+
});
131+
132+
test('separatorIsAmongExpectedValues', () {
133+
expect(fs.path.separator, anyOf('/', r'\'));
130134
});
131135
});
132136

0 commit comments

Comments
 (0)