-
Notifications
You must be signed in to change notification settings - Fork 17
Add support for writing defaultPackageName and fragment in .packages #54
Conversation
lib/packages_file.dart
Outdated
@@ -106,10 +106,13 @@ 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 [defaultPackageName] is provided, the output will contain a | |||
/// `:<defaultPackageName>` entry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If defaultPackageName is provided, then the map must not contain an empty-named entry, otherwise it can (and we should probably check that it has a valid package name as value), and then that will be the default package name (and not get a trailing /
).
That's the format you get when reading a .packages
file, so we should support writing it too.
9a2311c
to
e7c5b8a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks.
/// 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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't bother with this check. It will fail below if the value is null
, and an optional parameter with a non-null
default value is only null
if someone explicitly passes that.
It is not documented that null
is accepted, so that should be assumed to be an error, and so rare that I don't want to add code for it.
(When comes NNBD, this check will be unnecessary anyway).
Now it's here, it's fine, but not something I'd usually bother doing.
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't bother using final
for local variables. Just use var
.
That's just personal style, but it's my package 😁.
(You can let it stay, I'll just remove it later).
defaultPackageName
.Uri
included a fragment.