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 * [File]s are leaf [Resource]s which contain data. | |
15 */ | |
16 abstract class File extends Resource { | |
17 /** | |
18 * Watch for changes to this file | |
19 */ | |
20 Stream<WatchEvent> get changes; | |
21 | |
22 /** | |
23 * Return the last-modified stamp of the file. | |
24 * Throws [FileSystemException] if the file does not exist. | |
25 */ | |
26 int get modificationStamp; | |
27 | |
28 /** | |
29 * Create a new [Source] instance that serves this file. | |
30 */ | |
31 Source createSource([Uri uri]); | |
32 | |
33 /** | |
34 * Synchronously read the entire file contents as a [String]. | |
35 * Throws [FileSystemException] if the file does not exist. | |
36 */ | |
37 String readAsStringSync(); | |
38 } | |
39 | |
40 /** | |
41 * Base class for all file system exceptions. | |
42 */ | |
43 class FileSystemException implements Exception { | |
44 final String path; | |
45 final String message; | |
46 | |
47 FileSystemException(this.path, this.message); | |
48 | |
49 String toString() => 'FileSystemException(path=$path; message=$message)'; | |
50 } | |
51 | |
52 /** | |
53 * [Folder]s are [Resource]s which may contain files and/or other folders. | |
54 */ | |
55 abstract class Folder extends Resource { | |
56 /** | |
57 * Watch for changes to the files inside this folder (and in any nested | |
58 * folders, including folders reachable via links). | |
59 */ | |
60 Stream<WatchEvent> get changes; | |
61 | |
62 /** | |
63 * 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 | |
65 * path, then don't change it. | |
66 * | |
67 * However, regardless of whether [path] is relative or absolute, normalize | |
68 * it by removing path components of the form '.' or '..'. | |
69 */ | |
70 String canonicalizePath(String path); | |
71 | |
72 /** | |
73 * Return `true` if absolute [path] references a resource in this folder. | |
74 */ | |
75 bool contains(String path); | |
76 | |
77 /** | |
78 * Return an existing child [Resource] with the given [relPath]. | |
79 * Return a not existing [File] if no such child exist. | |
80 */ | |
81 Resource getChild(String relPath); | |
82 | |
83 /** | |
84 * Return a [Folder] representing a child [Resource] with the given | |
85 * [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 | |
87 * to determine whether the folder actually exists. | |
88 */ | |
89 Folder getChildAssumingFolder(String relPath); | |
90 | |
91 /** | |
92 * Return a list of existing direct children [Resource]s (folders and files) | |
93 * in this folder, in no particular order. | |
94 */ | |
95 List<Resource> getChildren(); | |
96 } | |
97 | |
98 /** | |
99 * The abstract class [Resource] is an abstraction of file or folder. | |
100 */ | |
101 abstract class Resource { | |
102 /** | |
103 * Return `true` if this resource exists. | |
104 */ | |
105 bool get exists; | |
106 | |
107 /** | |
108 * Return the [Folder] that contains this resource, or `null` if this resource | |
109 * is a root folder. | |
110 */ | |
111 Folder get parent; | |
112 | |
113 /** | |
114 * Return the full path to this resource. | |
115 */ | |
116 String get path; | |
117 | |
118 /** | |
119 * Return a short version of the name that can be displayed to the user to | |
120 * denote this resource. | |
121 */ | |
122 String get shortName; | |
123 | |
124 /** | |
125 * Return `true` if absolute [path] references this resource or a resource in | |
126 * this folder. | |
127 */ | |
128 bool isOrContains(String path); | |
129 } | |
130 | |
131 /** | |
132 * Instances of the class [ResourceProvider] convert [String] paths into | |
133 * [Resource]s. | |
134 */ | |
135 abstract class ResourceProvider { | |
136 /** | |
137 * Get the path context used by this resource provider. | |
138 */ | |
139 Context get pathContext; | |
140 | |
141 /** | |
142 * Return a [File] that corresponds to the given [path]. | |
143 * | |
144 * A file may or may not exist at this location. | |
145 */ | |
146 File getFile(String path); | |
147 | |
148 /** | |
149 * Return a [Folder] that corresponds to the given [path]. | |
150 * | |
151 * A folder may or may not exist at this location. | |
152 */ | |
153 Folder getFolder(String path); | |
154 | |
155 /** | |
156 * Return the [Resource] that corresponds to the given [path]. | |
157 */ | |
158 Resource getResource(String path); | |
159 | |
160 /** | |
161 * 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 | |
163 * 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 | |
165 * the plugin ids are unique. The plugin ids must be valid folder names. | |
166 */ | |
167 Folder getStateLocation(String pluginId); | |
168 } | |
169 | |
170 /** | |
171 * A [UriResolver] for [Resource]s. | |
172 */ | |
173 class ResourceUriResolver extends UriResolver { | |
174 /** | |
175 * The name of the `file` scheme. | |
176 */ | |
177 static String _FILE_SCHEME = "file"; | |
178 | |
179 final ResourceProvider _provider; | |
180 | |
181 ResourceUriResolver(this._provider); | |
182 | |
183 @override | |
184 Source resolveAbsolute(Uri uri) { | |
185 if (!_isFileUri(uri)) { | |
186 return null; | |
187 } | |
188 Resource resource = | |
189 _provider.getResource(_provider.pathContext.fromUri(uri)); | |
190 if (resource is File) { | |
191 return resource.createSource(uri); | |
192 } | |
193 return null; | |
194 } | |
195 | |
196 @override | |
197 Uri restoreAbsolute(Source source) => source.uri; | |
198 | |
199 /** | |
200 * Return `true` if the given [uri] is a `file` URI. | |
201 */ | |
202 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; | |
203 } | |
OLD | NEW |