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

Unified Diff: lib/src/lock_file.dart

Issue 1281043004: Make LockFile immutable. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Created 5 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
Index: lib/src/lock_file.dart
diff --git a/lib/src/lock_file.dart b/lib/src/lock_file.dart
index 99b233276294f5baeeffa3de32bbfadd9b67f9da..21a40e722f5bde4cbf48b267dae78b2487695a07 100644
--- a/lib/src/lock_file.dart
+++ b/lib/src/lock_file.dart
@@ -4,6 +4,8 @@
library pub.lock_file;
+import 'dart:collection';
+
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:source_span/source_span.dart';
@@ -16,23 +18,45 @@ import 'utils.dart';
/// A parsed and validated `pubspec.lock` file.
class LockFile {
+ /// The source registry with which the lock file's IDs are interpreted.
+ ///
+ /// This is `null` for an empty lock file, since there are no IDs to
+ /// interpret.
+ final SourceRegistry _sources;
+
/// The packages this lockfile pins.
- Map<String, PackageId> packages;
+ final Map<String, PackageId> packages;
/// Creates a new lockfile containing [ids].
- factory LockFile(List<PackageId> ids) {
- var lockFile = new LockFile.empty();
+ ///
+ /// Throws an [ArgumentError] if any package has an undefined source according
+ /// to [sources] or an unresolved ID according to [Source.isResolved].
+ factory LockFile(Iterable<PackageId> ids, SourceRegistry sources) {
+ var packages = {};
for (var id in ids) {
- if (!id.isRoot) lockFile.packages[id.name] = id;
+ if (id.isRoot) continue;
+
+ var source = sources[id.source];
+ if (source == null) {
+ throw new ArgumentError('Undefined source "$source" for package "$id".');
Bob Nystrom 2015/08/07 21:50:56 Long line. Also, does this do the right thing on
nweiz 2015/08/07 22:16:07 Done.
+ }
+
+ if (!source.isResolved(id)) {
+ throw new ArgumentError('ID "$id" is not resolved.');
+ }
+
+ packages[id.name] = id;
}
- return lockFile;
+ return new LockFile._(packages, sources);
}
- LockFile._(this.packages);
+ LockFile._(Map<String, PackageId> packages, this._sources)
+ : packages = new UnmodifiableMapView(packages);
LockFile.empty()
- : packages = <String, PackageId>{};
+ : packages = const {},
+ _sources = null;
/// Loads a lockfile from [filePath].
factory LockFile.load(String filePath, SourceRegistry sources) {
@@ -50,7 +74,7 @@ class LockFile {
/// `null`.
static LockFile _parse(String filePath, String contents,
SourceRegistry sources) {
- var packages = <String, PackageId>{};
+ var packages = {};
if (contents.trim() == '') return new LockFile.empty();
@@ -100,7 +124,7 @@ class LockFile {
});
}
- return new LockFile._(packages);
+ return new LockFile._(packages, sources);
}
/// If [condition] is `false` throws a format error with [message] for [node].
@@ -113,12 +137,12 @@ class LockFile {
///
/// [packageDir] is the containing directory of the root package, used to
/// properly serialize package descriptions.
- String serialize(String packageDir, SourceRegistry sources) {
+ String serialize(String packageDir) {
// Convert the dependencies to a simple object.
var data = {};
packages.forEach((name, package) {
- var description = sources[package.source].serializeDescription(packageDir,
- package.description);
+ var description = _sources[package.source]
+ .serializeDescription(packageDir, package.description);
data[name] = {
'version': package.version.toString(),

Powered by Google App Engine
This is Rietveld 408576698