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:core' hide Resource; | |
9 import 'dart:io' as io; | |
10 | |
11 import 'package:analyzer/src/generated/java_io.dart'; | |
12 import 'package:analyzer/src/generated/source_io.dart'; | |
13 import 'package:path/path.dart'; | |
14 import 'package:watcher/watcher.dart'; | |
15 | |
16 import 'file_system.dart'; | |
17 | |
18 /** | |
19 * A `dart:io` based implementation of [ResourceProvider]. | |
20 */ | |
21 class PhysicalResourceProvider implements ResourceProvider { | |
22 static final NORMALIZE_EOL_ALWAYS = | |
23 (String string) => string.replaceAll(new RegExp('\r\n?'), '\n'); | |
24 | |
25 static final PhysicalResourceProvider INSTANCE = | |
26 new PhysicalResourceProvider(null); | |
27 | |
28 /** | |
29 * The name of the directory containing plugin specific subfolders used to | |
30 * store data across sessions. | |
31 */ | |
32 static final String SERVER_DIR = ".dartServer"; | |
33 | |
34 PhysicalResourceProvider(String fileReadMode(String s)) { | |
35 if (fileReadMode != null) { | |
36 FileBasedSource.fileReadMode = fileReadMode; | |
37 } | |
38 } | |
39 | |
40 @override | |
41 Context get pathContext => io.Platform.isWindows ? windows : posix; | |
42 | |
43 @override | |
44 File getFile(String path) => new _PhysicalFile(new io.File(path)); | |
45 | |
46 @override | |
47 Folder getFolder(String path) => new _PhysicalFolder(new io.Directory(path)); | |
48 | |
49 @override | |
50 Resource getResource(String path) { | |
51 if (io.FileSystemEntity.isDirectorySync(path)) { | |
52 return getFolder(path); | |
53 } else { | |
54 return getFile(path); | |
55 } | |
56 } | |
57 | |
58 @override | |
59 Folder getStateLocation(String pluginId) { | |
60 String home; | |
61 if (io.Platform.isWindows) { | |
62 home = io.Platform.environment['LOCALAPPDATA']; | |
63 } else { | |
64 home = io.Platform.environment['HOME']; | |
65 } | |
66 if (home != null && io.FileSystemEntity.isDirectorySync(home)) { | |
67 io.Directory directory = | |
68 new io.Directory(join(home, SERVER_DIR, pluginId)); | |
69 directory.createSync(recursive: true); | |
70 return new _PhysicalFolder(directory); | |
71 } | |
72 return null; | |
73 } | |
74 } | |
75 | |
76 /** | |
77 * A `dart:io` based implementation of [File]. | |
78 */ | |
79 class _PhysicalFile extends _PhysicalResource implements File { | |
80 _PhysicalFile(io.File file) : super(file); | |
81 | |
82 @override | |
83 Stream<WatchEvent> get changes => new FileWatcher(_entry.path).events; | |
84 | |
85 @override | |
86 int get modificationStamp { | |
87 try { | |
88 io.File file = _entry as io.File; | |
89 return file.lastModifiedSync().millisecondsSinceEpoch; | |
90 } on io.FileSystemException catch (exception) { | |
91 throw new FileSystemException(exception.path, exception.message); | |
92 } | |
93 } | |
94 | |
95 @override | |
96 Source createSource([Uri uri]) { | |
97 io.File file = _entry as io.File; | |
98 JavaFile javaFile = new JavaFile(file.absolute.path); | |
99 if (uri == null) { | |
100 uri = javaFile.toURI(); | |
101 } | |
102 return new FileBasedSource(javaFile, uri); | |
103 } | |
104 | |
105 @override | |
106 bool isOrContains(String path) { | |
107 return path == this.path; | |
108 } | |
109 | |
110 @override | |
111 String readAsStringSync() { | |
112 try { | |
113 io.File file = _entry as io.File; | |
114 return file.readAsStringSync(); | |
115 } on io.FileSystemException catch (exception) { | |
116 throw new FileSystemException(exception.path, exception.message); | |
117 } | |
118 } | |
119 } | |
120 | |
121 /** | |
122 * A `dart:io` based implementation of [Folder]. | |
123 */ | |
124 class _PhysicalFolder extends _PhysicalResource implements Folder { | |
125 _PhysicalFolder(io.Directory directory) : super(directory); | |
126 | |
127 @override | |
128 Stream<WatchEvent> get changes => new DirectoryWatcher(_entry.path).events; | |
129 | |
130 @override | |
131 String canonicalizePath(String relPath) { | |
132 return normalize(join(_entry.absolute.path, relPath)); | |
133 } | |
134 | |
135 @override | |
136 bool contains(String path) { | |
137 return isWithin(this.path, path); | |
138 } | |
139 | |
140 @override | |
141 Resource getChild(String relPath) { | |
142 String canonicalPath = canonicalizePath(relPath); | |
143 return PhysicalResourceProvider.INSTANCE.getResource(canonicalPath); | |
144 } | |
145 | |
146 @override | |
147 _PhysicalFolder getChildAssumingFolder(String relPath) { | |
148 String canonicalPath = canonicalizePath(relPath); | |
149 io.Directory directory = new io.Directory(canonicalPath); | |
150 return new _PhysicalFolder(directory); | |
151 } | |
152 | |
153 @override | |
154 List<Resource> getChildren() { | |
155 try { | |
156 List<Resource> children = <Resource>[]; | |
157 io.Directory directory = _entry as io.Directory; | |
158 List<io.FileSystemEntity> entries = directory.listSync(recursive: false); | |
159 int numEntries = entries.length; | |
160 for (int i = 0; i < numEntries; i++) { | |
161 io.FileSystemEntity entity = entries[i]; | |
162 if (entity is io.Directory) { | |
163 children.add(new _PhysicalFolder(entity)); | |
164 } else if (entity is io.File) { | |
165 children.add(new _PhysicalFile(entity)); | |
166 } | |
167 } | |
168 return children; | |
169 } on io.FileSystemException catch (exception) { | |
170 throw new FileSystemException(exception.path, exception.message); | |
171 } | |
172 } | |
173 | |
174 @override | |
175 bool isOrContains(String path) { | |
176 if (path == this.path) { | |
177 return true; | |
178 } | |
179 return contains(path); | |
180 } | |
181 } | |
182 | |
183 /** | |
184 * A `dart:io` based implementation of [Resource]. | |
185 */ | |
186 abstract class _PhysicalResource implements Resource { | |
187 final io.FileSystemEntity _entry; | |
188 | |
189 _PhysicalResource(this._entry); | |
190 | |
191 @override | |
192 bool get exists => _entry.existsSync(); | |
193 | |
194 @override | |
195 get hashCode => path.hashCode; | |
196 | |
197 @override | |
198 Folder get parent { | |
199 String parentPath = dirname(path); | |
200 if (parentPath == path) { | |
201 return null; | |
202 } | |
203 return new _PhysicalFolder(new io.Directory(parentPath)); | |
204 } | |
205 | |
206 @override | |
207 String get path => _entry.absolute.path; | |
208 | |
209 @override | |
210 String get shortName => basename(path); | |
211 | |
212 @override | |
213 bool operator ==(other) { | |
214 if (runtimeType != other.runtimeType) { | |
215 return false; | |
216 } | |
217 return path == other.path; | |
218 } | |
219 | |
220 @override | |
221 String toString() => path; | |
222 } | |
OLD | NEW |