From a5016cc3a465299787fa11973b8681614ccedd0d Mon Sep 17 00:00:00 2001 From: Lasse Reichstein Holst Nielsen Date: Thu, 12 Mar 2020 12:48:22 +0100 Subject: [PATCH 1/3] 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. --- CHANGELOG.md | 2 ++ lib/src/packages_file.dart | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) 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 "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 @@ -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 { From 72a8e5ad39a5dda824c83af1c8ff75791b021d70 Mon Sep 17 00:00:00 2001 From: Lasse Reichstein Holst Nielsen Date: Mon, 16 Mar 2020 09:03:43 +0100 Subject: [PATCH 2/3] Fix test. --- test/parse_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parse_test.dart b/test/parse_test.dart index 367b643..4507ea9 100644 --- a/test/parse_test.dart +++ b/test/parse_test.dart @@ -34,7 +34,7 @@ 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); }); From d2f200a8c0db84d0c7c8d5f35d53ead32fba55ba Mon Sep 17 00:00:00 2001 From: Lasse Reichstein Holst Nielsen Date: Mon, 16 Mar 2020 09:57:03 +0100 Subject: [PATCH 3/3] Fix another test. --- test/parse_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parse_test.dart b/test/parse_test.dart index 4507ea9..59a7e71 100644 --- a/test/parse_test.dart +++ b/test/parse_test.dart @@ -36,7 +36,7 @@ void main() { expect(foo, isNotNull); 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", () {