OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub.pubspec; | 5 library pub.pubspec; |
6 | 6 |
7 import 'package:path/path.dart' as path; | 7 import 'package:path/path.dart' as path; |
8 import 'package:source_maps/source_maps.dart'; | 8 import 'package:source_span/source_span.dart'; |
9 import 'package:yaml/yaml.dart'; | 9 import 'package:yaml/yaml.dart'; |
10 | 10 |
11 import 'barback/transformer_config.dart'; | 11 import 'barback/transformer_config.dart'; |
12 import 'exceptions.dart'; | 12 import 'exceptions.dart'; |
13 import 'io.dart'; | 13 import 'io.dart'; |
14 import 'package.dart'; | 14 import 'package.dart'; |
15 import 'source_registry.dart'; | 15 import 'source_registry.dart'; |
16 import 'utils.dart'; | 16 import 'utils.dart'; |
17 import 'version.dart'; | 17 import 'version.dart'; |
18 | 18 |
(...skipping 12 matching lines...) Expand all Loading... |
31 /// [devDependencies]. | 31 /// [devDependencies]. |
32 /// | 32 /// |
33 /// This will be null if this was created using [new Pubspec] or [new | 33 /// This will be null if this was created using [new Pubspec] or [new |
34 /// Pubspec.empty]. | 34 /// Pubspec.empty]. |
35 final SourceRegistry _sources; | 35 final SourceRegistry _sources; |
36 | 36 |
37 /// The location from which the pubspec was loaded. | 37 /// The location from which the pubspec was loaded. |
38 /// | 38 /// |
39 /// This can be null if the pubspec was created in-memory or if its location | 39 /// This can be null if the pubspec was created in-memory or if its location |
40 /// is unknown. | 40 /// is unknown. |
41 Uri get _location => fields.span.sourceUrl == null ? null : | 41 Uri get _location => fields.span.sourceUrl; |
42 Uri.parse(fields.span.sourceUrl); | |
43 | 42 |
44 /// All pubspec fields. | 43 /// All pubspec fields. |
45 /// | 44 /// |
46 /// This includes the fields from which other properties are derived. | 45 /// This includes the fields from which other properties are derived. |
47 final YamlMap fields; | 46 final YamlMap fields; |
48 | 47 |
49 /// The package's name. | 48 /// The package's name. |
50 String get name { | 49 String get name { |
51 if (_name != null) return _name; | 50 if (_name != null) return _name; |
52 | 51 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 249 |
251 /// Returns a Pubspec object for an already-parsed map representing its | 250 /// Returns a Pubspec object for an already-parsed map representing its |
252 /// contents. | 251 /// contents. |
253 /// | 252 /// |
254 /// If [expectedName] is passed and the pubspec doesn't have a matching name | 253 /// If [expectedName] is passed and the pubspec doesn't have a matching name |
255 /// field, this will throw a [PubspecError]. | 254 /// field, this will throw a [PubspecError]. |
256 /// | 255 /// |
257 /// [location] is the location from which this pubspec was loaded. | 256 /// [location] is the location from which this pubspec was loaded. |
258 Pubspec.fromMap(Map fields, this._sources, {String expectedName, | 257 Pubspec.fromMap(Map fields, this._sources, {String expectedName, |
259 Uri location}) | 258 Uri location}) |
260 : fields = fields is YamlMap ? fields : new YamlMap.wrap(fields, | 259 : fields = fields is YamlMap ? fields : |
261 sourceName: location == null ? null : location.toString()) { | 260 new YamlMap.wrap(fields, sourceUrl: location) { |
262 // If [expectedName] is passed, ensure that the actual 'name' field exists | 261 // If [expectedName] is passed, ensure that the actual 'name' field exists |
263 // and matches the expectation. | 262 // and matches the expectation. |
264 if (expectedName == null) return; | 263 if (expectedName == null) return; |
265 if (name == expectedName) return; | 264 if (name == expectedName) return; |
266 | 265 |
267 throw new PubspecException('"name" field doesn\'t match expected name ' | 266 throw new PubspecException('"name" field doesn\'t match expected name ' |
268 '"$expectedName".', this.fields.nodes["name"].span); | 267 '"$expectedName".', this.fields.nodes["name"].span); |
269 } | 268 } |
270 | 269 |
271 /// Parses the pubspec stored at [filePath] whose text is [contents]. | 270 /// Parses the pubspec stored at [filePath] whose text is [contents]. |
272 /// | 271 /// |
273 /// If the pubspec doesn't define a version for itself, it defaults to | 272 /// If the pubspec doesn't define a version for itself, it defaults to |
274 /// [Version.none]. | 273 /// [Version.none]. |
275 factory Pubspec.parse(String contents, SourceRegistry sources, | 274 factory Pubspec.parse(String contents, SourceRegistry sources, |
276 {String expectedName, Uri location}) { | 275 {String expectedName, Uri location}) { |
277 if (contents.trim() == '') return new Pubspec.empty(); | 276 if (contents.trim() == '') return new Pubspec.empty(); |
278 | 277 |
279 var pubspecNode = loadYamlNode(contents, sourceName: location.toString()); | 278 var pubspecNode = loadYamlNode(contents, sourceUrl: location); |
280 if (pubspecNode is YamlScalar && pubspecNode.value == null) { | 279 if (pubspecNode is YamlScalar && pubspecNode.value == null) { |
281 pubspecNode = new YamlMap(); | 280 pubspecNode = new YamlMap(); |
282 } else if (pubspecNode is! YamlMap) { | 281 } else if (pubspecNode is! YamlMap) { |
283 throw new PubspecException( | 282 throw new PubspecException( |
284 'The pubspec must be a YAML mapping.', pubspecNode.span); | 283 'The pubspec must be a YAML mapping.', pubspecNode.span); |
285 } | 284 } |
286 | 285 |
287 return new Pubspec.fromMap(pubspecNode, sources, | 286 return new Pubspec.fromMap(pubspecNode, sources, |
288 expectedName: expectedName, location: location); | 287 expectedName: expectedName, location: location); |
289 } | 288 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 'appear in both "dependencies" and "dev_dependencies".', | 423 'appear in both "dependencies" and "dev_dependencies".', |
425 span); | 424 span); |
426 } | 425 } |
427 | 426 |
428 /// Runs [fn] and wraps any [FormatException] it throws in a | 427 /// Runs [fn] and wraps any [FormatException] it throws in a |
429 /// [PubspecException]. | 428 /// [PubspecException]. |
430 /// | 429 /// |
431 /// [description] should be a noun phrase that describes whatever's being | 430 /// [description] should be a noun phrase that describes whatever's being |
432 /// parsed or processed by [fn]. [span] should be the location of whatever's | 431 /// parsed or processed by [fn]. [span] should be the location of whatever's |
433 /// being processed within the pubspec. | 432 /// being processed within the pubspec. |
434 _wrapFormatException(String description, Span span, fn()) { | 433 _wrapFormatException(String description, SourceSpan span, fn()) { |
435 try { | 434 try { |
436 return fn(); | 435 return fn(); |
437 } on FormatException catch (e) { | 436 } on FormatException catch (e) { |
438 _error('Invalid $description: ${e.message}', span); | 437 _error('Invalid $description: ${e.message}', span); |
439 } | 438 } |
440 } | 439 } |
441 | 440 |
442 _wrapSpanFormatException(String description, fn()) { | 441 _wrapSpanFormatException(String description, fn()) { |
443 try { | 442 try { |
444 return fn(); | 443 return fn(); |
445 } on SpanFormatException catch (e) { | 444 } on SourceSpanFormatException catch (e) { |
446 _error('Invalid $description: ${e.message}', e.span); | 445 _error('Invalid $description: ${e.message}', e.span); |
447 } | 446 } |
448 } | 447 } |
449 | 448 |
450 /// Throws a [PubspecException] with the given message. | 449 /// Throws a [PubspecException] with the given message. |
451 void _error(String message, Span span) { | 450 void _error(String message, SourceSpan span) { |
452 var name; | 451 var name; |
453 try { | 452 try { |
454 name = this.name; | 453 name = this.name; |
455 } on PubspecException catch (_) { | 454 } on PubspecException catch (_) { |
456 // [name] is null. | 455 // [name] is null. |
457 } | 456 } |
458 | 457 |
459 throw new PubspecException(message, span); | 458 throw new PubspecException(message, span); |
460 } | 459 } |
461 } | 460 } |
462 | 461 |
463 /// The environment-related metadata in the pubspec. | 462 /// The environment-related metadata in the pubspec. |
464 /// | 463 /// |
465 /// Corresponds to the data under the "environment:" key in the pubspec. | 464 /// Corresponds to the data under the "environment:" key in the pubspec. |
466 class PubspecEnvironment { | 465 class PubspecEnvironment { |
467 /// The version constraint specifying which SDK versions this package works | 466 /// The version constraint specifying which SDK versions this package works |
468 /// with. | 467 /// with. |
469 final VersionConstraint sdkVersion; | 468 final VersionConstraint sdkVersion; |
470 | 469 |
471 PubspecEnvironment([VersionConstraint sdk]) | 470 PubspecEnvironment([VersionConstraint sdk]) |
472 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; | 471 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; |
473 } | 472 } |
474 | 473 |
475 /// An exception thrown when parsing a pubspec. | 474 /// An exception thrown when parsing a pubspec. |
476 /// | 475 /// |
477 /// These exceptions are often thrown lazily while accessing pubspec properties. | 476 /// These exceptions are often thrown lazily while accessing pubspec properties. |
478 class PubspecException extends SpanFormatException | 477 class PubspecException extends SourceSpanFormatException |
479 implements ApplicationException { | 478 implements ApplicationException { |
480 PubspecException(String message, Span span) | 479 PubspecException(String message, SourceSpan span) |
481 : super(message, span); | 480 : super(message, span); |
482 } | 481 } |
483 | 482 |
484 /// Returns whether [uri] is a file URI. | 483 /// Returns whether [uri] is a file URI. |
485 /// | 484 /// |
486 /// This is slightly more complicated than just checking if the scheme is | 485 /// This is slightly more complicated than just checking if the scheme is |
487 /// 'file', since relative URIs also refer to the filesystem on the VM. | 486 /// 'file', since relative URIs also refer to the filesystem on the VM. |
488 bool _isFileUri(Uri uri) => uri.scheme == 'file' || uri.scheme == ''; | 487 bool _isFileUri(Uri uri) => uri.scheme == 'file' || uri.scheme == ''; |
OLD | NEW |