OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library physical_file_system; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io' as io; |
| 9 |
| 10 import 'package:analyzer/src/generated/java_io.dart'; |
| 11 import 'package:analyzer/src/generated/source_io.dart'; |
| 12 import 'package:path/path.dart'; |
| 13 import 'package:watcher/watcher.dart'; |
| 14 |
| 15 import 'file_system.dart'; |
| 16 |
| 17 |
| 18 /** |
| 19 * A `dart:io` based implementation of [ResourceProvider]. |
| 20 */ |
| 21 class PhysicalResourceProvider implements ResourceProvider { |
| 22 static final PhysicalResourceProvider INSTANCE = |
| 23 new PhysicalResourceProvider._(); |
| 24 |
| 25 PhysicalResourceProvider._(); |
| 26 |
| 27 @override |
| 28 Context get pathContext => io.Platform.isWindows ? windows : posix; |
| 29 |
| 30 @override |
| 31 Resource getResource(String path) { |
| 32 if (io.FileSystemEntity.isDirectorySync(path)) { |
| 33 io.Directory directory = new io.Directory(path); |
| 34 return new _PhysicalFolder(directory); |
| 35 } else { |
| 36 io.File file = new io.File(path); |
| 37 return new _PhysicalFile(file); |
| 38 } |
| 39 } |
| 40 } |
| 41 |
| 42 |
| 43 /** |
| 44 * A `dart:io` based implementation of [File]. |
| 45 */ |
| 46 class _PhysicalFile extends _PhysicalResource implements File { |
| 47 _PhysicalFile(io.File file) : super(file); |
| 48 |
| 49 @override |
| 50 Source createSource([Uri uri]) { |
| 51 io.File file = _entry as io.File; |
| 52 JavaFile javaFile = new JavaFile(file.absolute.path); |
| 53 if (uri == null) { |
| 54 uri = javaFile.toURI(); |
| 55 } |
| 56 return new FileBasedSource.con2(uri, javaFile); |
| 57 } |
| 58 |
| 59 @override |
| 60 bool isOrContains(String path) { |
| 61 return path == this.path; |
| 62 } |
| 63 } |
| 64 |
| 65 |
| 66 /** |
| 67 * A `dart:io` based implementation of [Folder]. |
| 68 */ |
| 69 class _PhysicalFolder extends _PhysicalResource implements Folder { |
| 70 _PhysicalFolder(io.Directory directory) : super(directory); |
| 71 |
| 72 @override |
| 73 Stream<WatchEvent> get changes => new DirectoryWatcher(_entry.path).events; |
| 74 |
| 75 @override |
| 76 String canonicalizePath(String relPath) { |
| 77 return normalize(join(_entry.absolute.path, relPath)); |
| 78 } |
| 79 |
| 80 @override |
| 81 bool contains(String path) { |
| 82 return isWithin(this.path, path); |
| 83 } |
| 84 |
| 85 @override |
| 86 Resource getChild(String relPath) { |
| 87 String canonicalPath = canonicalizePath(relPath); |
| 88 return PhysicalResourceProvider.INSTANCE.getResource(canonicalPath); |
| 89 } |
| 90 |
| 91 @override |
| 92 List<Resource> getChildren() { |
| 93 List<Resource> children = <Resource>[]; |
| 94 io.Directory directory = _entry as io.Directory; |
| 95 List<io.FileSystemEntity> entries = directory.listSync(recursive: false); |
| 96 int numEntries = entries.length; |
| 97 for (int i = 0; i < numEntries; i++) { |
| 98 io.FileSystemEntity entity = entries[i]; |
| 99 if (entity is io.Directory) { |
| 100 children.add(new _PhysicalFolder(entity)); |
| 101 } else if (entity is io.File) { |
| 102 children.add(new _PhysicalFile(entity)); |
| 103 } |
| 104 } |
| 105 return children; |
| 106 } |
| 107 |
| 108 @override |
| 109 bool isOrContains(String path) { |
| 110 if (path == this.path) { |
| 111 return true; |
| 112 } |
| 113 return contains(path); |
| 114 } |
| 115 } |
| 116 |
| 117 |
| 118 /** |
| 119 * A `dart:io` based implementation of [Resource]. |
| 120 */ |
| 121 abstract class _PhysicalResource implements Resource { |
| 122 final io.FileSystemEntity _entry; |
| 123 |
| 124 _PhysicalResource(this._entry); |
| 125 |
| 126 @override |
| 127 bool get exists => _entry.existsSync(); |
| 128 |
| 129 @override |
| 130 get hashCode => path.hashCode; |
| 131 |
| 132 @override |
| 133 Folder get parent { |
| 134 String parentPath = dirname(path); |
| 135 if (parentPath == path) { |
| 136 return null; |
| 137 } |
| 138 return new _PhysicalFolder(new io.Directory(parentPath)); |
| 139 } |
| 140 |
| 141 @override |
| 142 String get path => _entry.absolute.path; |
| 143 |
| 144 @override |
| 145 String get shortName => basename(path); |
| 146 |
| 147 @override |
| 148 bool operator ==(other) { |
| 149 if (runtimeType != other.runtimeType) { |
| 150 return false; |
| 151 } |
| 152 return path == other.path; |
| 153 } |
| 154 |
| 155 @override |
| 156 String toString() => path; |
| 157 } |
OLD | NEW |