Add FileSystemEntity.absolutePath and .isAbsolute properties.

BUG=
R=sgjesse@google.com

Review URL: https://codereview.chromium.org//23468027

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@27467 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
whesse@google.com 2013-09-13 08:56:16 +00:00
parent d6e339eb11
commit ca27ddeafe
7 changed files with 138 additions and 0 deletions

View file

@ -108,6 +108,15 @@ abstract class Directory implements FileSystemEntity {
*/
Directory renameSync(String newPath);
/**
* Returns a [Directory] instance whose path is the absolute path to [this].
*
* The absolute path is computed by prefixing
* a relative path with the current working directory, and returning
* an absolute path unchanged.
*/
Directory get absolute;
/**
* Lists the sub-directories and files of this [Directory].
* Optionally recurses into sub-directories.

View file

@ -74,6 +74,8 @@ class _Directory extends FileSystemEntity implements Directory {
return (result == 1);
}
Directory get absolute => new Directory(_absolutePath);
Future<FileStat> stat() => FileStat.stat(path);
FileStat statSync() => FileStat.statSync(path);

View file

@ -98,6 +98,15 @@ abstract class File implements FileSystemEntity {
*/
int lengthSync();
/**
* Returns a [File] instance whose path is the absolute path to [this].
*
* The absolute path is computed by prefixing
* a relative path with the current working directory, and returning
* an absolute path unchanged.
*/
File get absolute;
/**
* Get the last-modified time of the file. Returns a
* [:Future<DateTime>:] that completes with a [DateTime] object for the

View file

@ -267,6 +267,8 @@ class _File extends FileSystemEntity implements File {
return result;
}
File get absolute => new File(_absolutePath);
Future<FileStat> stat() => FileStat.stat(path);
FileStat statSync() => FileStat.statSync(path);

View file

@ -343,6 +343,45 @@ abstract class FileSystemEntity {
});
}
static final RegExp _absoluteWindowsPathPattern =
new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])');
/**
* Returns a [bool] indicating whether this object's path is absolute.
*
* On Windows, a path is absolute if it starts with \\ or a drive letter
* between a and z (upper or lower case) followed by :\ or :/.
* On non-Windows, a path is absolute if it starts with /.
*/
bool get isAbsolute {
if (Platform.isWindows) {
return path.startsWith(_absoluteWindowsPathPattern);
} else {
return path.startsWith('/');
}
}
/**
* Returns a [FileSystemEntity] whose path is the absolute path to [this].
* The type of the returned instance is the type of [this].
*
* The absolute path is computed by prefixing
* a relative path with the current working directory, and returning
* an absolute path unchanged.
*/
FileSystemEntity get absolute;
String get _absolutePath {
if (isAbsolute) return path;
String current = Directory.current.path;
if (current.endsWith('/') ||
(Platform.isWindows && current.endsWith('\\'))) {
return '$current$path';
} else {
return '$current${Platform.pathSeparator}$path';
}
}
/**
* Synchronously checks whether two paths refer to the same object in the

View file

@ -84,6 +84,15 @@ abstract class Link implements FileSystemEntity {
*/
Link renameSync(String newPath);
/**
* Returns a [Link] instance whose path is the absolute path to [this].
*
* The absolute path is computed by prefixing
* a relative path with the current working directory, and returning
* an absolute path unchanged.
*/
Link get absolute;
/**
* Gets the target of the link. Returns a future that completes with
* the path to the target.
@ -127,6 +136,8 @@ class _Link extends FileSystemEntity implements Link {
bool existsSync() => FileSystemEntity.isLinkSync(path);
Link get absolute => new Link(_absolutePath);
Future<FileStat> stat() => FileStat.stat(path);
FileStat statSync() => FileStat.statSync(path);

View file

@ -0,0 +1,66 @@
// Copyright (c) 2013, 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.
//
// Dart test program for testing FileSystemEntity.absolute
import "package:expect/expect.dart";
import 'dart:io';
main() {
if (Platform.isWindows) {
testWindows();
try {
Directory.current = 'C:\\';
} catch (e) {
return;
}
testWindows();
} else {
testPosix();
Directory.current = '.';
testPosix();
Directory.current = '/';
testPosix();
}
}
testWindows() {
String current = Directory.current.path;
for (String relative in ['abd', '..', '.', 'efg/hij', 'abc/']) {
if (current.endsWith('\\')) {
Expect.equals(new File(relative).absolute.path, '$current$relative');
} else {
Expect.equals(new File(relative).absolute.path, '$current\\$relative');
}
Expect.isTrue(new File(relative).absolute.isAbsolute);
}
for (String absolute in ['c:/abd', 'D:\\rf', '\\\\a_share\\folder',
'\\\\?\\c:\\prefixed\path\\']) {
Expect.isTrue(new File(absolute).absolute.path == absolute);
Expect.isTrue(new File(absolute).absolute.isAbsolute);
}
}
testPosix() {
String current = Directory.current.path;
print(Directory.current.path);
for (String relative in ['abd', '..', '.', 'efg/hij', 'abc/']) {
if (current.endsWith('/')) {
Expect.equals(new File(relative).absolute.path, '$current$relative');
} else {
Expect.equals(new File(relative).absolute.path, '$current/$relative');
}
Expect.isTrue(new File(relative).absolute.isAbsolute);
Expect.equals(new Directory(relative).absolute.path,
new Link(relative).absolute.path);
Expect.isTrue(new File(relative).absolute is File);
Expect.isTrue(new Directory(relative).absolute is Directory);
Expect.isTrue(new Link(relative).absolute is Link);
}
for (String absolute in ['/abd', '/', '/./..\\', '/efg/hij', '/abc/']) {
Expect.equals(new File(absolute).absolute.path, absolute);
Expect.isTrue(new File(absolute).absolute.isAbsolute);
}
}