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([Uri uri]); |
| 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 `true` if absolute [path] references a resource in this folder. |
| 47 */ |
| 48 bool contains(String path); |
| 49 |
| 50 /** |
| 51 * Return an existing child [Resource] with the given [relPath]. |
| 52 * Return a not existing [File] if no such child exist. |
| 53 */ |
| 54 Resource getChild(String relPath); |
| 55 |
| 56 /** |
| 57 * Return a list of existing direct children [Resource]s (folders and files) |
| 58 * in this folder, in no particular order. |
| 59 */ |
| 60 List<Resource> getChildren(); |
| 61 } |
| 62 |
| 63 |
| 64 /** |
| 65 * The abstract class [Resource] is an abstraction of file or folder. |
| 66 */ |
| 67 abstract class Resource { |
| 68 /** |
| 69 * Return `true` if this resource exists. |
| 70 */ |
| 71 bool get exists; |
| 72 |
| 73 /** |
| 74 * Return the [Folder] that contains this resource, or `null` if this resource |
| 75 * is a root folder. |
| 76 */ |
| 77 Folder get parent; |
| 78 |
| 79 /** |
| 80 * Return the full path to this resource. |
| 81 */ |
| 82 String get path; |
| 83 |
| 84 /** |
| 85 * Return a short version of the name that can be displayed to the user to |
| 86 * denote this resource. |
| 87 */ |
| 88 String get shortName; |
| 89 |
| 90 /** |
| 91 * Return `true` if absolute [path] references this resource or a resource in |
| 92 * this folder. |
| 93 */ |
| 94 bool isOrContains(String path); |
| 95 } |
| 96 |
| 97 |
| 98 /** |
| 99 * Instances of the class [ResourceProvider] convert [String] paths into |
| 100 * [Resource]s. |
| 101 */ |
| 102 abstract class ResourceProvider { |
| 103 /** |
| 104 * Get the path context used by this resource provider. |
| 105 */ |
| 106 Context get pathContext; |
| 107 |
| 108 /** |
| 109 * Return the [Resource] that corresponds to the given [path]. |
| 110 */ |
| 111 Resource getResource(String path); |
| 112 } |
| 113 |
| 114 |
| 115 /** |
| 116 * A [UriResolver] for [Resource]s. |
| 117 */ |
| 118 class ResourceUriResolver extends UriResolver { |
| 119 /** |
| 120 * The name of the `file` scheme. |
| 121 */ |
| 122 static String _FILE_SCHEME = "file"; |
| 123 |
| 124 final ResourceProvider _provider; |
| 125 |
| 126 ResourceUriResolver(this._provider); |
| 127 |
| 128 @override |
| 129 Source resolveAbsolute(Uri uri) { |
| 130 if (!_isFileUri(uri)) { |
| 131 return null; |
| 132 } |
| 133 Resource resource = _provider.getResource(_provider.pathContext.fromUri(uri) |
| 134 ); |
| 135 if (resource is File) { |
| 136 return resource.createSource(uri); |
| 137 } |
| 138 return null; |
| 139 } |
| 140 |
| 141 /** |
| 142 * Return `true` if the given URI is a `file` URI. |
| 143 * |
| 144 * @param uri the URI being tested |
| 145 * @return `true` if the given URI is a `file` URI |
| 146 */ |
| 147 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; |
| 148 } |
OLD | NEW |