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_id; | 5 library barback.asset_id; |
6 | 6 |
7 import 'package:path/path.dart' as pathos; | 7 import 'package:path/path.dart' as pathos; |
8 | 8 |
9 /// AssetIDs always use POSIX style paths regardless of the host platform. | |
10 final _posix = new pathos.Builder(style: pathos.Style.posix); | |
11 | |
12 /// Identifies an asset within a package. | 9 /// Identifies an asset within a package. |
13 class AssetId implements Comparable<AssetId> { | 10 class AssetId implements Comparable<AssetId> { |
14 /// The name of the package containing this asset. | 11 /// The name of the package containing this asset. |
15 final String package; | 12 final String package; |
16 | 13 |
17 /// The path to the asset relative to the root directory of [package]. | 14 /// The path to the asset relative to the root directory of [package]. |
18 /// | 15 /// |
19 /// Source (i.e. read from disk) and generated (i.e. the output of a | 16 /// Source (i.e. read from disk) and generated (i.e. the output of a |
20 /// [Transformer]) assets all have paths. Even intermediate assets that are | 17 /// [Transformer]) assets all have paths. Even intermediate assets that are |
21 /// generated and then consumed by later transformations will still have | 18 /// generated and then consumed by later transformations will still have |
22 /// a path used to identify it. | 19 /// a path used to identify it. |
23 /// | 20 /// |
24 /// Asset paths always use forward slashes as path separators, regardless of | 21 /// Asset paths always use forward slashes as path separators, regardless of |
25 /// the host platform. | 22 /// the host platform. |
26 final String path; | 23 final String path; |
27 | 24 |
28 /// 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 ".". |
29 String get extension => pathos.extension(path); | 26 String get extension => pathos.extension(path); |
30 | 27 |
31 /// Creates a new AssetId at [path] within [package]. | 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. |
32 AssetId(this.package, String path) | 33 AssetId(this.package, String path) |
33 : path = _posix.normalize(path); | 34 : path = _normalizePath(path); |
34 | 35 |
35 /// Parses an [AssetId] string of the form "package|path/to/asset.txt". | 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. |
36 factory AssetId.parse(String description) { | 41 factory AssetId.parse(String description) { |
37 var parts = description.split("|"); | 42 var parts = description.split("|"); |
38 if (parts.length != 2) { | 43 if (parts.length != 2) { |
39 throw new FormatException('Could not parse "$description".'); | 44 throw new FormatException('Could not parse "$description".'); |
40 } | 45 } |
41 | 46 |
42 if (parts[0].isEmpty) { | 47 if (parts[0].isEmpty) { |
43 throw new FormatException( | 48 throw new FormatException( |
44 'Cannot have empty package name in "$description".'); | 49 'Cannot have empty package name in "$description".'); |
45 } | 50 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 /// but with file extension [newExtension]. | 90 /// but with file extension [newExtension]. |
86 AssetId changeExtension(String newExtension) => | 91 AssetId changeExtension(String newExtension) => |
87 new AssetId(package, pathos.withoutExtension(path) + newExtension); | 92 new AssetId(package, pathos.withoutExtension(path) + newExtension); |
88 | 93 |
89 String toString() => "$package|$path"; | 94 String toString() => "$package|$path"; |
90 | 95 |
91 /// Serializes this [AssetId] to an object that can be sent across isolates | 96 /// Serializes this [AssetId] to an object that can be sent across isolates |
92 /// and passed to [deserialize]. | 97 /// and passed to [deserialize]. |
93 serialize() => [package, path]; | 98 serialize() => [package, path]; |
94 } | 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 |