| 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 lock_file; | 5 library lock_file; |
| 6 | 6 |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 import 'io.dart'; |
| 8 import 'package.dart'; | 9 import 'package.dart'; |
| 9 import 'source_registry.dart'; | 10 import 'source_registry.dart'; |
| 10 import 'utils.dart'; | 11 import 'utils.dart'; |
| 11 import 'version.dart'; | 12 import 'version.dart'; |
| 12 import '../../pkg/yaml/lib/yaml.dart'; | 13 import '../../pkg/yaml/lib/yaml.dart'; |
| 13 | 14 |
| 14 /// A parsed and validated `pubspec.lock` file. | 15 /// A parsed and validated `pubspec.lock` file. |
| 15 class LockFile { | 16 class LockFile { |
| 16 /// The packages this lockfile pins. | 17 /// The packages this lockfile pins. |
| 17 Map<String, PackageId> packages; | 18 Map<String, PackageId> packages; |
| 18 | 19 |
| 19 LockFile._(this.packages); | 20 LockFile._(this.packages); |
| 20 | 21 |
| 21 LockFile.empty() | 22 LockFile.empty() |
| 22 : packages = <String, PackageId>{}; | 23 : packages = <String, PackageId>{}; |
| 23 | 24 |
| 25 /// Loads a lockfile from [filePath]. |
| 26 factory LockFile.load(String filePath, SourceRegistry sources) { |
| 27 return LockFile._parse(filePath, readTextFile(filePath), sources); |
| 28 } |
| 29 |
| 30 /// Parses a lockfile whose text is [contents]. |
| 31 factory LockFile.parse(String contents, SourceRegistry sources) { |
| 32 return LockFile._parse(null, contents, sources); |
| 33 } |
| 34 |
| 24 /// Parses the lockfile whose text is [contents]. | 35 /// Parses the lockfile whose text is [contents]. |
| 25 factory LockFile.parse(String contents, SourceRegistry sources) { | 36 static LockFile _parse(String filePath, String contents, |
| 37 SourceRegistry sources) { |
| 26 var packages = <String, PackageId>{}; | 38 var packages = <String, PackageId>{}; |
| 27 | 39 |
| 28 if (contents.trim() == '') return new LockFile.empty(); | 40 if (contents.trim() == '') return new LockFile.empty(); |
| 29 var parsed = loadYaml(contents); | 41 var parsed = loadYaml(contents); |
| 30 | 42 |
| 31 if (parsed.containsKey('packages')) { | 43 if (parsed.containsKey('packages')) { |
| 32 var packageEntries = parsed['packages']; | 44 var packageEntries = parsed['packages']; |
| 33 | 45 |
| 34 packageEntries.forEach((name, spec) { | 46 packageEntries.forEach((name, spec) { |
| 35 // Parse the version. | 47 // Parse the version. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 throw new FormatException( | 59 throw new FormatException( |
| 48 'Could not find a source named $sourceName.'); | 60 'Could not find a source named $sourceName.'); |
| 49 } | 61 } |
| 50 var source = sources[sourceName]; | 62 var source = sources[sourceName]; |
| 51 | 63 |
| 52 // Parse the description. | 64 // Parse the description. |
| 53 if (!spec.containsKey('description')) { | 65 if (!spec.containsKey('description')) { |
| 54 throw new FormatException('Package $name is missing a description.'); | 66 throw new FormatException('Package $name is missing a description.'); |
| 55 } | 67 } |
| 56 var description = spec['description']; | 68 var description = spec['description']; |
| 57 source.validateDescription(description, fromLockFile: true); | 69 description = source.parseDescription(filePath, description, |
| 70 fromLockFile: true); |
| 58 | 71 |
| 59 var id = new PackageId(name, source, version, description); | 72 var id = new PackageId(name, source, version, description); |
| 60 | 73 |
| 61 // Validate the name. | 74 // Validate the name. |
| 62 if (name != id.name) { | 75 if (name != id.name) { |
| 63 throw new FormatException( | 76 throw new FormatException( |
| 64 "Package name $name doesn't match ${id.name}."); | 77 "Package name $name doesn't match ${id.name}."); |
| 65 } | 78 } |
| 66 | 79 |
| 67 packages[name] = id; | 80 packages[name] = id; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 }); | 96 }); |
| 84 | 97 |
| 85 // TODO(nweiz): Serialize using the YAML library once it supports | 98 // TODO(nweiz): Serialize using the YAML library once it supports |
| 86 // serialization. For now, we use JSON, since it's a subset of YAML anyway. | 99 // serialization. For now, we use JSON, since it's a subset of YAML anyway. |
| 87 return ''' | 100 return ''' |
| 88 # Generated by pub. See: http://pub.dartlang.org/doc/glossary.html#lockf
ile | 101 # Generated by pub. See: http://pub.dartlang.org/doc/glossary.html#lockf
ile |
| 89 ${json.stringify({'packages': packagesObj})} | 102 ${json.stringify({'packages': packagesObj})} |
| 90 '''; | 103 '''; |
| 91 } | 104 } |
| 92 } | 105 } |
| OLD | NEW |