@@ -9,7 +9,7 @@ import 'dart:io';
9
9
10
10
import 'package:analyzer/dart/element/element.dart' ;
11
11
import 'package:dartdoc/dartdoc.dart' ;
12
- import 'package:path/path.dart' as path ;
12
+ import 'package:path/path.dart' as p ;
13
13
import 'package:yaml/yaml.dart' ;
14
14
15
15
import 'logging.dart' ;
@@ -19,7 +19,7 @@ Encoding utf8AllowMalformed = Utf8Codec(allowMalformed: true);
19
19
20
20
Directory get defaultSdkDir {
21
21
var sdkDir = File (Platform .resolvedExecutable).parent.parent;
22
- assert (path .equals (sdkDir.path, PubPackageMeta .sdkDirParent (sdkDir).path));
22
+ assert (p .equals (sdkDir.path, PubPackageMeta .sdkDirParent (sdkDir).path));
23
23
return sdkDir;
24
24
}
25
25
@@ -74,11 +74,11 @@ abstract class PackageMeta {
74
74
bool operator == (Object other) {
75
75
if (other is ! PackageMeta ) return false ;
76
76
PackageMeta otherMeta = other;
77
- return path .equals (dir.absolute.path, otherMeta.dir.absolute.path);
77
+ return p .equals (dir.absolute.path, otherMeta.dir.absolute.path);
78
78
}
79
79
80
80
@override
81
- int get hashCode => path .hash (dir.absolute.path);
81
+ int get hashCode => p .hash (dir.absolute.path);
82
82
83
83
/// Returns true if this represents a 'Dart' SDK. A package can be part of
84
84
/// Dart and Flutter at the same time, but if we are part of a Dart SDK
@@ -107,11 +107,11 @@ abstract class PackageMeta {
107
107
108
108
String get homepage;
109
109
110
- FileContents getReadmeContents ();
110
+ File getReadmeContents ();
111
111
112
- FileContents getLicenseContents ();
112
+ File getLicenseContents ();
113
113
114
- FileContents getChangelogContents ();
114
+ File getChangelogContents ();
115
115
116
116
/// Returns true if we are a valid package, valid enough to generate docs.
117
117
bool get isValid => getInvalidReasons ().isEmpty;
@@ -141,9 +141,9 @@ abstract class PubPackageMeta extends PackageMeta {
141
141
if (Platform .isWindows) {
142
142
for (var paths in __sdkDirFilePathsPosix) {
143
143
var windowsPaths = < String > [];
144
- for (var p in paths) {
145
- windowsPaths. add (
146
- path. joinAll (path .Context (style: path .Style .posix).split (p )));
144
+ for (var path in paths) {
145
+ windowsPaths
146
+ . add (p. joinAll (p .Context (style: p .Style .posix).split (path )));
147
147
}
148
148
__sdkDirFilePaths.add (windowsPaths);
149
149
}
@@ -159,17 +159,17 @@ abstract class PubPackageMeta extends PackageMeta {
159
159
static final Map <String , Directory > _sdkDirParent = {};
160
160
161
161
static Directory sdkDirParent (Directory dir) {
162
- var dirPathCanonical = path .canonicalize (dir.path);
162
+ var dirPathCanonical = p .canonicalize (dir.path);
163
163
if (! _sdkDirParent.containsKey (dirPathCanonical)) {
164
164
_sdkDirParent[dirPathCanonical] = null ;
165
165
while (dir.existsSync ()) {
166
166
if (_sdkDirFilePaths.every ((List <String > l) {
167
- return l.any ((f) => File (path .join (dir.path, f)).existsSync ());
167
+ return l.any ((f) => File (p .join (dir.path, f)).existsSync ());
168
168
})) {
169
169
_sdkDirParent[dirPathCanonical] = dir;
170
170
break ;
171
171
}
172
- if (path .equals (dir.path, dir.parent.path)) break ;
172
+ if (p .equals (dir.path, dir.parent.path)) break ;
173
173
dir = dir.parent;
174
174
}
175
175
}
@@ -183,7 +183,7 @@ abstract class PubPackageMeta extends PackageMeta {
183
183
return PubPackageMeta .fromDir (Directory (sdkDir));
184
184
}
185
185
return PubPackageMeta .fromDir (
186
- File (path .canonicalize (libraryElement.source.fullName)).parent);
186
+ File (p .canonicalize (libraryElement.source.fullName)).parent);
187
187
}
188
188
189
189
static PubPackageMeta fromFilename (String filename) {
@@ -210,14 +210,14 @@ abstract class PubPackageMeta extends PackageMeta {
210
210
packageMeta = _SdkMeta (parentSdkDir);
211
211
} else {
212
212
while (dir.existsSync ()) {
213
- var pubspec = File (path .join (dir.path, 'pubspec.yaml' ));
213
+ var pubspec = File (p .join (dir.path, 'pubspec.yaml' ));
214
214
if (pubspec.existsSync ()) {
215
215
packageMeta = _FilePackageMeta (dir);
216
216
break ;
217
217
}
218
218
// Allow a package to be at root (possible in a Windows setting with
219
219
// drive letter mappings).
220
- if (path .equals (dir.path, dir.parent.path)) break ;
220
+ if (p .equals (dir.path, dir.parent.path)) break ;
221
221
dir = dir.parent.absolute;
222
222
}
223
223
}
@@ -229,15 +229,13 @@ abstract class PubPackageMeta extends PackageMeta {
229
229
@override
230
230
String sdkType (String flutterRootPath) {
231
231
if (flutterRootPath != null ) {
232
- var flutterPackages = path .join (flutterRootPath, 'packages' );
233
- var flutterBinCache = path .join (flutterRootPath, 'bin' , 'cache' );
232
+ var flutterPackages = p .join (flutterRootPath, 'packages' );
233
+ var flutterBinCache = p .join (flutterRootPath, 'bin' , 'cache' );
234
234
235
235
/// Don't include examples or other non-SDK components as being the
236
236
/// "Flutter SDK".
237
- if (path.isWithin (
238
- flutterPackages, path.canonicalize (dir.absolute.path)) ||
239
- path.isWithin (
240
- flutterBinCache, path.canonicalize (dir.absolute.path))) {
237
+ if (p.isWithin (flutterPackages, p.canonicalize (dir.absolute.path)) ||
238
+ p.isWithin (flutterBinCache, p.canonicalize (dir.absolute.path))) {
241
239
return 'Flutter' ;
242
240
}
243
241
}
@@ -253,29 +251,18 @@ abstract class PubPackageMeta extends PackageMeta {
253
251
}
254
252
}
255
253
256
- class FileContents {
257
- final File file;
258
-
259
- FileContents ._(this .file);
260
-
261
- factory FileContents (File file) => file == null ? null : FileContents ._(file);
262
-
263
- String get contents => file.readAsStringSync (encoding: utf8AllowMalformed);
264
-
265
- bool get isMarkdown => file.path.toLowerCase ().endsWith ('.md' );
266
-
267
- @override
268
- String toString () => file.path;
254
+ extension FileContents on File {
255
+ String get contents => readAsStringSync (encoding: utf8AllowMalformed);
269
256
}
270
257
271
258
class _FilePackageMeta extends PubPackageMeta {
272
- FileContents _readme;
273
- FileContents _license;
274
- FileContents _changelog;
259
+ File _readme;
260
+ File _license;
261
+ File _changelog;
275
262
Map <dynamic , dynamic > _pubspec;
276
263
277
264
_FilePackageMeta (Directory dir) : super (dir) {
278
- var f = File (path .join (dir.path, 'pubspec.yaml' ));
265
+ var f = File (p .join (dir.path, 'pubspec.yaml' ));
279
266
if (f.existsSync ()) {
280
267
_pubspec = loadYaml (f.readAsStringSync ());
281
268
} else {
@@ -299,13 +286,13 @@ class _FilePackageMeta extends PubPackageMeta {
299
286
// possibly by calculating hosting directly from pubspec.yaml or importing
300
287
// a pub library to do this.
301
288
// People could have a pub cache at root with Windows drive mappings.
302
- if (path .split (path .canonicalize (dir.path)).length >= 3 ) {
289
+ if (p .split (p .canonicalize (dir.path)).length >= 3 ) {
303
290
var pubCacheRoot = dir.parent.parent.parent.path;
304
- var hosted = path .canonicalize (dir.parent.parent.path);
305
- var hostname = path .canonicalize (dir.parent.path);
306
- if (path .basename (hosted) == 'hosted' &&
307
- Directory (path .join (pubCacheRoot, '_temp' )).existsSync ()) {
308
- _hostedAt = path .basename (hostname);
291
+ var hosted = p .canonicalize (dir.parent.parent.path);
292
+ var hostname = p .canonicalize (dir.parent.path);
293
+ if (p .basename (hosted) == 'hosted' &&
294
+ Directory (p .join (pubCacheRoot, '_temp' )).existsSync ()) {
295
+ _hostedAt = p .basename (hostname);
309
296
}
310
297
}
311
298
}
@@ -317,18 +304,18 @@ class _FilePackageMeta extends PubPackageMeta {
317
304
318
305
@override
319
306
bool get needsPubGet =>
320
- ! (File (path .join (dir.path, '.dart_tool' , 'package_config.json' ))
307
+ ! (File (p .join (dir.path, '.dart_tool' , 'package_config.json' ))
321
308
.existsSync ());
322
309
323
310
@override
324
311
void runPubGet (String flutterRoot) {
325
312
String binPath;
326
313
List <String > parameters;
327
314
if (requiresFlutter) {
328
- binPath = path .join (flutterRoot, 'bin' , 'flutter' );
315
+ binPath = p .join (flutterRoot, 'bin' , 'flutter' );
329
316
parameters = ['pub' , 'get' ];
330
317
} else {
331
- binPath = path .join (path .dirname (Platform .resolvedExecutable), 'pub' );
318
+ binPath = p .join (p .dirname (Platform .resolvedExecutable), 'pub' );
332
319
parameters = ['get' ];
333
320
}
334
321
if (Platform .isWindows) binPath += '.bat' ;
@@ -367,27 +354,16 @@ class _FilePackageMeta extends PubPackageMeta {
367
354
_pubspec['dependencies' ]? .containsKey ('flutter' ) == true ;
368
355
369
356
@override
370
- FileContents getReadmeContents () {
371
- if (_readme != null ) return _readme;
372
- _readme = FileContents (_locate (dir, ['readme.md' , 'readme.txt' , 'readme' ]));
373
- return _readme;
374
- }
357
+ File getReadmeContents () =>
358
+ _readme ?? = _locate (dir, ['readme.md' , 'readme.txt' , 'readme' ]);
375
359
376
360
@override
377
- FileContents getLicenseContents () {
378
- if (_license != null ) return _license;
379
- _license =
380
- FileContents (_locate (dir, ['license.md' , 'license.txt' , 'license' ]));
381
- return _license;
382
- }
361
+ File getLicenseContents () =>
362
+ _license ?? = _locate (dir, ['license.md' , 'license.txt' , 'license' ]);
383
363
384
364
@override
385
- FileContents getChangelogContents () {
386
- if (_changelog != null ) return _changelog;
387
- _changelog = FileContents (
388
- _locate (dir, ['changelog.md' , 'changelog.txt' , 'changelog' ]));
389
- return _changelog;
390
- }
365
+ File getChangelogContents () => _changelog ?? =
366
+ _locate (dir, ['changelog.md' , 'changelog.txt' , 'changelog' ]);
391
367
392
368
/// Returns a list of reasons this package is invalid, or an
393
369
/// empty list if no reasons found.
@@ -408,7 +384,7 @@ File _locate(Directory dir, List<String> fileNames) {
408
384
409
385
for (var name in fileNames) {
410
386
for (var f in files) {
411
- var baseName = path .basename (f.path).toLowerCase ();
387
+ var baseName = p .basename (f.path).toLowerCase ();
412
388
if (baseName == name) return f;
413
389
if (baseName.startsWith (name)) return f;
414
390
}
@@ -421,7 +397,7 @@ class _SdkMeta extends PubPackageMeta {
421
397
String sdkReadmePath;
422
398
423
399
_SdkMeta (Directory dir) : super (dir) {
424
- sdkReadmePath = path .join (dir.path, 'lib' , 'api_readme.md' );
400
+ sdkReadmePath = p .join (dir.path, 'lib' , 'api_readme.md' );
425
401
}
426
402
427
403
@override
@@ -440,7 +416,7 @@ class _SdkMeta extends PubPackageMeta {
440
416
441
417
@override
442
418
String get version {
443
- var versionFile = File (path .join (dir.path, 'version' ));
419
+ var versionFile = File (p .join (dir.path, 'version' ));
444
420
if (versionFile.existsSync ()) return versionFile.readAsStringSync ().trim ();
445
421
return 'unknown' ;
446
422
}
@@ -457,21 +433,21 @@ class _SdkMeta extends PubPackageMeta {
457
433
bool get requiresFlutter => false ;
458
434
459
435
@override
460
- FileContents getReadmeContents () {
461
- var f = File (path .join (dir.path, 'lib' , 'api_readme.md' ));
436
+ File getReadmeContents () {
437
+ var f = File (p .join (dir.path, 'lib' , 'api_readme.md' ));
462
438
if (! f.existsSync ()) {
463
- f = File (path .join (dir.path, 'api_readme.md' ));
439
+ f = File (p .join (dir.path, 'api_readme.md' ));
464
440
}
465
- return f.existsSync () ? FileContents (f) : null ;
441
+ return f.existsSync () ? f : null ;
466
442
}
467
443
468
444
@override
469
445
List <String > getInvalidReasons () => [];
470
446
471
447
@override
472
- FileContents getLicenseContents () => null ;
448
+ File getLicenseContents () => null ;
473
449
474
450
// TODO: The changelog doesn't seem to be available in the sdk.
475
451
@override
476
- FileContents getChangelogContents () => null ;
452
+ File getChangelogContents () => null ;
477
453
}
0 commit comments