| 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 file_system; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:path/path.dart'; |
| 11 import 'package:watcher/watcher.dart'; |
| 12 |
| 13 |
| 14 /** |
| 15 * [File]s are leaf [Resource]s which contain data. |
| 16 */ |
| 17 abstract class File extends Resource { |
| 18 /** |
| 19 * Create a new [Source] instance that serves this file. |
| 20 */ |
| 21 Source createSource(UriKind uriKind); |
| 22 } |
| 23 |
| 24 |
| 25 /** |
| 26 * [Folder]s are [Resource]s which may contain files and/or other folders. |
| 27 */ |
| 28 abstract class Folder extends Resource { |
| 29 /** |
| 30 * Watch for changes to the files inside this folder (and in any nested |
| 31 * folders, including folders reachable via links). |
| 32 */ |
| 33 Stream<WatchEvent> get changes; |
| 34 |
| 35 /** |
| 36 * If the path [path] is a relative path, convert it to an absolute path |
| 37 * by interpreting it relative to this folder. If it is already an aboslute |
| 38 * path, then don't change it. |
| 39 * |
| 40 * However, regardless of whether [path] is relative or absolute, normalize |
| 41 * it by removing path components of the form '.' or '..'. |
| 42 */ |
| 43 String canonicalizePath(String path); |
| 44 |
| 45 /** |
| 46 * Return an existing child [Resource] with the given [relPath]. |
| 47 * Return a not existing [File] if no such child exist. |
| 48 */ |
| 49 Resource getChild(String relPath); |
| 50 |
| 51 /** |
| 52 * Return a list of existing direct children [Resource]s (folders and files) |
| 53 * in this folder, in no particular order. |
| 54 */ |
| 55 List<Resource> getChildren(); |
| 56 } |
| 57 |
| 58 |
| 59 /** |
| 60 * The abstract class [Resource] is an abstraction of file or folder. |
| 61 */ |
| 62 abstract class Resource { |
| 63 /** |
| 64 * Return `true` if this resource exists. |
| 65 */ |
| 66 bool get exists; |
| 67 |
| 68 /** |
| 69 * Return the [Folder] that contains this resource, or `null` if this resource |
| 70 * is a root folder. |
| 71 */ |
| 72 Folder get parent; |
| 73 |
| 74 /** |
| 75 * Return the full path to this resource. |
| 76 */ |
| 77 String get path; |
| 78 |
| 79 /** |
| 80 * Return a short version of the name that can be displayed to the user to |
| 81 * denote this resource. |
| 82 */ |
| 83 String get shortName; |
| 84 } |
| 85 |
| 86 |
| 87 /** |
| 88 * Instances of the class [ResourceProvider] convert [String] paths into |
| 89 * [Resource]s. |
| 90 */ |
| 91 abstract class ResourceProvider { |
| 92 /** |
| 93 * Get the path context used by this resource provider. |
| 94 */ |
| 95 Context get pathContext; |
| 96 |
| 97 /** |
| 98 * Return the [Resource] that corresponds to the given [path]. |
| 99 */ |
| 100 Resource getResource(String path); |
| 101 } |
| 102 |
| 103 |
| 104 /** |
| 105 * A [UriResolver] for [Resource]s. |
| 106 */ |
| 107 class ResourceUriResolver extends UriResolver { |
| 108 /** |
| 109 * The name of the `file` scheme. |
| 110 */ |
| 111 static String _FILE_SCHEME = "file"; |
| 112 |
| 113 final ResourceProvider _provider; |
| 114 |
| 115 ResourceUriResolver(this._provider); |
| 116 |
| 117 @override |
| 118 Source fromEncoding(UriKind kind, Uri uri) { |
| 119 if (kind == UriKind.FILE_URI) { |
| 120 Resource resource = _provider.getResource(uri.path); |
| 121 if (resource is File) { |
| 122 return resource.createSource(kind); |
| 123 } |
| 124 } |
| 125 return null; |
| 126 } |
| 127 |
| 128 @override |
| 129 Source resolveAbsolute(Uri uri) { |
| 130 if (!_isFileUri(uri)) { |
| 131 return null; |
| 132 } |
| 133 Resource resource = _provider.getResource(uri.path); |
| 134 if (resource is File) { |
| 135 return resource.createSource(UriKind.FILE_URI); |
| 136 } |
| 137 return null; |
| 138 } |
| 139 |
| 140 /** |
| 141 * Return `true` if the given URI is a `file` URI. |
| 142 * |
| 143 * @param uri the URI being tested |
| 144 * @return `true` if the given URI is a `file` URI |
| 145 */ |
| 146 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; |
| 147 } |
| OLD | NEW |