Chromium Code Reviews| 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 resource; | 5 library resource; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:io' as io; | 9 import 'dart:io' as io; |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 /** | 62 /** |
| 63 * Return the full path to this resource. | 63 * Return the full path to this resource. |
| 64 */ | 64 */ |
| 65 String get path; | 65 String get path; |
| 66 | 66 |
| 67 /** | 67 /** |
| 68 * Return a short version of the name that can be displayed to the user to | 68 * Return a short version of the name that can be displayed to the user to |
| 69 * denote this resource. | 69 * denote this resource. |
| 70 */ | 70 */ |
| 71 String get shortName; | 71 String get shortName; |
| 72 | |
| 73 /** | |
| 74 * Return the [Folder] that contains this resource, or null if this resource | |
|
scheglov
2014/06/25 21:44:25
`null` ?
"is a root folder" ?
Paul Berry
2014/06/26 15:03:08
Done.
| |
| 75 * is a root directory. | |
| 76 */ | |
| 77 Folder get parent; | |
| 72 } | 78 } |
| 73 | 79 |
| 74 | 80 |
| 75 /** | 81 /** |
| 76 * Instances of the class [ResourceProvider] convert [String] paths into | 82 * Instances of the class [ResourceProvider] convert [String] paths into |
| 77 * [Resource]s. | 83 * [Resource]s. |
| 78 */ | 84 */ |
| 79 abstract class ResourceProvider { | 85 abstract class ResourceProvider { |
| 80 /** | 86 /** |
| 81 * Return the [Resource] that corresponds to the given [path]. | 87 * Return the [Resource] that corresponds to the given [path]. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 107 bool get exists => _provider._pathToResource.containsKey(path); | 113 bool get exists => _provider._pathToResource.containsKey(path); |
| 108 | 114 |
| 109 @override | 115 @override |
| 110 get hashCode => path.hashCode; | 116 get hashCode => path.hashCode; |
| 111 | 117 |
| 112 @override | 118 @override |
| 113 String get shortName => posix.basename(path); | 119 String get shortName => posix.basename(path); |
| 114 | 120 |
| 115 @override | 121 @override |
| 116 String toString() => path; | 122 String toString() => path; |
| 123 | |
| 124 @override | |
| 125 Folder get parent { | |
| 126 String parentPath = posix.dirname(path); | |
| 127 if (parentPath == path) { | |
| 128 return null; | |
| 129 } | |
| 130 return _provider.getResource(parentPath); | |
| 131 } | |
| 117 } | 132 } |
| 118 | 133 |
| 119 | 134 |
| 120 /** | 135 /** |
| 121 * An in-memory implementation of [File]. | 136 * An in-memory implementation of [File]. |
| 122 */ | 137 */ |
| 123 class _MemoryFile extends _MemoryResource implements File { | 138 class _MemoryFile extends _MemoryResource implements File { |
| 124 _MemoryFile(MemoryResourceProvider provider, String path) : | 139 _MemoryFile(MemoryResourceProvider provider, String path) : |
| 125 super(provider, path); | 140 super(provider, path); |
| 126 | 141 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 resource = new _MemoryFile(this, path); | 298 resource = new _MemoryFile(this, path); |
| 284 } | 299 } |
| 285 return resource; | 300 return resource; |
| 286 } | 301 } |
| 287 | 302 |
| 288 Folder newFolder(String path) { | 303 Folder newFolder(String path) { |
| 289 path = posix.normalize(path); | 304 path = posix.normalize(path); |
| 290 if (!path.startsWith('/')) { | 305 if (!path.startsWith('/')) { |
| 291 throw new ArgumentError("Path must start with '/'"); | 306 throw new ArgumentError("Path must start with '/'"); |
| 292 } | 307 } |
| 293 _MemoryFolder folder = null; | 308 _MemoryResource resource = _pathToResource[path]; |
| 294 String partialPath = ""; | 309 if (resource == null) { |
| 295 for (String pathPart in path.split('/')) { | 310 String parentPath = posix.dirname(path); |
| 296 if (pathPart.isEmpty) { | 311 if (parentPath != path) { |
| 297 continue; | 312 newFolder(parentPath); |
| 298 } | 313 } |
| 299 partialPath += '/' + pathPart; | 314 _MemoryFolder folder = new _MemoryFolder(this, path); |
| 300 _MemoryResource resource = _pathToResource[partialPath]; | 315 _pathToResource[path] = folder; |
| 301 if (resource == null) { | 316 _pathToTimestamp[path] = nextStamp++; |
| 302 folder = new _MemoryFolder(this, partialPath); | 317 return folder; |
| 303 _pathToResource[partialPath] = folder; | 318 } else if (resource is _MemoryFolder) { |
| 304 _pathToTimestamp[partialPath] = nextStamp++; | 319 return resource; |
| 305 } else if (resource is _MemoryFolder) { | 320 } else { |
| 306 folder = resource; | 321 String message = 'Folder expected at ' |
| 307 } else { | 322 "'$path'" |
| 308 String message = 'Folder expected at ' | 323 'but ${resource.runtimeType} found'; |
| 309 "'$partialPath'" | 324 throw new ArgumentError(message); |
| 310 'but ${resource.runtimeType} found'; | |
| 311 throw new ArgumentError(message); | |
| 312 } | |
| 313 } | 325 } |
| 314 return folder; | |
| 315 } | 326 } |
| 316 | 327 |
| 317 File newFile(String path, String content) { | 328 File newFile(String path, String content) { |
| 318 path = posix.normalize(path); | 329 path = posix.normalize(path); |
| 319 newFolder(posix.dirname(path)); | 330 newFolder(posix.dirname(path)); |
| 320 _MemoryFile file = new _MemoryFile(this, path); | 331 _MemoryFile file = new _MemoryFile(this, path); |
| 321 _pathToResource[path] = file; | 332 _pathToResource[path] = file; |
| 322 _pathToContent[path] = content; | 333 _pathToContent[path] = content; |
| 323 _pathToTimestamp[path] = nextStamp++; | 334 _pathToTimestamp[path] = nextStamp++; |
| 324 _notifyWatchers(path, ChangeType.ADD); | 335 _notifyWatchers(path, ChangeType.ADD); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 String get path => _entry.absolute.path; | 438 String get path => _entry.absolute.path; |
| 428 | 439 |
| 429 @override | 440 @override |
| 430 get hashCode => _entry.hashCode; | 441 get hashCode => _entry.hashCode; |
| 431 | 442 |
| 432 @override | 443 @override |
| 433 String get shortName => basename(path); | 444 String get shortName => basename(path); |
| 434 | 445 |
| 435 @override | 446 @override |
| 436 String toString() => path; | 447 String toString() => path; |
| 448 | |
| 449 @override | |
| 450 Folder get parent { | |
| 451 String parentPath = dirname(path); | |
| 452 if (parentPath == path) { | |
| 453 return null; | |
| 454 } | |
| 455 return new _PhysicalFolder(new io.Directory(parentPath)); | |
| 456 } | |
| 437 } | 457 } |
| 438 | 458 |
| 439 | 459 |
| 440 /** | 460 /** |
| 441 * A `dart:io` based implementation of [ResourceProvider]. | 461 * A `dart:io` based implementation of [ResourceProvider]. |
| 442 */ | 462 */ |
| 443 class PhysicalResourceProvider implements ResourceProvider { | 463 class PhysicalResourceProvider implements ResourceProvider { |
| 444 static final PhysicalResourceProvider INSTANCE = new PhysicalResourceProvider. _(); | 464 static final PhysicalResourceProvider INSTANCE = new PhysicalResourceProvider. _(); |
| 445 | 465 |
| 446 PhysicalResourceProvider._(); | 466 PhysicalResourceProvider._(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 } | 518 } |
| 499 | 519 |
| 500 /** | 520 /** |
| 501 * Return `true` if the given URI is a `file` URI. | 521 * Return `true` if the given URI is a `file` URI. |
| 502 * | 522 * |
| 503 * @param uri the URI being tested | 523 * @param uri the URI being tested |
| 504 * @return `true` if the given URI is a `file` URI | 524 * @return `true` if the given URI is a `file` URI |
| 505 */ | 525 */ |
| 506 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; | 526 static bool _isFileUri(Uri uri) => uri.scheme == _FILE_SCHEME; |
| 507 } | 527 } |
| OLD | NEW |