| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library file_system; | 5 library analyzer.file_system.file_system; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:analyzer/src/util/absolute_path.dart'; |
| 10 import 'package:path/path.dart'; | 11 import 'package:path/path.dart'; |
| 11 import 'package:watcher/watcher.dart'; | 12 import 'package:watcher/watcher.dart'; |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * [File]s are leaf [Resource]s which contain data. | 15 * [File]s are leaf [Resource]s which contain data. |
| 15 */ | 16 */ |
| 16 abstract class File extends Resource { | 17 abstract class File implements Resource { |
| 17 /** | 18 /** |
| 18 * Watch for changes to this file | 19 * Watch for changes to this file |
| 19 */ | 20 */ |
| 20 Stream<WatchEvent> get changes; | 21 Stream<WatchEvent> get changes; |
| 21 | 22 |
| 22 /** | 23 /** |
| 23 * Return the last-modified stamp of the file. | 24 * Return the last-modified stamp of the file. |
| 24 * Throws [FileSystemException] if the file does not exist. | 25 * Throws [FileSystemException] if the file does not exist. |
| 25 */ | 26 */ |
| 26 int get modificationStamp; | 27 int get modificationStamp; |
| 27 | 28 |
| 28 /** | 29 /** |
| 29 * Create a new [Source] instance that serves this file. | 30 * Create a new [Source] instance that serves this file. |
| 30 */ | 31 */ |
| 31 Source createSource([Uri uri]); | 32 Source createSource([Uri uri]); |
| 32 | 33 |
| 33 /** | 34 /** |
| 35 * Synchronously read the entire file contents as a list of bytes. |
| 36 * Throws a [FileSystemException] if the operation fails. |
| 37 */ |
| 38 List<int> readAsBytesSync(); |
| 39 |
| 40 /** |
| 34 * Synchronously read the entire file contents as a [String]. | 41 * Synchronously read the entire file contents as a [String]. |
| 35 * Throws [FileSystemException] if the file does not exist. | 42 * Throws [FileSystemException] if the file does not exist. |
| 36 */ | 43 */ |
| 37 String readAsStringSync(); | 44 String readAsStringSync(); |
| 45 |
| 46 /** |
| 47 * Synchronously rename this file. |
| 48 * Return a [File] instance for the renamed file. |
| 49 * |
| 50 * If [newPath] identifies an existing file, that file is replaced. |
| 51 * If [newPath] identifies an existing resource the operation might fail and |
| 52 * an exception is thrown. |
| 53 */ |
| 54 File renameSync(String newPath); |
| 55 |
| 56 /** |
| 57 * Synchronously write the given [bytes] to the file. The new content will |
| 58 * replace any existing content. |
| 59 * |
| 60 * Throws a [FileSystemException] if the operation fails. |
| 61 */ |
| 62 void writeAsBytesSync(List<int> bytes); |
| 63 |
| 64 /** |
| 65 * Synchronously write the given [content] to the file. The new content will |
| 66 * replace any existing content. |
| 67 * |
| 68 * Throws a [FileSystemException] if the operation fails. |
| 69 */ |
| 70 void writeAsStringSync(String content); |
| 38 } | 71 } |
| 39 | 72 |
| 40 /** | 73 /** |
| 41 * Base class for all file system exceptions. | 74 * Base class for all file system exceptions. |
| 42 */ | 75 */ |
| 43 class FileSystemException implements Exception { | 76 class FileSystemException implements Exception { |
| 44 final String path; | 77 final String path; |
| 45 final String message; | 78 final String message; |
| 46 | 79 |
| 47 FileSystemException(this.path, this.message); | 80 FileSystemException(this.path, this.message); |
| 48 | 81 |
| 82 @override |
| 49 String toString() => 'FileSystemException(path=$path; message=$message)'; | 83 String toString() => 'FileSystemException(path=$path; message=$message)'; |
| 50 } | 84 } |
| 51 | 85 |
| 52 /** | 86 /** |
| 53 * [Folder]s are [Resource]s which may contain files and/or other folders. | 87 * [Folder]s are [Resource]s which may contain files and/or other folders. |
| 54 */ | 88 */ |
| 55 abstract class Folder extends Resource { | 89 abstract class Folder implements Resource { |
| 56 /** | 90 /** |
| 57 * Watch for changes to the files inside this folder (and in any nested | 91 * Watch for changes to the files inside this folder (and in any nested |
| 58 * folders, including folders reachable via links). | 92 * folders, including folders reachable via links). |
| 59 */ | 93 */ |
| 60 Stream<WatchEvent> get changes; | 94 Stream<WatchEvent> get changes; |
| 61 | 95 |
| 62 /** | 96 /** |
| 63 * If the path [path] is a relative path, convert it to an absolute path | 97 * If the path [path] is a relative path, convert it to an absolute path |
| 64 * by interpreting it relative to this folder. If it is already an aboslute | 98 * by interpreting it relative to this folder. If it is already an absolute |
| 65 * path, then don't change it. | 99 * path, then don't change it. |
| 66 * | 100 * |
| 67 * However, regardless of whether [path] is relative or absolute, normalize | 101 * However, regardless of whether [path] is relative or absolute, normalize |
| 68 * it by removing path components of the form '.' or '..'. | 102 * it by removing path components of the form '.' or '..'. |
| 69 */ | 103 */ |
| 70 String canonicalizePath(String path); | 104 String canonicalizePath(String path); |
| 71 | 105 |
| 72 /** | 106 /** |
| 73 * Return `true` if absolute [path] references a resource in this folder. | 107 * Return `true` if absolute [path] references a resource in this folder. |
| 74 */ | 108 */ |
| 75 bool contains(String path); | 109 bool contains(String path); |
| 76 | 110 |
| 77 /** | 111 /** |
| 78 * Return an existing child [Resource] with the given [relPath]. | 112 * Return an existing child [Resource] with the given [relPath]. |
| 79 * Return a not existing [File] if no such child exist. | 113 * Return a not existing [File] if no such child exist. |
| 80 */ | 114 */ |
| 81 Resource getChild(String relPath); | 115 Resource getChild(String relPath); |
| 82 | 116 |
| 83 /** | 117 /** |
| 118 * Return a [File] representing a child [Resource] with the given |
| 119 * [relPath]. This call does not check whether a file with the given name |
| 120 * exists on the filesystem - client must call the [File]'s `exists` getter |
| 121 * to determine whether the folder actually exists. |
| 122 */ |
| 123 File getChildAssumingFile(String relPath); |
| 124 |
| 125 /** |
| 84 * Return a [Folder] representing a child [Resource] with the given | 126 * Return a [Folder] representing a child [Resource] with the given |
| 85 * [relPath]. This call does not check whether a folder with the given name | 127 * [relPath]. This call does not check whether a folder with the given name |
| 86 * exists on the filesystem--client must call the [Folder]'s `exists` getter | 128 * exists on the filesystem--client must call the [Folder]'s `exists` getter |
| 87 * to determine whether the folder actually exists. | 129 * to determine whether the folder actually exists. |
| 88 */ | 130 */ |
| 89 Folder getChildAssumingFolder(String relPath); | 131 Folder getChildAssumingFolder(String relPath); |
| 90 | 132 |
| 91 /** | 133 /** |
| 92 * Return a list of existing direct children [Resource]s (folders and files) | 134 * Return a list of existing direct children [Resource]s (folders and files) |
| 93 * in this folder, in no particular order. | 135 * in this folder, in no particular order. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 115 */ | 157 */ |
| 116 String get path; | 158 String get path; |
| 117 | 159 |
| 118 /** | 160 /** |
| 119 * Return a short version of the name that can be displayed to the user to | 161 * Return a short version of the name that can be displayed to the user to |
| 120 * denote this resource. | 162 * denote this resource. |
| 121 */ | 163 */ |
| 122 String get shortName; | 164 String get shortName; |
| 123 | 165 |
| 124 /** | 166 /** |
| 167 * Synchronously deletes this resource and its children. |
| 168 * |
| 169 * Throws an exception if the resource cannot be deleted. |
| 170 */ |
| 171 void delete(); |
| 172 |
| 173 /** |
| 125 * Return `true` if absolute [path] references this resource or a resource in | 174 * Return `true` if absolute [path] references this resource or a resource in |
| 126 * this folder. | 175 * this folder. |
| 127 */ | 176 */ |
| 128 bool isOrContains(String path); | 177 bool isOrContains(String path); |
| 178 |
| 179 /** |
| 180 * Return a resource that refers to the same resource as this resource, but |
| 181 * whose path does not contain any symbolic links. |
| 182 */ |
| 183 Resource resolveSymbolicLinksSync(); |
| 184 |
| 185 /** |
| 186 * Return a Uri representing this resource. |
| 187 */ |
| 188 Uri toUri(); |
| 129 } | 189 } |
| 130 | 190 |
| 131 /** | 191 /** |
| 132 * Instances of the class [ResourceProvider] convert [String] paths into | 192 * Instances of the class [ResourceProvider] convert [String] paths into |
| 133 * [Resource]s. | 193 * [Resource]s. |
| 134 */ | 194 */ |
| 135 abstract class ResourceProvider { | 195 abstract class ResourceProvider { |
| 136 /** | 196 /** |
| 197 * Get the absolute path context used by this resource provider. |
| 198 */ |
| 199 AbsolutePathContext get absolutePathContext; |
| 200 |
| 201 /** |
| 137 * Get the path context used by this resource provider. | 202 * Get the path context used by this resource provider. |
| 138 */ | 203 */ |
| 139 Context get pathContext; | 204 Context get pathContext; |
| 140 | 205 |
| 141 /** | 206 /** |
| 142 * Return a [File] that corresponds to the given [path]. | 207 * Return a [File] that corresponds to the given [path]. |
| 143 * | 208 * |
| 144 * A file may or may not exist at this location. | 209 * A file may or may not exist at this location. |
| 145 */ | 210 */ |
| 146 File getFile(String path); | 211 File getFile(String path); |
| 147 | 212 |
| 148 /** | 213 /** |
| 149 * Return a [Folder] that corresponds to the given [path]. | 214 * Return a [Folder] that corresponds to the given [path]. |
| 150 * | 215 * |
| 151 * A folder may or may not exist at this location. | 216 * A folder may or may not exist at this location. |
| 152 */ | 217 */ |
| 153 Folder getFolder(String path); | 218 Folder getFolder(String path); |
| 154 | 219 |
| 155 /** | 220 /** |
| 221 * Complete with a list of modification times for the given [sources]. |
| 222 * |
| 223 * If the file of a source is not managed by this provider, return `null`. |
| 224 * If the file a source does not exist, return `-1`. |
| 225 */ |
| 226 Future<List<int>> getModificationTimes(List<Source> sources); |
| 227 |
| 228 /** |
| 156 * Return the [Resource] that corresponds to the given [path]. | 229 * Return the [Resource] that corresponds to the given [path]. |
| 157 */ | 230 */ |
| 158 Resource getResource(String path); | 231 Resource getResource(String path); |
| 159 | 232 |
| 160 /** | 233 /** |
| 161 * Return the folder in which the plugin with the given [pluginId] can store | 234 * Return the folder in which the plugin with the given [pluginId] can store |
| 162 * state that will persist across sessions. The folder returned for a given id | 235 * state that will persist across sessions. The folder returned for a given id |
| 163 * will not be returned for a different id, ensuring that plugins do not need | 236 * will not be returned for a different id, ensuring that plugins do not need |
| 164 * to be concerned with file name collisions with other plugins, assuming that | 237 * to be concerned with file name collisions with other plugins, assuming that |
| 165 * the plugin ids are unique. The plugin ids must be valid folder names. | 238 * the plugin ids are unique. The plugin ids must be valid folder names. |
| 166 */ | 239 */ |
| 167 Folder getStateLocation(String pluginId); | 240 Folder getStateLocation(String pluginId); |
| 168 } | 241 } |
| 169 | 242 |
| 170 /** | 243 /** |
| 171 * A [UriResolver] for [Resource]s. | 244 * A [UriResolver] for [Resource]s. |
| 172 */ | 245 */ |
| 173 class ResourceUriResolver extends UriResolver { | 246 class ResourceUriResolver extends UriResolver { |
| 174 /** | 247 /** |
| 175 * The name of the `file` scheme. | 248 * The name of the `file` scheme. |
| 176 */ | 249 */ |
| 177 static String _FILE_SCHEME = "file"; | 250 static final String FILE_SCHEME = "file"; |
| 178 | 251 |
| 179 final ResourceProvider _provider; | 252 final ResourceProvider _provider; |
| 180 | 253 |
| 181 ResourceUriResolver(this._provider); | 254 ResourceUriResolver(this._provider); |
| 182 | 255 |
| 256 ResourceProvider get provider => _provider; |
| 257 |
| 183 @override | 258 @override |
| 184 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 259 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 185 if (!_isFileUri(uri)) { | 260 if (!isFileUri(uri)) { |
| 186 return null; | 261 return null; |
| 187 } | 262 } |
| 188 Resource resource = | 263 Resource resource = |
| 189 _provider.getResource(_provider.pathContext.fromUri(uri)); | 264 _provider.getResource(_provider.pathContext.fromUri(uri)); |
| 190 if (resource is File) { | 265 if (resource is File) { |
| 191 return resource.createSource(actualUri != null ? actualUri : uri); | 266 return resource.createSource(actualUri ?? uri); |
| 192 } | 267 } |
| 193 return null; | 268 return null; |
| 194 } | 269 } |
| 195 | 270 |
| 196 @override | 271 @override |
| 197 Uri restoreAbsolute(Source source) => | 272 Uri restoreAbsolute(Source source) => |
| 198 _provider.pathContext.toUri(source.fullName); | 273 _provider.pathContext.toUri(source.fullName); |
| 199 | 274 |
| 200 /** | 275 /** |
| 201 * Return `true` if the given [uri] is a `file` URI. | 276 * Return `true` if the given [uri] is a `file` URI. |
| 202 */ | 277 */ |
| 203 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; | 278 static bool isFileUri(Uri uri) => uri.scheme == FILE_SCHEME; |
| 204 } | 279 } |
| OLD | NEW |