| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 barback.asset.asset_id; | 5 library barback.asset.asset_id; |
| 6 | 6 |
| 7 import 'package:path/path.dart' as pathos; | 7 import 'package:path/path.dart' as pathos; |
| 8 | 8 |
| 9 /// Identifies an asset within a package. | 9 /// Identifies an asset within a package. |
| 10 class AssetId implements Comparable<AssetId> { | 10 class AssetId implements Comparable<AssetId> { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 final String path; | 23 final String path; |
| 24 | 24 |
| 25 /// Gets the file extension of the asset, if it has one, including the ".". | 25 /// Gets the file extension of the asset, if it has one, including the ".". |
| 26 String get extension => pathos.extension(path); | 26 String get extension => pathos.extension(path); |
| 27 | 27 |
| 28 /// Creates a new AssetId at [path] within [package]. | 28 /// Creates a new AssetId at [path] within [package]. |
| 29 /// | 29 /// |
| 30 /// The [path] will be normalized: any backslashes will be replaced with | 30 /// The [path] will be normalized: any backslashes will be replaced with |
| 31 /// forward slashes (regardless of host OS) and "." and ".." will be removed | 31 /// forward slashes (regardless of host OS) and "." and ".." will be removed |
| 32 /// where possible. | 32 /// where possible. |
| 33 AssetId(this.package, String path) | 33 AssetId(this.package, String path) : path = _normalizePath(path); |
| 34 : path = _normalizePath(path); | |
| 35 | 34 |
| 36 /// Parses an [AssetId] string of the form "package|path/to/asset.txt". | 35 /// Parses an [AssetId] string of the form "package|path/to/asset.txt". |
| 37 /// | 36 /// |
| 38 /// The [path] will be normalized: any backslashes will be replaced with | 37 /// The [path] will be normalized: any backslashes will be replaced with |
| 39 /// forward slashes (regardless of host OS) and "." and ".." will be removed | 38 /// forward slashes (regardless of host OS) and "." and ".." will be removed |
| 40 /// where possible. | 39 /// where possible. |
| 41 factory AssetId.parse(String description) { | 40 factory AssetId.parse(String description) { |
| 42 var parts = description.split("|"); | 41 var parts = description.split("|"); |
| 43 if (parts.length != 2) { | 42 if (parts.length != 2) { |
| 44 throw new FormatException('Could not parse "$description".'); | 43 throw new FormatException('Could not parse "$description".'); |
| 45 } | 44 } |
| 46 | 45 |
| 47 if (parts[0].isEmpty) { | 46 if (parts[0].isEmpty) { |
| 48 throw new FormatException( | 47 throw new FormatException( |
| 49 'Cannot have empty package name in "$description".'); | 48 'Cannot have empty package name in "$description".'); |
| 50 } | 49 } |
| 51 | 50 |
| 52 if (parts[1].isEmpty) { | 51 if (parts[1].isEmpty) { |
| 53 throw new FormatException( | 52 throw new FormatException('Cannot have empty path in "$description".'); |
| 54 'Cannot have empty path in "$description".'); | |
| 55 } | 53 } |
| 56 | 54 |
| 57 return new AssetId(parts[0], parts[1]); | 55 return new AssetId(parts[0], parts[1]); |
| 58 } | 56 } |
| 59 | 57 |
| 60 /// Deserializes an [AssetId] from [data], which must be the result of | 58 /// Deserializes an [AssetId] from [data], which must be the result of |
| 61 /// calling [serialize] on an existing [AssetId]. | 59 /// calling [serialize] on an existing [AssetId]. |
| 62 /// | 60 /// |
| 63 /// Note that this is intended for communicating ids across isolates and not | 61 /// Note that this is intended for communicating ids across isolates and not |
| 64 /// for persistent storage of asset identifiers. There is no guarantee of | 62 /// for persistent storage of asset identifiers. There is no guarantee of |
| 65 /// backwards compatibility in serialization form across versions. | 63 /// backwards compatibility in serialization form across versions. |
| 66 AssetId.deserialize(data) | 64 AssetId.deserialize(data) |
| 67 : package = data[0], | 65 : package = data[0], |
| 68 path = data[1]; | 66 path = data[1]; |
| 69 | 67 |
| 70 /// Returns `true` of [other] is an [AssetId] with the same package and path. | 68 /// Returns `true` of [other] is an [AssetId] with the same package and path. |
| 71 operator ==(other) => | 69 operator ==(other) => |
| 72 other is AssetId && | 70 other is AssetId && package == other.package && path == other.path; |
| 73 package == other.package && | |
| 74 path == other.path; | |
| 75 | 71 |
| 76 int get hashCode => package.hashCode ^ path.hashCode; | 72 int get hashCode => package.hashCode ^ path.hashCode; |
| 77 | 73 |
| 78 int compareTo(AssetId other) { | 74 int compareTo(AssetId other) { |
| 79 var packageComp = package.compareTo(other.package); | 75 var packageComp = package.compareTo(other.package); |
| 80 if (packageComp != 0) return packageComp; | 76 if (packageComp != 0) return packageComp; |
| 81 return path.compareTo(other.path); | 77 return path.compareTo(other.path); |
| 82 } | 78 } |
| 83 | 79 |
| 84 /// Returns a new [AssetId] with the same [package] as this one and with the | 80 /// Returns a new [AssetId] with the same [package] as this one and with the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 String _normalizePath(String path) { | 97 String _normalizePath(String path) { |
| 102 if (pathos.isAbsolute(path)) { | 98 if (pathos.isAbsolute(path)) { |
| 103 throw new ArgumentError('Asset paths must be relative, but got "$path".'); | 99 throw new ArgumentError('Asset paths must be relative, but got "$path".'); |
| 104 } | 100 } |
| 105 | 101 |
| 106 // Normalize path separators so that they are always "/" in the AssetID. | 102 // Normalize path separators so that they are always "/" in the AssetID. |
| 107 path = path.replaceAll(r"\", "/"); | 103 path = path.replaceAll(r"\", "/"); |
| 108 | 104 |
| 109 // Collapse "." and "..". | 105 // Collapse "." and "..". |
| 110 return pathos.posix.normalize(path); | 106 return pathos.posix.normalize(path); |
| 111 } | 107 } |
| OLD | NEW |