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 analyzer.file_system.physical_file_system; | 5 library analyzer.file_system.physical_file_system; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
9 import 'dart:io' as io; | 9 import 'dart:io' as io; |
10 | 10 |
11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
12 import 'package:analyzer/src/generated/java_io.dart'; | 12 import 'package:analyzer/src/generated/java_io.dart'; |
13 import 'package:analyzer/src/generated/source_io.dart'; | 13 import 'package:analyzer/src/generated/source_io.dart'; |
14 import 'package:analyzer/src/util/absolute_path.dart'; | 14 import 'package:analyzer/src/util/absolute_path.dart'; |
15 import 'package:isolate/isolate_runner.dart'; | |
15 import 'package:path/path.dart'; | 16 import 'package:path/path.dart'; |
16 import 'package:watcher/watcher.dart'; | 17 import 'package:watcher/watcher.dart'; |
17 | 18 |
18 /** | 19 /** |
20 * Return modification times for every file path in [paths]. | |
21 * | |
22 * If a path is `null`, the modification time is also `null`. | |
23 * | |
24 * If any exception happens, the file is considered as a not existing and | |
25 * `-1` is its modification time. | |
26 */ | |
27 List<int> _pathsToTimes(List<String> paths) { | |
28 return paths.map((path) { | |
29 if (path != null) { | |
30 try { | |
31 io.File file = new io.File(path); | |
32 return file.lastModifiedSync().millisecondsSinceEpoch; | |
33 } catch (_) { | |
34 return -1; | |
35 } | |
36 } else { | |
37 return null; | |
38 } | |
39 }).toList(); | |
40 } | |
41 | |
42 /** | |
19 * A `dart:io` based implementation of [ResourceProvider]. | 43 * A `dart:io` based implementation of [ResourceProvider]. |
20 */ | 44 */ |
21 class PhysicalResourceProvider implements ResourceProvider { | 45 class PhysicalResourceProvider implements ResourceProvider { |
22 static final NORMALIZE_EOL_ALWAYS = | 46 static final NORMALIZE_EOL_ALWAYS = |
23 (String string) => string.replaceAll(new RegExp('\r\n?'), '\n'); | 47 (String string) => string.replaceAll(new RegExp('\r\n?'), '\n'); |
24 | 48 |
25 static final PhysicalResourceProvider INSTANCE = | 49 static final PhysicalResourceProvider INSTANCE = |
26 new PhysicalResourceProvider(null); | 50 new PhysicalResourceProvider(null); |
27 | 51 |
28 /** | 52 /** |
29 * The name of the directory containing plugin specific subfolders used to | 53 * The name of the directory containing plugin specific subfolders used to |
30 * store data across sessions. | 54 * store data across sessions. |
31 */ | 55 */ |
32 static final String SERVER_DIR = ".dartServer"; | 56 static final String SERVER_DIR = ".dartServer"; |
33 | 57 |
58 static _SingleIsolateRunnerProvider pathsToTimesIsolateProvider = | |
59 new _SingleIsolateRunnerProvider(); | |
60 | |
34 @override | 61 @override |
35 final AbsolutePathContext absolutePathContext = | 62 final AbsolutePathContext absolutePathContext = |
36 new AbsolutePathContext(io.Platform.isWindows); | 63 new AbsolutePathContext(io.Platform.isWindows); |
37 | 64 |
38 PhysicalResourceProvider(String fileReadMode(String s)) { | 65 PhysicalResourceProvider(String fileReadMode(String s)) { |
39 if (fileReadMode != null) { | 66 if (fileReadMode != null) { |
40 FileBasedSource.fileReadMode = fileReadMode; | 67 FileBasedSource.fileReadMode = fileReadMode; |
41 } | 68 } |
42 } | 69 } |
43 | 70 |
44 @override | 71 @override |
45 Context get pathContext => io.Platform.isWindows ? windows : posix; | 72 Context get pathContext => io.Platform.isWindows ? windows : posix; |
46 | 73 |
47 @override | 74 @override |
48 File getFile(String path) => new _PhysicalFile(new io.File(path)); | 75 File getFile(String path) => new _PhysicalFile(new io.File(path)); |
49 | 76 |
50 @override | 77 @override |
51 Folder getFolder(String path) => new _PhysicalFolder(new io.Directory(path)); | 78 Folder getFolder(String path) => new _PhysicalFolder(new io.Directory(path)); |
52 | 79 |
53 @override | 80 @override |
81 Future<List<int>> getModificationTimes(List<Source> sources) async { | |
82 List<String> paths = sources | |
83 .map((source) => source is FileBasedSource ? source.fullName : null) | |
84 .toList(); | |
85 IsolateRunner runner = await pathsToTimesIsolateProvider.get(); | |
86 return runner.run(_pathsToTimes, paths); | |
87 } | |
88 | |
89 @override | |
54 Resource getResource(String path) { | 90 Resource getResource(String path) { |
55 if (io.FileSystemEntity.isDirectorySync(path)) { | 91 if (io.FileSystemEntity.isDirectorySync(path)) { |
56 return getFolder(path); | 92 return getFolder(path); |
57 } else { | 93 } else { |
58 return getFile(path); | 94 return getFile(path); |
59 } | 95 } |
60 } | 96 } |
61 | 97 |
62 @override | 98 @override |
63 Folder getStateLocation(String pluginId) { | 99 Folder getStateLocation(String pluginId) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 try { | 303 try { |
268 _entry.deleteSync(recursive: true); | 304 _entry.deleteSync(recursive: true); |
269 } on io.FileSystemException catch (exception) { | 305 } on io.FileSystemException catch (exception) { |
270 throw new FileSystemException(exception.path, exception.message); | 306 throw new FileSystemException(exception.path, exception.message); |
271 } | 307 } |
272 } | 308 } |
273 | 309 |
274 @override | 310 @override |
275 String toString() => path; | 311 String toString() => path; |
276 } | 312 } |
313 | |
314 /** | |
315 * This class encapsulates logic for creating a single [IsolateRunner]. | |
316 */ | |
317 class _SingleIsolateRunnerProvider { | |
318 bool _isBeenCreated = false; | |
Brian Wilkerson
2016/07/08 21:25:09
"_is" --> "_has" || "_isBeenCreated" --> "_created
| |
319 IsolateRunner _runner; | |
320 | |
321 /** | |
322 * Complete with the only [IsolateRunner] instance. | |
323 */ | |
324 Future<IsolateRunner> get() async { | |
325 if (_runner != null) { | |
326 return _runner; | |
327 } | |
328 if (_isBeenCreated) { | |
329 Completer<IsolateRunner> completer = new Completer<IsolateRunner>(); | |
330 new Timer.periodic(new Duration(milliseconds: 20), (Timer timer) { | |
331 if (_runner != null) { | |
332 completer.complete(_runner); | |
333 timer.cancel(); | |
334 } | |
335 }); | |
336 return completer.future; | |
337 } | |
338 _isBeenCreated = true; | |
339 _runner = await IsolateRunner.spawn(); | |
340 return _runner; | |
341 } | |
342 } | |
OLD | NEW |