Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Unified Diff: sdk/lib/_internal/pub/lib/src/pubspec.dart

Issue 23295025: Handle invalid version syntax in pubspecs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Refactor a bit. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/test/pubspec_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/pub/lib/src/pubspec.dart
diff --git a/sdk/lib/_internal/pub/lib/src/pubspec.dart b/sdk/lib/_internal/pub/lib/src/pubspec.dart
index 6da96ba621cc6f2456a9a5bc41e00cc99bd0ac71..df7ebd93a349fe20ea8f8f4e84f4af605e59b990 100644
--- a/sdk/lib/_internal/pub/lib/src/pubspec.dart
+++ b/sdk/lib/_internal/pub/lib/src/pubspec.dart
@@ -121,7 +121,6 @@ void _validateFieldUrl(url, String field) {
Pubspec _parseMap(String filePath, Map map, SourceRegistry sources) {
var name = null;
- var version = Version.none;
if (map.containsKey('name')) {
name = map['name'];
@@ -131,9 +130,9 @@ Pubspec _parseMap(String filePath, Map map, SourceRegistry sources) {
}
}
- if (map.containsKey('version')) {
- version = new Version.parse(map['version']);
- }
+ var version = _parseVersion(map['version'], (v) =>
+ 'The pubspec "version" field should be a semantic version number, '
+ 'but was "$v".');
var dependencies = _parseDependencies(filePath, sources,
map['dependencies']);
@@ -183,14 +182,9 @@ Pubspec _parseMap(String filePath, Map map, SourceRegistry sources) {
'"$environmentYaml".');
}
- var sdkYaml = environmentYaml['sdk'];
- if (sdkYaml is! String) {
- throw new FormatException(
- 'The "sdk" field of "environment" should be a string, but was '
- '"$sdkYaml".');
- }
-
- sdkConstraint = new VersionConstraint.parse(sdkYaml);
+ sdkConstraint = _parseVersionConstraint(environmentYaml['sdk'], (v) =>
+ 'The "sdk" field of "environment" should be a semantic version '
+ 'constraint, but was "$v".');
}
var environment = new PubspecEnvironment(sdkConstraint);
@@ -241,6 +235,36 @@ Pubspec _parseMap(String filePath, Map map, SourceRegistry sources) {
environment, map);
}
+/// Parses [yaml] to a [Version] or throws a [FormatException] with the result
+/// of calling [message] if it isn't valid.
+///
+/// If [yaml] is `null`, returns [Version.none].
+Version _parseVersion(yaml, String message(yaml)) {
+ if (yaml == null) return Version.none;
+ if (yaml is! String) throw new FormatException(message(yaml));
+
+ try {
+ return new Version.parse(yaml);
+ } on FormatException catch(_) {
+ throw new FormatException(message(yaml));
+ }
+}
+
+/// Parses [yaml] to a [VersionConstraint] or throws a [FormatException] with
+/// the result of calling [message] if it isn't valid.
+///
+/// If [yaml] is `null`, returns [VersionConstraint.any].
+VersionConstraint _parseVersionConstraint(yaml, String getMessage(yaml)) {
+ if (yaml == null) return VersionConstraint.any;
+ if (yaml is! String) throw new FormatException(getMessage(yaml));
+
+ try {
+ return new VersionConstraint.parse(yaml);
+ } on FormatException catch(_) {
+ throw new FormatException(getMessage(yaml));
+ }
+}
+
List<PackageDep> _parseDependencies(String pubspecPath, SourceRegistry sources,
yaml) {
var dependencies = <PackageDep>[];
@@ -268,7 +292,9 @@ List<PackageDep> _parseDependencies(String pubspecPath, SourceRegistry sources,
versionConstraint = new VersionConstraint.parse(spec);
} else if (spec is Map) {
if (spec.containsKey('version')) {
- versionConstraint = new VersionConstraint.parse(spec.remove('version'));
+ versionConstraint = _parseVersionConstraint(spec.remove('version'),
+ (v) => 'The "version" field for $name should be a semantic '
+ 'version constraint, but was "$v".');
}
var sourceNames = spec.keys.toList();
« no previous file with comments | « no previous file | sdk/lib/_internal/pub/test/pubspec_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698