Skip to content

Commit

Permalink
Add uri getter on file system entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
lrhn committed Apr 17, 2015
1 parent 9f56280 commit 8cf32dc
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sdk/lib/core/uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class Uri {
* If the path passed is not a legal file path [ArgumentError] is thrown.
*/
factory Uri.file(String path, {bool windows}) {
windows = windows == null ? Uri._isWindows : windows;
windows = (windows == null) ? Uri._isWindows : windows;
return windows ? _makeWindowsFileUrl(path) : _makeFileUri(path);
}

Expand Down
15 changes: 12 additions & 3 deletions sdk/lib/io/directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ part of dart.io;
* print(directory.path);
* });
* }
*
*
* ## List a directory
*
* Use the [list] or [listSync] methods to get the files and directories
Expand Down Expand Up @@ -100,7 +100,7 @@ part of dart.io;
* ## Other resources
*
* * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directories-and-symlinks)
* provides additional task-oriented code samples that show how to use
* provides additional task-oriented code samples that show how to use
* various API from the Directory class and the related [File] class.
*
* * [I/O for Command-Line Apps](https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-dartio---file-and-socket-io-for-command-line-apps)
Expand All @@ -111,7 +111,7 @@ part of dart.io;
* a tutorial about writing command-line apps, includes information
* about files and directories.
*/
abstract class Directory extends FileSystemEntity {
abstract class Directory implements FileSystemEntity {
/**
* Gets the path of this directory.
*/
Expand Down Expand Up @@ -141,6 +141,15 @@ abstract class Directory extends FileSystemEntity {
*/
static Directory get current => _Directory.current;

/**
* Returns a [Uri] representing the directory's location.
*
* The returned URI's scheme is always "file" if the entity's [path] is
* absolute, otherwise the scheme will be empty.
* The returned URI's path always ends in a slash ('/').
*/
Uri get uri;

/**
* Sets the current working directory of the Dart process including
* all running isolates. The new value set can be either a [Directory]
Expand Down
8 changes: 8 additions & 0 deletions sdk/lib/io/directory_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class _Directory extends FileSystemEntity implements Directory {
}
}

Uri get uri {
String path = this.path;
if (!path.endsWith("/")) {
path = path + "/";
}
return new Uri.file(path);
}

Future<bool> exists() {
return _IOService._dispatch(_DIRECTORY_EXISTS, [path]).then((response) {
if (_isErrorResponse(response)) {
Expand Down
10 changes: 5 additions & 5 deletions sdk/lib/io/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ enum FileLock {
* You might want to use a stream to read large files,
* to manipulate the data with tranformers,
* or for compatibility with another API, such as [WebSocket]s.
*
*
* import 'dart:io';
* import 'dart:convert';
* import 'dart:async';
Expand Down Expand Up @@ -136,7 +136,7 @@ enum FileLock {
* Be sure to close the file with the [close] method.
*
* import 'dart:io';
*
*
* void main() {
* var file = new File('file.txt');
* var sink = file.openWrite();
Expand All @@ -158,7 +158,7 @@ enum FileLock {
*
* main() {
* final file = new File('file.txt');
*
*
* file.length().then((len) {
* print(len);
* });
Expand All @@ -170,7 +170,7 @@ enum FileLock {
* ## Other resources
*
* * [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directories-and-symlinks)
* provides additional task-oriented code samples that show how to use
* provides additional task-oriented code samples that show how to use
* various API from the Directory class and the related [File] class.
*
* * [I/O for Command-Line Apps](https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-dartio---file-and-socket-io-for-command-line-apps)
Expand All @@ -182,7 +182,7 @@ enum FileLock {
* about files and directories.
*/
abstract class File extends FileSystemEntity {
abstract class File implements FileSystemEntity {
/**
* Creates a [File] object.
*
Expand Down
10 changes: 9 additions & 1 deletion sdk/lib/io/file_system_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ FileStat: type $type
* ## Other resources
*
* [Dart by Example](https://www.dartlang.org/dart-by-example/#files-directories-and-symlinks)
* provides additional task-oriented code samples that show how to use
* provides additional task-oriented code samples that show how to use
* various API from the [Directory] class and the [File] class,
* both subclasses of FileSystemEntity.
*
Expand All @@ -214,6 +214,14 @@ FileStat: type $type
abstract class FileSystemEntity {
String get path;

/**
* Returns a [Uri] representing the file system entity's location.
*
* The returned URI's scheme is always "file" if the entity's [path] is
* absolute, otherwise the scheme will be empty.
*/
Uri get uri => new Uri.file(path);

/**
* Checks whether the file system entity with this path exists. Returns
* a [:Future<bool>:] that completes with the result.
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/io/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part of dart.io;
* [Link] objects are references to filesystem links.
*
*/
abstract class Link extends FileSystemEntity {
abstract class Link implements FileSystemEntity {
/**
* Creates a Link object.
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/standalone/io/file_system_uri_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2015, 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 "package:expect/expect.dart";
import "dart:io";

testFile(String input) {
if (Platform.isWindows) {
input = input.replaceAll('/', '\\');
}
var file = new File(input);
var uri = file.uri;
var file2 = new File.fromUri(uri);
Expect.equals(file.path, file2.path, input);
}

testDirectory(String input, [String output]) {
if (output == null) output = input;
if (Platform.isWindows) {
input = input.replaceAll('/', '\\');
output = output.replaceAll('/', '\\');
}
var dir = new Directory(input);
var uri = dir.uri;
var dir2 = new Directory.fromUri(uri);
Expect.equals(output, dir2.path, input);
}

void main() {
testFile("");
testFile("/");
testFile("foo/bar");
testFile("/foo/bar");
testFile("/foo/bar/");

testDirectory("", "/");
testDirectory("/");
testDirectory("foo/bar", "foo/bar/");
testDirectory("/foo/bar", "/foo/bar/");
testDirectory("/foo/bar/");
}

0 comments on commit 8cf32dc

Please sign in to comment.