3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:analyzer/file_system/file_system.dart' ;
6
+ import 'package:analyzer/src/context/packages.dart' ;
6
7
import 'package:analyzer/src/generated/source.dart' ;
8
+ import 'package:analyzer/src/lint/pub.dart' ;
7
9
import 'package:analyzer/src/workspace/simple.dart' ;
8
10
import 'package:analyzer/src/workspace/workspace.dart' ;
9
- import 'package:analyzer/src/context/packages.dart' ;
10
11
11
12
/// Information about a Pub workspace.
12
13
class PubWorkspace extends SimpleWorkspace {
@@ -18,10 +19,14 @@ class PubWorkspace extends SimpleWorkspace {
18
19
/// Each Pub workspace is itself one package.
19
20
PubWorkspacePackage _theOnlyPackage;
20
21
22
+ /// The associated pubspec file.
23
+ final File _pubspecFile;
24
+
21
25
PubWorkspace ._(
22
26
ResourceProvider provider,
23
27
Map <String , List <Folder >> packageMap,
24
28
String root,
29
+ this ._pubspecFile,
25
30
) : super (provider, packageMap, root);
26
31
27
32
@override
@@ -52,10 +57,11 @@ class PubWorkspace extends SimpleWorkspace {
52
57
return null ;
53
58
}
54
59
55
- if (folder.getChildAssumingFile (_pubspecName).exists) {
60
+ var pubspec = folder.getChildAssumingFile (_pubspecName);
61
+ if (pubspec.exists) {
56
62
// Found the pubspec.yaml file; this is our root.
57
63
String root = folder.path;
58
- return PubWorkspace ._(provider, packageMap, root);
64
+ return PubWorkspace ._(provider, packageMap, root, pubspec );
59
65
}
60
66
61
67
// Go up a folder.
@@ -73,11 +79,31 @@ class PubWorkspacePackage extends WorkspacePackage {
73
79
@override
74
80
final String root;
75
81
82
+ Pubspec _pubspec;
83
+
84
+ /// A flag to indicate if we've tried to parse the pubspec.
85
+ bool _parsedPubspec = false ;
86
+
76
87
@override
77
88
final PubWorkspace workspace;
78
89
79
90
PubWorkspacePackage (this .root, this .workspace);
80
91
92
+ /// Get the associated parsed [Pubspec] , or `null` if there was an error in
93
+ /// reading or parsing.
94
+ Pubspec get pubspec {
95
+ if (! _parsedPubspec) {
96
+ _parsedPubspec = true ;
97
+ try {
98
+ final content = workspace._pubspecFile.readAsStringSync ();
99
+ _pubspec = Pubspec .parse (content);
100
+ } catch (_) {
101
+ // Pubspec will be null.
102
+ }
103
+ }
104
+ return _pubspec;
105
+ }
106
+
81
107
@override
82
108
bool contains (Source source) {
83
109
String filePath = filePathFromSource (source);
0 commit comments