| 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 pub.barback; | 5 library pub.barback; |
| 6 | 6 |
| 7 import 'package:barback/barback.dart'; | 7 import 'package:barback/barback.dart'; |
| 8 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
| 9 import 'package:pub_semver/pub_semver.dart'; | 9 import 'package:pub_semver/pub_semver.dart'; |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 /// Converts [id] to a "package:" URI. | 45 /// Converts [id] to a "package:" URI. |
| 46 /// | 46 /// |
| 47 /// This will throw an [ArgumentError] if [id] doesn't represent a library in | 47 /// This will throw an [ArgumentError] if [id] doesn't represent a library in |
| 48 /// `lib/`. | 48 /// `lib/`. |
| 49 Uri idToPackageUri(AssetId id) { | 49 Uri idToPackageUri(AssetId id) { |
| 50 if (!id.path.startsWith('lib/')) { | 50 if (!id.path.startsWith('lib/')) { |
| 51 throw new ArgumentError("Asset id $id doesn't identify a library."); | 51 throw new ArgumentError("Asset id $id doesn't identify a library."); |
| 52 } | 52 } |
| 53 | 53 |
| 54 return new Uri(scheme: 'package', | 54 return new Uri( |
| 55 scheme: 'package', |
| 55 path: p.url.join(id.package, id.path.replaceFirst('lib/', ''))); | 56 path: p.url.join(id.package, id.path.replaceFirst('lib/', ''))); |
| 56 } | 57 } |
| 57 | 58 |
| 58 /// Converts [uri] into an [AssetId] if its path is within "packages". | 59 /// Converts [uri] into an [AssetId] if its path is within "packages". |
| 59 /// | 60 /// |
| 60 /// If the URL contains a special directory, but lacks a following package name, | 61 /// If the URL contains a special directory, but lacks a following package name, |
| 61 /// throws a [FormatException]. | 62 /// throws a [FormatException]. |
| 62 /// | 63 /// |
| 63 /// If the URI doesn't contain one of those special directories, returns null. | 64 /// If the URI doesn't contain one of those special directories, returns null. |
| 64 AssetId packagesUrlToId(Uri url) { | 65 AssetId packagesUrlToId(Uri url) { |
| 65 var parts = p.url.split(url.path); | 66 var parts = p.url.split(url.path); |
| 66 | 67 |
| 67 // Strip the leading "/" from the URL. | 68 // Strip the leading "/" from the URL. |
| 68 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1).toList(); | 69 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1).toList(); |
| 69 | 70 |
| 70 if (parts.isEmpty) return null; | 71 if (parts.isEmpty) return null; |
| 71 | 72 |
| 72 // Check for "packages" in the URL. | 73 // Check for "packages" in the URL. |
| 73 // TODO(rnystrom): If we rewrite "package:" imports to relative imports that | 74 // TODO(rnystrom): If we rewrite "package:" imports to relative imports that |
| 74 // point to a canonical "packages" directory, we can limit "packages" to the | 75 // point to a canonical "packages" directory, we can limit "packages" to the |
| 75 // root of the URL as well. See: #16649. | 76 // root of the URL as well. See: #16649. |
| 76 var index = parts.indexOf("packages"); | 77 var index = parts.indexOf("packages"); |
| 77 if (index == -1) return null; | 78 if (index == -1) return null; |
| 78 | 79 |
| 79 // There should be a package name after "packages". | 80 // There should be a package name after "packages". |
| 80 if (parts.length <= index + 1) { | 81 if (parts.length <= index + 1) { |
| 81 throw new FormatException( | 82 throw new FormatException( |
| 82 'Invalid URL path "${url.path}". Expected package name ' | 83 'Invalid URL path "${url.path}". Expected package name ' 'after "package
s".'); |
| 83 'after "packages".'); | |
| 84 } | 84 } |
| 85 | 85 |
| 86 var package = parts[index + 1]; | 86 var package = parts[index + 1]; |
| 87 var assetPath = p.url.join("lib", p.url.joinAll(parts.skip(index + 2))); | 87 var assetPath = p.url.join("lib", p.url.joinAll(parts.skip(index + 2))); |
| 88 return new AssetId(package, assetPath); | 88 return new AssetId(package, assetPath); |
| 89 } | 89 } |
| OLD | NEW |