Skip to content

Commit

Permalink
Make .packages files be more clever about defaults. (#80)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
lrhn committed Mar 18, 2020
1 parent c3301f6 commit 74c0bbb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 17 additions & 4 deletions lib/src/packages_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import "package_config_impl.dart";
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
Expand Down Expand Up @@ -100,17 +106,24 @@ PackageConfig parse(
"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 {
Expand Down
4 changes: 2 additions & 2 deletions test/parse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void main() {

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", () {
Expand Down

0 comments on commit 74c0bbb

Please sign in to comment.