Make `.packages` files be more clever about defaults. (#80)
* Make `.packages` files be more clever about defaults.
The `PackageConfig` for a `.packages` file now assigns a default language version of 2.7
to all packages, and if the package location ends in `/lib/`, it assumes the package's
root directory is the parent directory of that.
* Fix test.
* Fix another test.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0b5e156..3015523 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
- Updated to support new rules for picking `package_config.json` over
a specified `.packages`.
+- Deduce package root from `.packages` derived package configuration,
+ and default all such packages to language version 2.7.
## 1.9.1
diff --git a/lib/src/packages_file.dart b/lib/src/packages_file.dart
index e65e7e8..184b0dd 100644
--- a/lib/src/packages_file.dart
+++ b/lib/src/packages_file.dart
@@ -7,6 +7,12 @@
import "util.dart";
import "errors.dart";
+/// The language version prior to the release of language versioning.
+///
+/// This is the default language version used by all packages from a
+/// `.packages` file.
+final LanguageVersion _languageVersion = LanguageVersion(2, 7);
+
/// Parses a `.packages` file into a [PackageConfig].
///
/// The [source] is the byte content of a `.packages` file, assumed to be
@@ -100,17 +106,24 @@
"Package URI as location for package", source, separatorIndex + 1));
continue;
}
- if (!packageLocation.path.endsWith('/')) {
- packageLocation =
- packageLocation.replace(path: packageLocation.path + "/");
+ var path = packageLocation.path;
+ if (!path.endsWith('/')) {
+ path += "/";
+ packageLocation = packageLocation.replace(path: path);
}
if (packageNames.contains(packageName)) {
onError(PackageConfigFormatException(
"Same package name occured more than once", source, start));
continue;
}
+ var rootUri = packageLocation;
+ if (path.endsWith("/lib/")) {
+ // Assume default Pub package layout. Include package itself in root.
+ rootUri =
+ packageLocation.replace(path: path.substring(0, path.length - 4));
+ }
var package = SimplePackage.validate(
- packageName, packageLocation, packageLocation, null, null, (error) {
+ packageName, rootUri, packageLocation, _languageVersion, null, (error) {
if (error is ArgumentError) {
onError(PackageConfigFormatException(error.message, source));
} else {
diff --git a/test/parse_test.dart b/test/parse_test.dart
index 367b643..59a7e71 100644
--- a/test/parse_test.dart
+++ b/test/parse_test.dart
@@ -34,9 +34,9 @@
var foo = result["foo"];
expect(foo, isNotNull);
- expect(foo.root, Uri.parse("file:///foo/lib/"));
+ expect(foo.root, Uri.parse("file:///foo/"));
expect(foo.packageUriRoot, Uri.parse("file:///foo/lib/"));
- expect(foo.languageVersion, null);
+ expect(foo.languageVersion, LanguageVersion(2, 7));
});
test("valid empty", () {