4
4
5
5
import 'dart:collection' ;
6
6
7
+ import 'package:collection/collection.dart' ;
7
8
import 'package:path/path.dart' as p;
8
9
import 'package:package_config/packages_file.dart' as packages_file;
9
10
import 'package:pub_semver/pub_semver.dart' ;
@@ -28,30 +29,62 @@ class LockFile {
28
29
/// or `null` if no packages require the Flutter SDK.
29
30
final VersionConstraint flutterSdkConstraint;
30
31
32
+ /// Dependency names that appeared in the root package's `dependencies`
33
+ /// section.
34
+ final Set <String > _mainDependencies;
35
+
36
+ /// Dependency names that appeared in the root package's `dev_dependencies`
37
+ /// section.
38
+ final Set <String > _devDependencies;
39
+
40
+ /// Dependency names that appeared in the root package's
41
+ /// `dependency_overrides` section.
42
+ final Set <String > _overriddenDependencies;
43
+
31
44
/// Creates a new lockfile containing [ids] .
32
45
///
33
46
/// If passed, [dartSdkConstraint] represents the intersection of all Dart SDK
34
47
/// constraints for all locked packages. It defaults to
35
48
/// [VersionConstraint.any] . Similarly, [flutterSdkConstraint] represents the
36
49
/// intersection of all Flutter SDK constraints; however, it defaults to
37
50
/// `null` .
51
+ ///
52
+ /// If passed, [mainDependencies] , [devDependencies] , and
53
+ /// [overriddenDependencies] indicate which dependencies should be marked as
54
+ /// being listed in the main package's `dependencies` , `dev_dependencies` , and
55
+ /// `dependency_overrides` sections, respectively. These are consumed by the
56
+ /// analysis server to provide better auto-completion.
38
57
LockFile (Iterable <PackageId > ids,
39
58
{VersionConstraint dartSdkConstraint,
40
- VersionConstraint flutterSdkConstraint})
59
+ VersionConstraint flutterSdkConstraint,
60
+ Set <String > mainDependencies,
61
+ Set <String > devDependencies,
62
+ Set <String > overriddenDependencies})
41
63
: this ._(
42
64
new Map .fromIterable (ids.where ((id) => ! id.isRoot),
43
65
key: (id) => id.name),
44
66
dartSdkConstraint ?? VersionConstraint .any,
45
- flutterSdkConstraint);
46
-
47
- LockFile ._(Map <String , PackageId > packages, this .dartSdkConstraint,
48
- this .flutterSdkConstraint)
67
+ flutterSdkConstraint,
68
+ mainDependencies ?? const UnmodifiableSetView .empty (),
69
+ devDependencies ?? const UnmodifiableSetView .empty (),
70
+ overriddenDependencies ?? const UnmodifiableSetView .empty ());
71
+
72
+ LockFile ._(
73
+ Map <String , PackageId > packages,
74
+ this .dartSdkConstraint,
75
+ this .flutterSdkConstraint,
76
+ this ._mainDependencies,
77
+ this ._devDependencies,
78
+ this ._overriddenDependencies)
49
79
: packages = new UnmodifiableMapView (packages);
50
80
51
81
LockFile .empty ()
52
82
: packages = const {},
53
83
dartSdkConstraint = VersionConstraint .any,
54
- flutterSdkConstraint = null ;
84
+ flutterSdkConstraint = null ,
85
+ _mainDependencies = const UnmodifiableSetView .empty (),
86
+ _devDependencies = const UnmodifiableSetView .empty (),
87
+ _overriddenDependencies = const UnmodifiableSetView .empty ();
55
88
56
89
/// Loads a lockfile from [filePath] .
57
90
factory LockFile .load (String filePath, SourceRegistry sources) {
@@ -135,7 +168,13 @@ class LockFile {
135
168
});
136
169
}
137
170
138
- return new LockFile ._(packages, dartSdkConstraint, flutterSdkConstraint);
171
+ return new LockFile ._(
172
+ packages,
173
+ dartSdkConstraint,
174
+ flutterSdkConstraint,
175
+ const UnmodifiableSetView .empty (),
176
+ const UnmodifiableSetView .empty (),
177
+ const UnmodifiableSetView .empty ());
139
178
}
140
179
141
180
/// Asserts that [node] is a version constraint, and parses it.
@@ -170,18 +209,6 @@ class LockFile {
170
209
throw new SourceSpanFormatException (message, node.span);
171
210
}
172
211
173
- /// Returns a copy of this LockFile with [id] added.
174
- ///
175
- /// If there's already an ID with the same name as [id] in the LockFile, it's
176
- /// overwritten.
177
- LockFile setPackage (PackageId id) {
178
- if (id.isRoot) return this ;
179
-
180
- var packages = new Map <String , PackageId >.from (this .packages);
181
- packages[id.name] = id;
182
- return new LockFile ._(packages, dartSdkConstraint, flutterSdkConstraint);
183
- }
184
-
185
212
/// Returns a copy of this LockFile with a package named [name] removed.
186
213
///
187
214
/// Returns an identical [LockFile] if there's no package named [name] .
@@ -190,7 +217,8 @@ class LockFile {
190
217
191
218
var packages = new Map <String , PackageId >.from (this .packages);
192
219
packages.remove (name);
193
- return new LockFile ._(packages, dartSdkConstraint, flutterSdkConstraint);
220
+ return new LockFile ._(packages, dartSdkConstraint, flutterSdkConstraint,
221
+ _mainDependencies, _devDependencies, _overriddenDependencies);
194
222
}
195
223
196
224
/// Returns the contents of the `.packages` file generated from this lockfile.
@@ -228,7 +256,8 @@ class LockFile {
228
256
packageMap[name] = {
229
257
'version' : package.version.toString (),
230
258
'source' : package.source.name,
231
- 'description' : description
259
+ 'description' : description,
260
+ 'dependency' : _dependencyType (package.name)
232
261
};
233
262
});
234
263
@@ -244,4 +273,17 @@ class LockFile {
244
273
${yamlToString (data )}
245
274
""" ;
246
275
}
276
+
277
+ /// Returns the dependency classification for [package] .
278
+ String _dependencyType (String package) {
279
+ if (_mainDependencies.contains (package)) return 'direct main' ;
280
+ if (_devDependencies.contains (package)) return 'direct dev' ;
281
+
282
+ // If a package appears in `dependency_overrides` and another dependency
283
+ // section, the main section it appears in takes precedence.
284
+ if (_overriddenDependencies.contains (package)) {
285
+ return 'direct overridden' ;
286
+ }
287
+ return 'transitive' ;
288
+ }
247
289
}
0 commit comments