Skip to content

Commit

Permalink
Add support for writing defaultPackageName (dart-lang/package_config#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfj authored and lrhn committed Sep 2, 2019
1 parent 375e28c commit 8e0739f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkgs/package_config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 21 additions & 1 deletion pkgs/package_config/lib/packages_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ Map<String, Uri> parse(List<int> 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<String, Uri> 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");
}
Expand All @@ -128,6 +133,21 @@ void write(StringSink output, Map<String, Uri> 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');
Expand Down
2 changes: 1 addition & 1 deletion pkgs/package_config/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/dart-lang/package_config
Expand Down
47 changes: 45 additions & 2 deletions pkgs/package_config/test/parse_write_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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),
);
});
});
}

Expand Down Expand Up @@ -82,8 +117,16 @@ main() {
});
}

String writeToString(Map<String, Uri> map, {Uri baseUri, String comment}) {
String writeToString(
Map<String, Uri> 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();
}

0 comments on commit 8e0739f

Please sign in to comment.