Chromium Code Reviews| Index: sdk/lib/_internal/pub/lib/src/barback.dart |
| diff --git a/sdk/lib/_internal/pub/lib/src/barback.dart b/sdk/lib/_internal/pub/lib/src/barback.dart |
| index 372aaa92c099c58a281ccbb31695e8dc0274533c..2e3027cb7ec60171ffb737b6197911fe36346148 100644 |
| --- a/sdk/lib/_internal/pub/lib/src/barback.dart |
| +++ b/sdk/lib/_internal/pub/lib/src/barback.dart |
| @@ -57,3 +57,52 @@ Future<BarbackServer> createServer(String host, int port, PackageGraph graph) { |
| }); |
| }); |
| } |
| + |
| +/// Parses a library identifier to an asset id. |
| +/// |
| +/// A library identifier is a string of the form "package_name" or |
| +/// "package_name/path/to/library". It does not have a trailing extension. If it |
| +/// just has a package name, it expands to lib/${package}.dart in that package. |
| +/// Otherwise, it expands to lib/${path}.dart in that package. |
| +AssetId libraryIdentifierToId(String identifier) { |
|
Bob Nystrom
2013/09/10 20:31:09
Throw an ArgumentError on an empty string.
nweiz
2013/09/10 21:44:52
Throwing a FormatError instead, since it's likely
|
| + // Convert the concise asset name in the pubspec (of the form "package" |
| + // or "package/library") to an AssetId that points to an actual dart |
| + // file ("package/lib/package.dart" or "package/lib/library.dart", |
| + // respectively). |
| + var parts = split1(identifier, "/"); |
| + if (parts.length == 1) parts.add(parts.single); |
| + return new AssetId(parts.first, 'lib/' + parts.last + '.dart'); |
| +} |
| + |
| +final _libraryPathRegexp = new RegExp(r"^lib/(.*)\.dart$"); |
|
Bob Nystrom
2013/09/10 20:31:09
Capitalize "e" in "exp". (I wish the class was nam
nweiz
2013/09/10 21:44:52
Done.
|
| + |
| +/// Converts [id] to a library identifier. |
| +/// |
| +/// A library identifier is a string of the form "package_name" or |
| +/// "package_name/path/to/library". It does not have a trailing extension. If it |
| +/// just has a package name, it expands to lib/${package}.dart in that package. |
| +/// Otherwise, it expands to lib/${path}.dart in that package. |
| +/// |
| +/// This will throw an [ArgumentError] if [id] doesn't represent a library in |
| +/// `lib/`. |
| +String idToLibraryIdentifier(AssetId id) { |
| + var match = _libraryPathRegexp.firstMatch(id.path); |
| + if (match == null) { |
| + throw new ArgumentError("Asset id $id doesn't identify a library."); |
| + } |
| + |
| + if (match[1] == id.package) return id.package; |
| + return '${id.package}/${match[1]}'; |
| +} |
| + |
| +/// Converts [id] to a "package:" URI. |
| +/// |
| +/// This will throw an [ArgumentError] if [id] doesn't represent a library in |
| +/// `lib/`. |
| +Uri idToPackageUri(AssetId id) { |
| + if (!id.path.startsWith('lib/')) { |
| + throw new ArgumentError("Asset id $id doesn't identify a library."); |
| + } |
| + |
| + return new Uri(scheme: 'package', path: id.path.replaceFirst('lib/', '')); |
| +} |