OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library barback.asset.asset_id; |
| 6 |
| 7 import 'package:path/path.dart' as pathos; |
| 8 |
| 9 /// Identifies an asset within a package. |
| 10 class AssetId implements Comparable<AssetId> { |
| 11 /// The name of the package containing this asset. |
| 12 final String package; |
| 13 |
| 14 /// The path to the asset relative to the root directory of [package]. |
| 15 /// |
| 16 /// Source (i.e. read from disk) and generated (i.e. the output of a |
| 17 /// [Transformer]) assets all have paths. Even intermediate assets that are |
| 18 /// generated and then consumed by later transformations will still have |
| 19 /// a path used to identify it. |
| 20 /// |
| 21 /// Asset paths always use forward slashes as path separators, regardless of |
| 22 /// the host platform. |
| 23 final String path; |
| 24 |
| 25 /// Gets the file extension of the asset, if it has one, including the ".". |
| 26 String get extension => pathos.extension(path); |
| 27 |
| 28 /// Creates a new AssetId at [path] within [package]. |
| 29 /// |
| 30 /// The [path] will be normalized: any backslashes will be replaced with |
| 31 /// forward slashes (regardless of host OS) and "." and ".." will be removed |
| 32 /// where possible. |
| 33 AssetId(this.package, String path) |
| 34 : path = _normalizePath(path); |
| 35 |
| 36 /// Parses an [AssetId] string of the form "package|path/to/asset.txt". |
| 37 /// |
| 38 /// The [path] will be normalized: any backslashes will be replaced with |
| 39 /// forward slashes (regardless of host OS) and "." and ".." will be removed |
| 40 /// where possible. |
| 41 factory AssetId.parse(String description) { |
| 42 var parts = description.split("|"); |
| 43 if (parts.length != 2) { |
| 44 throw new FormatException('Could not parse "$description".'); |
| 45 } |
| 46 |
| 47 if (parts[0].isEmpty) { |
| 48 throw new FormatException( |
| 49 'Cannot have empty package name in "$description".'); |
| 50 } |
| 51 |
| 52 if (parts[1].isEmpty) { |
| 53 throw new FormatException( |
| 54 'Cannot have empty path in "$description".'); |
| 55 } |
| 56 |
| 57 return new AssetId(parts[0], parts[1]); |
| 58 } |
| 59 |
| 60 /// Deserializes an [AssetId] from [data], which must be the result of |
| 61 /// calling [serialize] on an existing [AssetId]. |
| 62 /// |
| 63 /// Note that this is intended for communicating ids across isolates and not |
| 64 /// for persistent storage of asset identifiers. There is no guarantee of |
| 65 /// backwards compatibility in serialization form across versions. |
| 66 AssetId.deserialize(data) |
| 67 : package = data[0], |
| 68 path = data[1]; |
| 69 |
| 70 /// Returns `true` of [other] is an [AssetId] with the same package and path. |
| 71 operator ==(other) => |
| 72 other is AssetId && |
| 73 package == other.package && |
| 74 path == other.path; |
| 75 |
| 76 int get hashCode => package.hashCode ^ path.hashCode; |
| 77 |
| 78 int compareTo(AssetId other) { |
| 79 var packageComp = package.compareTo(other.package); |
| 80 if (packageComp != 0) return packageComp; |
| 81 return path.compareTo(other.path); |
| 82 } |
| 83 |
| 84 /// Returns a new [AssetId] with the same [package] as this one and with the |
| 85 /// [path] extended to include [extension]. |
| 86 AssetId addExtension(String extension) => |
| 87 new AssetId(package, "$path$extension"); |
| 88 |
| 89 /// Returns a new [AssetId] with the same [package] and [path] as this one |
| 90 /// but with file extension [newExtension]. |
| 91 AssetId changeExtension(String newExtension) => |
| 92 new AssetId(package, pathos.withoutExtension(path) + newExtension); |
| 93 |
| 94 String toString() => "$package|$path"; |
| 95 |
| 96 /// Serializes this [AssetId] to an object that can be sent across isolates |
| 97 /// and passed to [deserialize]. |
| 98 serialize() => [package, path]; |
| 99 } |
| 100 |
| 101 String _normalizePath(String path) { |
| 102 if (pathos.isAbsolute(path)) { |
| 103 throw new ArgumentError('Asset paths must be relative, but got "$path".'); |
| 104 } |
| 105 |
| 106 // Normalize path separators so that they are always "/" in the AssetID. |
| 107 path = path.replaceAll(r"\", "/"); |
| 108 |
| 109 // Collapse "." and "..". |
| 110 return pathos.posix.normalize(path); |
| 111 } |
OLD | NEW |