Skip to content

Commit b7da88b

Browse files
authoredAug 19, 2020
Remove FileContents class. (#2312)
The public constructor was a factory constructor which can return null, which is illegal under the null safety language feature. Any refactoring is breaking, and the class's functionality is better implemented as an extension.
1 parent 291ebc5 commit b7da88b

File tree

4 files changed

+59
-83
lines changed

4 files changed

+59
-83
lines changed
 

‎lib/src/model/category.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:io';
77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:dartdoc/src/dartdoc_options.dart';
99
import 'package:dartdoc/src/model/model.dart';
10-
import 'package:dartdoc/src/package_meta.dart';
1110
import 'package:dartdoc/src/render/category_renderer.dart';
1211
import 'package:dartdoc/src/warnings.dart';
1312

@@ -174,14 +173,13 @@ class Category extends Nameable
174173
@override
175174
String get kind => 'Topic';
176175

177-
FileContents _documentationFile;
176+
File _documentationFile;
178177

179178
@override
180-
FileContents get documentationFile {
179+
File get documentationFile {
181180
if (_documentationFile == null) {
182181
if (categoryDefinition?.documentationMarkdown != null) {
183-
_documentationFile =
184-
FileContents(File(categoryDefinition.documentationMarkdown));
182+
_documentationFile = File(categoryDefinition.documentationMarkdown);
185183
}
186184
}
187185
return _documentationFile;

‎lib/src/model/documentable.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io' show File;
6+
57
import 'package:dartdoc/src/dartdoc_options.dart';
68
import 'package:dartdoc/src/package_meta.dart';
7-
import 'package:path/path.dart' as path;
9+
import 'package:path/path.dart' as p;
810

911
import 'model.dart';
1012

@@ -69,10 +71,10 @@ mixin MarkdownFileDocumentation implements Documentable, Canonicalization {
6971
@override
7072
String get oneLineDoc => __documentation.asOneLiner;
7173

72-
FileContents get documentationFile;
74+
File get documentationFile;
7375

7476
@override
75-
String get location => path.toUri(documentationFile.file.path).toString();
77+
String get location => p.toUri(documentationFile.path).toString();
7678

7779
@override
7880
Set<String> get locationPieces => <String>{location};

‎lib/src/model/package.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Package extends LibraryContainer
125125
// plain text or markdown.
126126
bool get hasDocumentationFile => documentationFile != null;
127127

128-
FileContents get documentationFile => packageMeta.getReadmeContents();
128+
File get documentationFile => packageMeta.getReadmeContents();
129129

130130
@override
131131
String get oneLineDoc => '';

‎lib/src/package_meta.dart

+50-74
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:io';
99

1010
import 'package:analyzer/dart/element/element.dart';
1111
import 'package:dartdoc/dartdoc.dart';
12-
import 'package:path/path.dart' as path;
12+
import 'package:path/path.dart' as p;
1313
import 'package:yaml/yaml.dart';
1414

1515
import 'logging.dart';
@@ -19,7 +19,7 @@ Encoding utf8AllowMalformed = Utf8Codec(allowMalformed: true);
1919

2020
Directory get defaultSdkDir {
2121
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));
2323
return sdkDir;
2424
}
2525

@@ -74,11 +74,11 @@ abstract class PackageMeta {
7474
bool operator ==(Object other) {
7575
if (other is! PackageMeta) return false;
7676
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);
7878
}
7979

8080
@override
81-
int get hashCode => path.hash(dir.absolute.path);
81+
int get hashCode => p.hash(dir.absolute.path);
8282

8383
/// Returns true if this represents a 'Dart' SDK. A package can be part of
8484
/// Dart and Flutter at the same time, but if we are part of a Dart SDK
@@ -107,11 +107,11 @@ abstract class PackageMeta {
107107

108108
String get homepage;
109109

110-
FileContents getReadmeContents();
110+
File getReadmeContents();
111111

112-
FileContents getLicenseContents();
112+
File getLicenseContents();
113113

114-
FileContents getChangelogContents();
114+
File getChangelogContents();
115115

116116
/// Returns true if we are a valid package, valid enough to generate docs.
117117
bool get isValid => getInvalidReasons().isEmpty;
@@ -141,9 +141,9 @@ abstract class PubPackageMeta extends PackageMeta {
141141
if (Platform.isWindows) {
142142
for (var paths in __sdkDirFilePathsPosix) {
143143
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)));
147147
}
148148
__sdkDirFilePaths.add(windowsPaths);
149149
}
@@ -159,17 +159,17 @@ abstract class PubPackageMeta extends PackageMeta {
159159
static final Map<String, Directory> _sdkDirParent = {};
160160

161161
static Directory sdkDirParent(Directory dir) {
162-
var dirPathCanonical = path.canonicalize(dir.path);
162+
var dirPathCanonical = p.canonicalize(dir.path);
163163
if (!_sdkDirParent.containsKey(dirPathCanonical)) {
164164
_sdkDirParent[dirPathCanonical] = null;
165165
while (dir.existsSync()) {
166166
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());
168168
})) {
169169
_sdkDirParent[dirPathCanonical] = dir;
170170
break;
171171
}
172-
if (path.equals(dir.path, dir.parent.path)) break;
172+
if (p.equals(dir.path, dir.parent.path)) break;
173173
dir = dir.parent;
174174
}
175175
}
@@ -183,7 +183,7 @@ abstract class PubPackageMeta extends PackageMeta {
183183
return PubPackageMeta.fromDir(Directory(sdkDir));
184184
}
185185
return PubPackageMeta.fromDir(
186-
File(path.canonicalize(libraryElement.source.fullName)).parent);
186+
File(p.canonicalize(libraryElement.source.fullName)).parent);
187187
}
188188

189189
static PubPackageMeta fromFilename(String filename) {
@@ -210,14 +210,14 @@ abstract class PubPackageMeta extends PackageMeta {
210210
packageMeta = _SdkMeta(parentSdkDir);
211211
} else {
212212
while (dir.existsSync()) {
213-
var pubspec = File(path.join(dir.path, 'pubspec.yaml'));
213+
var pubspec = File(p.join(dir.path, 'pubspec.yaml'));
214214
if (pubspec.existsSync()) {
215215
packageMeta = _FilePackageMeta(dir);
216216
break;
217217
}
218218
// Allow a package to be at root (possible in a Windows setting with
219219
// drive letter mappings).
220-
if (path.equals(dir.path, dir.parent.path)) break;
220+
if (p.equals(dir.path, dir.parent.path)) break;
221221
dir = dir.parent.absolute;
222222
}
223223
}
@@ -229,15 +229,13 @@ abstract class PubPackageMeta extends PackageMeta {
229229
@override
230230
String sdkType(String flutterRootPath) {
231231
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');
234234

235235
/// Don't include examples or other non-SDK components as being the
236236
/// "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))) {
241239
return 'Flutter';
242240
}
243241
}
@@ -253,29 +251,18 @@ abstract class PubPackageMeta extends PackageMeta {
253251
}
254252
}
255253

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);
269256
}
270257

271258
class _FilePackageMeta extends PubPackageMeta {
272-
FileContents _readme;
273-
FileContents _license;
274-
FileContents _changelog;
259+
File _readme;
260+
File _license;
261+
File _changelog;
275262
Map<dynamic, dynamic> _pubspec;
276263

277264
_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'));
279266
if (f.existsSync()) {
280267
_pubspec = loadYaml(f.readAsStringSync());
281268
} else {
@@ -299,13 +286,13 @@ class _FilePackageMeta extends PubPackageMeta {
299286
// possibly by calculating hosting directly from pubspec.yaml or importing
300287
// a pub library to do this.
301288
// 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) {
303290
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);
309296
}
310297
}
311298
}
@@ -317,18 +304,18 @@ class _FilePackageMeta extends PubPackageMeta {
317304

318305
@override
319306
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'))
321308
.existsSync());
322309

323310
@override
324311
void runPubGet(String flutterRoot) {
325312
String binPath;
326313
List<String> parameters;
327314
if (requiresFlutter) {
328-
binPath = path.join(flutterRoot, 'bin', 'flutter');
315+
binPath = p.join(flutterRoot, 'bin', 'flutter');
329316
parameters = ['pub', 'get'];
330317
} else {
331-
binPath = path.join(path.dirname(Platform.resolvedExecutable), 'pub');
318+
binPath = p.join(p.dirname(Platform.resolvedExecutable), 'pub');
332319
parameters = ['get'];
333320
}
334321
if (Platform.isWindows) binPath += '.bat';
@@ -367,27 +354,16 @@ class _FilePackageMeta extends PubPackageMeta {
367354
_pubspec['dependencies']?.containsKey('flutter') == true;
368355

369356
@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']);
375359

376360
@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']);
383363

384364
@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']);
391367

392368
/// Returns a list of reasons this package is invalid, or an
393369
/// empty list if no reasons found.
@@ -408,7 +384,7 @@ File _locate(Directory dir, List<String> fileNames) {
408384

409385
for (var name in fileNames) {
410386
for (var f in files) {
411-
var baseName = path.basename(f.path).toLowerCase();
387+
var baseName = p.basename(f.path).toLowerCase();
412388
if (baseName == name) return f;
413389
if (baseName.startsWith(name)) return f;
414390
}
@@ -421,7 +397,7 @@ class _SdkMeta extends PubPackageMeta {
421397
String sdkReadmePath;
422398

423399
_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');
425401
}
426402

427403
@override
@@ -440,7 +416,7 @@ class _SdkMeta extends PubPackageMeta {
440416

441417
@override
442418
String get version {
443-
var versionFile = File(path.join(dir.path, 'version'));
419+
var versionFile = File(p.join(dir.path, 'version'));
444420
if (versionFile.existsSync()) return versionFile.readAsStringSync().trim();
445421
return 'unknown';
446422
}
@@ -457,21 +433,21 @@ class _SdkMeta extends PubPackageMeta {
457433
bool get requiresFlutter => false;
458434

459435
@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'));
462438
if (!f.existsSync()) {
463-
f = File(path.join(dir.path, 'api_readme.md'));
439+
f = File(p.join(dir.path, 'api_readme.md'));
464440
}
465-
return f.existsSync() ? FileContents(f) : null;
441+
return f.existsSync() ? f : null;
466442
}
467443

468444
@override
469445
List<String> getInvalidReasons() => [];
470446

471447
@override
472-
FileContents getLicenseContents() => null;
448+
File getLicenseContents() => null;
473449

474450
// TODO: The changelog doesn't seem to be available in the sdk.
475451
@override
476-
FileContents getChangelogContents() => null;
452+
File getChangelogContents() => null;
477453
}

0 commit comments

Comments
 (0)
Please sign in to comment.