@@ -11,7 +11,6 @@ import 'package:analyzer/file_system/file_system.dart';
11
11
import 'package:analyzer/file_system/physical_file_system.dart' ;
12
12
// ignore: implementation_imports
13
13
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
14
- import 'package:dartdoc/src/dartdoc_options.dart' ;
15
14
import 'package:dartdoc/src/failure.dart' ;
16
15
import 'package:meta/meta.dart' ;
17
16
import 'package:path/path.dart' as path;
@@ -23,9 +22,13 @@ class PackageMetaFailure extends DartdocFailure {
23
22
PackageMetaFailure (super .message);
24
23
}
25
24
26
- /// For each list in this list, at least one of the given paths must exist
27
- /// for this to be detected as an SDK. Update `_writeMockSdkBinFiles` in
28
- /// `test/src/utils.dart` if removing any entries here.
25
+ /// Various relative paths that indicate that a root direoctory is an SDK (Dart
26
+ /// or Flutter).
27
+ ///
28
+ /// For a given directory to be detected as an SDK, at least one of the given
29
+ /// paths must exist, for each list of paths.
30
+ // Update `_writeMockSdkBinFiles` in `test/src/utils.dart` if removing any
31
+ // entries here.
29
32
const List <List <String >> _sdkDirFilePathsPosix = [
30
33
['bin/dart.bat' , 'bin/dart.exe' , 'bin/dart' ],
31
34
['include/dart_version.h' ],
@@ -43,7 +46,6 @@ final PackageMetaProvider pubPackageMetaProvider = PackageMetaProvider(
43
46
.parent
44
47
.parent,
45
48
Platform .environment,
46
- messageForMissingPackageMeta: PubPackageMeta .messageForMissingPackageMeta,
47
49
);
48
50
49
51
/// Sets the supported way of constructing [PackageMeta] objects.
@@ -65,9 +67,6 @@ class PackageMetaProvider {
65
67
final DartSdk ? defaultSdk;
66
68
final Map <String , String > environmentProvider;
67
69
68
- final String Function (LibraryElement , DartdocOptionContext )
69
- _messageForMissingPackageMeta;
70
-
71
70
PackageMetaProvider (
72
71
this ._fromElement,
73
72
this ._fromFilename,
@@ -76,24 +75,12 @@ class PackageMetaProvider {
76
75
this .defaultSdkDir,
77
76
this .environmentProvider, {
78
77
this .defaultSdk,
79
- String Function (LibraryElement , DartdocOptionContext )?
80
- messageForMissingPackageMeta,
81
- }) : _messageForMissingPackageMeta = messageForMissingPackageMeta ??
82
- _defaultMessageForMissingPackageMeta;
78
+ });
83
79
84
80
PackageMeta ? fromElement (LibraryElement library, String s) =>
85
81
_fromElement (library, s, resourceProvider);
86
82
PackageMeta ? fromFilename (String s) => _fromFilename (s, resourceProvider);
87
83
PackageMeta ? fromDir (Folder dir) => _fromDir (dir, resourceProvider);
88
-
89
- String getMessageForMissingPackageMeta (
90
- LibraryElement library, DartdocOptionContext optionContext) =>
91
- _messageForMissingPackageMeta (library, optionContext);
92
-
93
- static String _defaultMessageForMissingPackageMeta (
94
- LibraryElement library, DartdocOptionContext optionContext) {
95
- return 'Unknown package for library: ${library .source .fullName }' ;
96
- }
97
84
}
98
85
99
86
/// Describes a single package in the context of `dartdoc` .
@@ -104,6 +91,8 @@ class PackageMetaProvider {
104
91
///
105
92
/// Overriding this is typically done by overriding factories as rest of
106
93
/// `dartdoc` creates this object by calling these static factories.
94
+ // This class has a single direct subclass in this package, [PubPackageMeta],
95
+ // but has other subclasses in google3.
107
96
abstract class PackageMeta {
108
97
final Folder dir;
109
98
@@ -112,25 +101,22 @@ abstract class PackageMeta {
112
101
PackageMeta (this .dir, this .resourceProvider);
113
102
114
103
@override
115
- bool operator == (Object other) {
116
- if (other is ! PackageMeta ) return false ;
117
- var otherMeta = other;
118
- return resourceProvider.pathContext.equals (dir.path, otherMeta.dir.path);
119
- }
104
+ bool operator == (Object other) =>
105
+ other is PackageMeta && _pathContext.equals (dir.path, other.dir.path);
120
106
121
107
@override
122
- int get hashCode => pathContext .hash (pathContext .absolute (dir.path));
108
+ int get hashCode => _pathContext .hash (_pathContext .absolute (dir.path));
123
109
124
- path.Context get pathContext => resourceProvider.pathContext;
110
+ path.Context get _pathContext => resourceProvider.pathContext;
125
111
126
- /// Returns true if this represents a 'Dart' SDK.
112
+ /// Whether this represents a 'Dart' SDK.
127
113
///
128
- /// A package can be part of Dart and Flutter at the same time, but if we are
114
+ /// A package can be part of Dart and Flutter at the same time, but if this is
129
115
/// part of a Dart SDK, [sdkType] should never return null.
130
116
bool get isSdk;
131
117
132
118
/// Returns 'Dart' or 'Flutter' (preferentially, 'Flutter' when the answer is
133
- /// "both"), or null if this package is not part of a SDK.
119
+ /// "both"), or ` null` if this package is not part of an SDK.
134
120
String ? sdkType (String ? flutterRootPath);
135
121
136
122
bool get requiresFlutter;
@@ -147,14 +133,12 @@ abstract class PackageMeta {
147
133
148
134
File ? getReadmeContents ();
149
135
150
- /// Returns true if we are a valid package, valid enough to generate docs.
136
+ /// Whether this is a valid package ( valid enough to generate docs) .
151
137
bool get isValid => getInvalidReasons ().isEmpty;
152
138
153
- /// Returns resolved directory.
154
139
String get resolvedDir;
155
140
156
- /// Returns a list of reasons this package is invalid, or an
157
- /// empty list if no reasons found.
141
+ /// The list of reasons this package is invalid.
158
142
///
159
143
/// If the list is empty, this package is valid.
160
144
List <String > getInvalidReasons ();
@@ -179,8 +163,8 @@ abstract class PubPackageMeta extends PackageMeta {
179
163
180
164
static final _sdkDirParent = < String , Folder ? > {};
181
165
182
- /// If [folder] is inside a Dart SDK, returns the directory of the SDK, and `null`
183
- /// otherwise.
166
+ /// If [folder] is inside a Dart SDK, returns the directory of the SDK, and
167
+ /// `null` otherwise.
184
168
static Folder ? sdkDirParent (
185
169
Folder folder, ResourceProvider resourceProvider) {
186
170
var pathContext = resourceProvider.pathContext;
@@ -200,7 +184,7 @@ abstract class PubPackageMeta extends PackageMeta {
200
184
return _sdkDirParent[dirPathCanonical];
201
185
}
202
186
203
- /// Use this instead of fromDir where possible.
187
+ /// Use this instead of [ fromDir] where possible.
204
188
static PackageMeta ? fromElement (LibraryElement libraryElement, String sdkDir,
205
189
ResourceProvider resourceProvider) {
206
190
if (libraryElement.isInSdk) {
@@ -223,64 +207,49 @@ abstract class PubPackageMeta extends PackageMeta {
223
207
224
208
/// This factory is guaranteed to return the same object for any given
225
209
/// `dir.absolute.path` . Multiple `dir.absolute.path` s will resolve to the
226
- /// same object if they are part of the same package. Returns null
227
- /// if the directory is not part of a known package.
210
+ /// same object if they are part of the same package. Returns ` null` if the
211
+ /// directory is not part of a known package.
228
212
static PackageMeta ? fromDir (
229
213
Folder folder, ResourceProvider resourceProvider) {
230
214
var pathContext = resourceProvider.pathContext;
231
- var original =
232
- resourceProvider.getFolder (pathContext.absolute (folder.path));
233
- folder = original;
234
- if (! original.exists) {
215
+ folder = resourceProvider.getFolder (pathContext.absolute (folder.path));
216
+ if (! folder.exists) {
235
217
throw PackageMetaFailure (
236
- 'fatal error: unable to locate the input directory at '
237
- "'${original .path }'." );
218
+ 'fatal error: unable to locate the input directory at '
219
+ "'${folder .path }'." ,
220
+ );
238
221
}
239
222
240
- if (! _packageMetaCache.containsKey (folder.path)) {
241
- PackageMeta ? packageMeta;
223
+ return _packageMetaCache.putIfAbsent (pathContext.absolute (folder.path), () {
242
224
// There are pubspec.yaml files inside the SDK. Ignore them.
243
225
var parentSdkDir = sdkDirParent (folder, resourceProvider);
244
226
if (parentSdkDir != null ) {
245
- packageMeta = _SdkMeta (parentSdkDir, resourceProvider);
227
+ return _SdkMeta (parentSdkDir, resourceProvider);
246
228
} else {
247
229
for (var dir in folder.withAncestors) {
248
230
var pubspec = resourceProvider
249
231
.getFile (pathContext.join (dir.path, 'pubspec.yaml' ));
250
232
if (pubspec.exists) {
251
- packageMeta = _FilePackageMeta (dir, resourceProvider);
252
- break ;
233
+ return _FilePackageMeta (dir, resourceProvider);
253
234
}
254
235
}
255
236
}
256
- _packageMetaCache[pathContext.absolute (folder.path)] = packageMeta;
257
- }
258
- return _packageMetaCache[pathContext.absolute (folder.path)];
259
- }
260
-
261
- /// Create a helpful user error message for a case where a package can not
262
- /// be found.
263
- static String messageForMissingPackageMeta (
264
- LibraryElement library, DartdocOptionContext optionContext) {
265
- var libraryString = library.librarySource.fullName;
266
- var dartOrFlutter = optionContext.flutterRoot == null ? 'dart' : 'flutter' ;
267
- return 'Unknown package for library: $libraryString . Consider `$dartOrFlutter pub get` and/or '
268
- '`$dartOrFlutter pub global deactivate dartdoc` followed by `$dartOrFlutter pub global activate dartdoc` to fix. '
269
- 'Also, be sure that `$dartOrFlutter analyze` completes without errors.' ;
237
+ return null ;
238
+ });
270
239
}
271
240
272
241
@override
273
242
String ? sdkType (String ? flutterRootPath) {
274
243
if (flutterRootPath != null ) {
275
- var flutterPackages = pathContext .join (flutterRootPath, 'packages' );
276
- var flutterBinCache = pathContext .join (flutterRootPath, 'bin' , 'cache' );
244
+ var flutterPackages = _pathContext .join (flutterRootPath, 'packages' );
245
+ var flutterBinCache = _pathContext .join (flutterRootPath, 'bin' , 'cache' );
277
246
278
247
/// Don't include examples or other non-SDK components as being the
279
248
/// "Flutter SDK".
280
- var canonicalizedDir = pathContext
249
+ var canonicalizedDir = _pathContext
281
250
.canonicalize (resourceProvider.pathContext.absolute (dir.path));
282
- if (pathContext .isWithin (flutterPackages, canonicalizedDir) ||
283
- pathContext .isWithin (flutterBinCache, canonicalizedDir)) {
251
+ if (_pathContext .isWithin (flutterPackages, canonicalizedDir) ||
252
+ _pathContext .isWithin (flutterBinCache, canonicalizedDir)) {
284
253
return 'Flutter' ;
285
254
}
286
255
}
@@ -313,17 +282,17 @@ class _FilePackageMeta extends PubPackageMeta {
313
282
// possibly by calculating hosting directly from pubspec.yaml or importing
314
283
// a pub library to do this.
315
284
// People could have a pub cache at root with Windows drive mappings.
316
- if (pathContext .split (pathContext .canonicalize (dir.path)).length >= 3 ) {
285
+ if (_pathContext .split (_pathContext .canonicalize (dir.path)).length >= 3 ) {
317
286
var pubCacheRoot = dir.parent.parent.parent.path;
318
287
// Check for directory structure too close to root.
319
288
if (pubCacheRoot != dir.parent.parent.path) {
320
- var hosted = pathContext .canonicalize (dir.parent.parent.path);
321
- var hostname = pathContext .canonicalize (dir.parent.path);
322
- if (pathContext .basename (hosted) == 'hosted' &&
289
+ var hosted = _pathContext .canonicalize (dir.parent.parent.path);
290
+ var hostname = _pathContext .canonicalize (dir.parent.path);
291
+ if (_pathContext .basename (hosted) == 'hosted' &&
323
292
resourceProvider
324
- .getFolder (pathContext .join (pubCacheRoot, '_temp' ))
293
+ .getFolder (_pathContext .join (pubCacheRoot, '_temp' ))
325
294
.exists) {
326
- hostedAt = pathContext .basename (hostname);
295
+ hostedAt = _pathContext .basename (hostname);
327
296
}
328
297
}
329
298
}
@@ -360,13 +329,10 @@ class _FilePackageMeta extends PubPackageMeta {
360
329
/// empty list if no reasons found.
361
330
@override
362
331
List <String > getInvalidReasons () {
363
- var reasons = < String > [];
364
- if (_pubspec.isEmpty) {
365
- reasons.add ('no pubspec.yaml found' );
366
- } else if (! _pubspec.containsKey ('name' )) {
367
- reasons.add ('no name found in pubspec.yaml' );
368
- }
369
- return reasons;
332
+ return [
333
+ if (_pubspec.isEmpty) 'no pubspec.yaml found' ,
334
+ if (! _pubspec.containsKey ('name' )) "no 'name' field found in pubspec.yaml"
335
+ ];
370
336
}
371
337
}
372
338
0 commit comments