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