| 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'); | 7 #import('dart:json'); |
| 8 #import('package.dart'); | 8 #import('package.dart'); |
| 9 #import('source_registry.dart'); | 9 #import('source_registry.dart'); |
| 10 #import('utils.dart'); | 10 #import('utils.dart'); |
| 11 #import('version.dart'); | 11 #import('version.dart'); |
| 12 #import('yaml/yaml.dart'); | 12 #import('yaml/yaml.dart'); |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * A parsed and validated `pubspec.lock` file. | 15 * A parsed and validated `pubspec.lock` file. |
| 16 */ | 16 */ |
| 17 class LockFile { | 17 class LockFile { |
| 18 /** | 18 /** |
| 19 * The packages this lockfile pins. | 19 * The packages this lockfile pins. |
| 20 */ | 20 */ |
| 21 Map<String, PackageId> packages; | 21 Map<String, PackageId> packages; |
| 22 | 22 |
| 23 LockFile._(this.packages); | 23 LockFile._(this.packages); |
| 24 | 24 |
| 25 LockFile.empty() | 25 LockFile.empty() |
| 26 : packages = <PackageId>{}; | 26 : packages = <String, PackageId>{}; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Parses the lockfile whose text is [contents]. | 29 * Parses the lockfile whose text is [contents]. |
| 30 */ | 30 */ |
| 31 factory LockFile.parse(String contents, SourceRegistry sources) { | 31 factory LockFile.parse(String contents, SourceRegistry sources) { |
| 32 var packages = <PackageId>{}; | 32 var packages = <String, PackageId>{}; |
| 33 | 33 |
| 34 if (contents.trim() == '') return new LockFile.empty(); | 34 if (contents.trim() == '') return new LockFile.empty(); |
| 35 | 35 |
| 36 var parsed = loadYaml(contents); | 36 var parsed = loadYaml(contents); |
| 37 | 37 |
| 38 if (parsed.containsKey('packages')) { | 38 if (parsed.containsKey('packages')) { |
| 39 var packageEntries = parsed['packages']; | 39 var packageEntries = parsed['packages']; |
| 40 | 40 |
| 41 packageEntries.forEach((name, spec) { | 41 packageEntries.forEach((name, spec) { |
| 42 // Parse the version. | 42 // Parse the version. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 }); | 75 }); |
| 76 } | 76 } |
| 77 | 77 |
| 78 return new LockFile._(packages); | 78 return new LockFile._(packages); |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Returns the serialized YAML text of the lock file. | 82 * Returns the serialized YAML text of the lock file. |
| 83 */ | 83 */ |
| 84 String serialize() { | 84 String serialize() { |
| 85 var packagesObj = <Map>{}; | 85 var packagesObj = <String, Map>{}; |
| 86 packages.forEach((name, id) { | 86 packages.forEach((name, id) { |
| 87 packagesObj[name] = { | 87 packagesObj[name] = { |
| 88 'version': id.version.toString(), | 88 'version': id.version.toString(), |
| 89 'source': id.source.name, | 89 'source': id.source.name, |
| 90 'description': id.description | 90 'description': id.description |
| 91 }; | 91 }; |
| 92 }); | 92 }); |
| 93 | 93 |
| 94 // TODO(nweiz): Serialize using the YAML library once it supports | 94 // TODO(nweiz): Serialize using the YAML library once it supports |
| 95 // serialization. For now, we use JSON, since it's a subset of YAML anyway. | 95 // serialization. For now, we use JSON, since it's a subset of YAML anyway. |
| 96 return JSON.stringify({'packages': packagesObj}); | 96 return JSON.stringify({'packages': packagesObj}); |
| 97 } | 97 } |
| 98 } | 98 } |
| OLD | NEW |