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 import 'dart:collection'; | 5 import 'dart:collection'; |
6 | 6 |
7 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
8 import 'package:package_config/packages_file.dart' as packages_file; | 8 import 'package:package_config/packages_file.dart' as packages_file; |
9 import 'package:pub_semver/pub_semver.dart'; | 9 import 'package:pub_semver/pub_semver.dart'; |
10 import 'package:source_span/source_span.dart'; | 10 import 'package:source_span/source_span.dart'; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 /// `null`. | 69 /// `null`. |
70 static LockFile _parse(String filePath, String contents, | 70 static LockFile _parse(String filePath, String contents, |
71 SourceRegistry sources) { | 71 SourceRegistry sources) { |
72 if (contents.trim() == '') return new LockFile.empty(); | 72 if (contents.trim() == '') return new LockFile.empty(); |
73 | 73 |
74 var sourceUrl; | 74 var sourceUrl; |
75 if (filePath != null) sourceUrl = p.toUri(filePath); | 75 if (filePath != null) sourceUrl = p.toUri(filePath); |
76 var parsed = loadYamlNode(contents, sourceUrl: sourceUrl); | 76 var parsed = loadYamlNode(contents, sourceUrl: sourceUrl); |
77 | 77 |
78 _validate(parsed is Map, 'The lockfile must be a YAML mapping.', parsed); | 78 _validate(parsed is Map, 'The lockfile must be a YAML mapping.', parsed); |
| 79 var parsedMap = parsed as YamlMap; |
79 | 80 |
80 var dartSdkConstraint = VersionConstraint.any; | 81 var dartSdkConstraint = VersionConstraint.any; |
81 VersionConstraint flutterSdkConstraint; | 82 VersionConstraint flutterSdkConstraint; |
82 var sdkNode = parsed.nodes['sdk']; | 83 var sdkNode = parsedMap.nodes['sdk']; |
83 if (sdkNode != null) { | 84 if (sdkNode != null) { |
84 // Lockfiles produced by pub versions from 1.14.0 through 1.18.0 included | 85 // Lockfiles produced by pub versions from 1.14.0 through 1.18.0 included |
85 // a top-level "sdk" field which encoded the unified constraint on the | 86 // a top-level "sdk" field which encoded the unified constraint on the |
86 // Dart SDK. They had no way of specifying constraints on other SDKs. | 87 // Dart SDK. They had no way of specifying constraints on other SDKs. |
87 dartSdkConstraint = _parseVersionConstraint(sdkNode); | 88 dartSdkConstraint = _parseVersionConstraint(sdkNode); |
88 } else if ((parsed as Map).containsKey('sdks')) { | 89 } else if (parsedMap.containsKey('sdks')) { |
89 var sdksField = parsed['sdks']; | 90 var sdksField = parsedMap['sdks']; |
90 _validate( | 91 _validate( |
91 sdksField is Map, | 92 sdksField is Map, |
92 'The "sdks" field must be a mapping.', | 93 'The "sdks" field must be a mapping.', |
93 parsed.nodes['sdks']); | 94 parsedMap.nodes['sdks']); |
94 | 95 |
95 dartSdkConstraint = _parseVersionConstraint(sdksField.nodes['dart']); | 96 dartSdkConstraint = _parseVersionConstraint(sdksField.nodes['dart']); |
96 flutterSdkConstraint = | 97 flutterSdkConstraint = |
97 _parseVersionConstraint(sdksField.nodes['flutter']); | 98 _parseVersionConstraint(sdksField.nodes['flutter']); |
98 } | 99 } |
99 | 100 |
100 var packages = {}; | 101 var packages = <String, PackageId>{}; |
101 var packageEntries = parsed['packages']; | 102 var packageEntries = parsedMap['packages']; |
102 if (packageEntries != null) { | 103 if (packageEntries != null) { |
103 _validate(packageEntries is Map, 'The "packages" field must be a map.', | 104 _validate(packageEntries is Map, 'The "packages" field must be a map.', |
104 parsed.nodes['packages']); | 105 parsedMap.nodes['packages']); |
105 | 106 |
106 packageEntries.forEach((name, spec) { | 107 packageEntries.forEach((name, spec) { |
107 // Parse the version. | 108 // Parse the version. |
108 _validate(spec.containsKey('version'), | 109 _validate(spec.containsKey('version'), |
109 'Package $name is missing a version.', spec); | 110 'Package $name is missing a version.', spec); |
110 var version = new Version.parse(spec['version']); | 111 var version = new Version.parse(spec['version']); |
111 | 112 |
112 // Parse the source. | 113 // Parse the source. |
113 _validate(spec.containsKey('source'), | 114 _validate(spec.containsKey('source'), |
114 'Package $name is missing a source.', spec); | 115 'Package $name is missing a source.', spec); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 throw new SourceSpanFormatException(message, node.span); | 176 throw new SourceSpanFormatException(message, node.span); |
176 } | 177 } |
177 | 178 |
178 /// Returns a copy of this LockFile with [id] added. | 179 /// Returns a copy of this LockFile with [id] added. |
179 /// | 180 /// |
180 /// If there's already an ID with the same name as [id] in the LockFile, it's | 181 /// If there's already an ID with the same name as [id] in the LockFile, it's |
181 /// overwritten. | 182 /// overwritten. |
182 LockFile setPackage(PackageId id) { | 183 LockFile setPackage(PackageId id) { |
183 if (id.isRoot) return this; | 184 if (id.isRoot) return this; |
184 | 185 |
185 var packages = new Map.from(this.packages); | 186 var packages = new Map<String, PackageId>.from(this.packages); |
186 packages[id.name] = id; | 187 packages[id.name] = id; |
187 return new LockFile._(packages, dartSdkConstraint, flutterSdkConstraint); | 188 return new LockFile._(packages, dartSdkConstraint, flutterSdkConstraint); |
188 } | 189 } |
189 | 190 |
190 /// Returns a copy of this LockFile with a package named [name] removed. | 191 /// Returns a copy of this LockFile with a package named [name] removed. |
191 /// | 192 /// |
192 /// Returns an identical [LockFile] if there's no package named [name]. | 193 /// Returns an identical [LockFile] if there's no package named [name]. |
193 LockFile removePackage(String name) { | 194 LockFile removePackage(String name) { |
194 if (!this.packages.containsKey(name)) return this; | 195 if (!this.packages.containsKey(name)) return this; |
195 | 196 |
196 var packages = new Map.from(this.packages); | 197 var packages = new Map<String, PackageId>.from(this.packages); |
197 packages.remove(name); | 198 packages.remove(name); |
198 return new LockFile._(packages, dartSdkConstraint, flutterSdkConstraint); | 199 return new LockFile._(packages, dartSdkConstraint, flutterSdkConstraint); |
199 } | 200 } |
200 | 201 |
201 /// Returns the contents of the `.packages` file generated from this lockfile. | 202 /// Returns the contents of the `.packages` file generated from this lockfile. |
202 /// | 203 /// |
203 /// If [entrypoint] is passed, a relative entry is added for its "lib/" | 204 /// If [entrypoint] is passed, a relative entry is added for its "lib/" |
204 /// directory. | 205 /// directory. |
205 String packagesFile(SystemCache cache, [String entrypoint]) { | 206 String packagesFile(SystemCache cache, [String entrypoint]) { |
206 var header = "Generated by pub on ${new DateTime.now()}."; | 207 var header = "Generated by pub on ${new DateTime.now()}."; |
207 | 208 |
208 var map = new Map.fromIterable(ordered(packages.keys), value: (name) { | 209 var map = new Map<String, Uri>.fromIterable(ordered(packages.keys), |
| 210 value: (name) { |
209 var id = packages[name]; | 211 var id = packages[name]; |
210 var source = cache.source(id.source); | 212 var source = cache.source(id.source); |
211 return p.toUri(p.join(source.getDirectory(id), "lib")); | 213 return p.toUri(p.join(source.getDirectory(id), "lib")); |
212 }); | 214 }); |
213 | 215 |
214 if (entrypoint != null) map[entrypoint] = Uri.parse("lib/"); | 216 if (entrypoint != null) map[entrypoint] = Uri.parse("lib/"); |
215 | 217 |
216 var text = new StringBuffer(); | 218 var text = new StringBuffer(); |
217 packages_file.write(text, map, comment: header); | 219 packages_file.write(text, map, comment: header); |
218 return text.toString(); | 220 return text.toString(); |
(...skipping 25 matching lines...) Expand all Loading... |
244 } | 246 } |
245 | 247 |
246 var data = {'sdks': sdks, 'packages': packageMap}; | 248 var data = {'sdks': sdks, 'packages': packageMap}; |
247 return """ | 249 return """ |
248 # Generated by pub | 250 # Generated by pub |
249 # See http://pub.dartlang.org/doc/glossary.html#lockfile | 251 # See http://pub.dartlang.org/doc/glossary.html#lockfile |
250 ${yamlToString(data)} | 252 ${yamlToString(data)} |
251 """; | 253 """; |
252 } | 254 } |
253 } | 255 } |
OLD | NEW |