OLD | NEW |
| 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 |
1 library pub.barback; | 5 library pub.barback; |
| 6 |
2 import 'package:barback/barback.dart'; | 7 import 'package:barback/barback.dart'; |
3 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
4 import 'package:pub_semver/pub_semver.dart'; | 9 import 'package:pub_semver/pub_semver.dart'; |
| 10 |
| 11 /// The currently supported versions of packages that this version of pub works |
| 12 /// with. |
| 13 /// |
| 14 /// Pub implicitly constrains these packages to these versions as long as |
| 15 /// barback is a dependency. |
| 16 /// |
| 17 /// Users' transformers are loaded in an isolate that uses the entrypoint |
| 18 /// package's dependency versions. However, that isolate also loads code |
| 19 /// provided by pub (`asset/dart/transformer_isolate.dart` and associated |
| 20 /// files). This code uses these packages as well, so these constraints exist to |
| 21 /// ensure that its usage of the packages remains valid. |
| 22 /// |
| 23 /// Most constraints here are like normal version constraints in that their |
| 24 /// upper bound is the next major version of the package (or minor version for |
| 25 /// pre-1.0.0 packages). If a new major version of the package is released, |
| 26 /// these *must* be incremented to synchronize with that. |
| 27 /// |
| 28 /// The constraint on barback is different. Its upper bound is the next *patch* |
| 29 /// version of barback—that is, the next version with new features. This is |
| 30 /// because most barback features need additional serialization code to be fully |
| 31 /// supported in pub, even if they're otherwise backwards-compatible. |
| 32 /// |
| 33 /// Whenever a new minor or patch version of barback is published, this *must* |
| 34 /// be incremented to synchronize with that. See the barback [compatibility |
| 35 /// documentation][compat] for details on the relationship between this |
| 36 /// constraint and barback's version. |
| 37 /// |
| 38 /// [compat]: https://gist.github.com/nex3/10942218 |
5 final pubConstraints = { | 39 final pubConstraints = { |
6 "barback": new VersionConstraint.parse(">=0.13.0 <0.15.3"), | 40 "barback": new VersionConstraint.parse(">=0.13.0 <0.15.3"), |
7 "source_span": new VersionConstraint.parse(">=1.0.0 <2.0.0"), | 41 "source_span": new VersionConstraint.parse(">=1.0.0 <2.0.0"), |
8 "stack_trace": new VersionConstraint.parse(">=0.9.1 <2.0.0") | 42 "stack_trace": new VersionConstraint.parse(">=0.9.1 <2.0.0") |
9 }; | 43 }; |
| 44 |
| 45 /// Converts [id] to a "package:" URI. |
| 46 /// |
| 47 /// This will throw an [ArgumentError] if [id] doesn't represent a library in |
| 48 /// `lib/`. |
10 Uri idToPackageUri(AssetId id) { | 49 Uri idToPackageUri(AssetId id) { |
11 if (!id.path.startsWith('lib/')) { | 50 if (!id.path.startsWith('lib/')) { |
12 throw new ArgumentError("Asset id $id doesn't identify a library."); | 51 throw new ArgumentError("Asset id $id doesn't identify a library."); |
13 } | 52 } |
| 53 |
14 return new Uri( | 54 return new Uri( |
15 scheme: 'package', | 55 scheme: 'package', |
16 path: p.url.join(id.package, id.path.replaceFirst('lib/', ''))); | 56 path: p.url.join(id.package, id.path.replaceFirst('lib/', ''))); |
17 } | 57 } |
| 58 |
| 59 /// Converts [uri] into an [AssetId] if its path is within "packages". |
| 60 /// |
| 61 /// If the URL contains a special directory, but lacks a following package name, |
| 62 /// throws a [FormatException]. |
| 63 /// |
| 64 /// If the URI doesn't contain one of those special directories, returns null. |
18 AssetId packagesUrlToId(Uri url) { | 65 AssetId packagesUrlToId(Uri url) { |
19 var parts = p.url.split(url.path); | 66 var parts = p.url.split(url.path); |
| 67 |
| 68 // Strip the leading "/" from the URL. |
20 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1).toList(); | 69 if (parts.isNotEmpty && parts.first == "/") parts = parts.skip(1).toList(); |
| 70 |
21 if (parts.isEmpty) return null; | 71 if (parts.isEmpty) return null; |
| 72 |
| 73 // Check for "packages" in the URL. |
| 74 // TODO(rnystrom): If we rewrite "package:" imports to relative imports that |
| 75 // point to a canonical "packages" directory, we can limit "packages" to the |
| 76 // root of the URL as well. See: #16649. |
22 var index = parts.indexOf("packages"); | 77 var index = parts.indexOf("packages"); |
23 if (index == -1) return null; | 78 if (index == -1) return null; |
| 79 |
| 80 // There should be a package name after "packages". |
24 if (parts.length <= index + 1) { | 81 if (parts.length <= index + 1) { |
25 throw new FormatException( | 82 throw new FormatException( |
26 'Invalid URL path "${url.path}". Expected package name ' 'after "package
s".'); | 83 'Invalid URL path "${url.path}". Expected package name ' 'after "package
s".'); |
27 } | 84 } |
| 85 |
28 var package = parts[index + 1]; | 86 var package = parts[index + 1]; |
29 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))); |
30 return new AssetId(package, assetPath); | 88 return new AssetId(package, assetPath); |
31 } | 89 } |
OLD | NEW |