diff --git a/pkgs/package_config/CHANGELOG.md b/pkgs/package_config/CHANGELOG.md index db4bb00d6..048eedf94 100644 --- a/pkgs/package_config/CHANGELOG.md +++ b/pkgs/package_config/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.2.0 + - Added support for writing default-package entries. + ## 1.1.0 - Allow parsing files with default-package entries and metadata. diff --git a/pkgs/package_config/lib/packages_file.dart b/pkgs/package_config/lib/packages_file.dart index 284d8e90a..1fa18e740 100644 --- a/pkgs/package_config/lib/packages_file.dart +++ b/pkgs/package_config/lib/packages_file.dart @@ -106,10 +106,15 @@ Map parse(List source, Uri baseLocation, /// If [baseUri] is provided, package locations will be made relative /// to the base URI, if possible, before writing. /// +/// If [allowDefaultPackage] is `true`, the [packageMapping] may contain an +/// empty string mapping to the _default package name_. +/// /// All the keys of [packageMapping] must be valid package names, /// and the values must be URIs that do not have the `package:` scheme. void write(StringSink output, Map packageMapping, - {Uri baseUri, String comment}) { + {Uri baseUri, String comment, bool allowDefaultPackage = false}) { + ArgumentError.checkNotNull(allowDefaultPackage, 'allowDefaultPackage'); + if (baseUri != null && !baseUri.isAbsolute) { throw new ArgumentError.value(baseUri, "baseUri", "Must be absolute"); } @@ -128,6 +133,21 @@ void write(StringSink output, Map packageMapping, } packageMapping.forEach((String packageName, Uri uri) { + // If [packageName] is empty then [uri] is the _default package name_. + if (allowDefaultPackage && packageName.isEmpty) { + final defaultPackageName = uri.toString(); + if (!isValidPackageName(defaultPackageName)) { + throw ArgumentError.value( + defaultPackageName, + 'defaultPackageName', + '"$defaultPackageName" is not a valid package name', + ); + } + output.write(':'); + output.write(defaultPackageName); + output.writeln(); + return; + } // Validate packageName. if (!isValidPackageName(packageName)) { throw new ArgumentError('"$packageName" is not a valid package name'); diff --git a/pkgs/package_config/pubspec.yaml b/pkgs/package_config/pubspec.yaml index b51932eb7..72d299bf2 100644 --- a/pkgs/package_config/pubspec.yaml +++ b/pkgs/package_config/pubspec.yaml @@ -1,5 +1,5 @@ name: package_config -version: 1.1.0 +version: 1.2.0 description: Support for working with Package Resolution config files. author: Dart Team homepage: https://github.com/dart-lang/package_config diff --git a/pkgs/package_config/test/parse_write_test.dart b/pkgs/package_config/test/parse_write_test.dart index b963eb5b5..415b479e9 100644 --- a/pkgs/package_config/test/parse_write_test.dart +++ b/pkgs/package_config/test/parse_write_test.dart @@ -4,6 +4,7 @@ library package_config.parse_write_test; +import "dart:convert" show utf8; import "package:package_config/packages_file.dart"; import "package:test/test.dart"; @@ -32,6 +33,40 @@ main() { var resultMap = parse(content, packagesFile); expect(resultMap, map); }); + + test("write with defaultPackageName", () { + var content = writeToString( + {'': Uri.parse('my_pkg')}..addAll(map), + allowDefaultPackage: true, + ).codeUnits; + var resultMap = parse( + content, + packagesFile, + allowDefaultPackage: true, + ); + expect(resultMap[''].toString(), 'my_pkg'); + expect( + resultMap, + {'': Uri.parse('my_pkg')}..addAll(map), + ); + }); + + test("write with defaultPackageName (utf8)", () { + var content = utf8.encode(writeToString( + {'': Uri.parse('my_pkg')}..addAll(map), + allowDefaultPackage: true, + )); + var resultMap = parse( + content, + packagesFile, + allowDefaultPackage: true, + ); + expect(resultMap[''].toString(), 'my_pkg'); + expect( + resultMap, + {'': Uri.parse('my_pkg')}..addAll(map), + ); + }); }); } @@ -82,8 +117,16 @@ main() { }); } -String writeToString(Map map, {Uri baseUri, String comment}) { +String writeToString( + Map map, { + Uri baseUri, + String comment, + bool allowDefaultPackage = false, +}) { var buffer = new StringBuffer(); - write(buffer, map, baseUri: baseUri, comment: comment); + write(buffer, map, + baseUri: baseUri, + comment: comment, + allowDefaultPackage: allowDefaultPackage); return buffer.toString(); }