Skip to content

Commit 356139e

Browse files
authored
Rearrange SDK PackageMeta instantiation to be similar to normal packages (#1639)
* Fix or disable miscellaneous lints in dartdoc * Clean up a few more lints. * Rename category to package in all remaining references * dartfmt * Review comments * Intermediate state * basic dartdoc options now working * Fix test failure (accidentally caching config info) * dartfmt * dartfmt * one last rename * Remove overly complicated config inheritance; we can add it back if we truly need this * dartfmt * Unify SDK PackageMeta construction and add cache * update test package docs * PackageMeta construction sufficiently robust for refactor * Fix infinite loop and error messages * Rename path/p import directive to pathLib everywhere. * dartfmt * 'Dart SDK' + '' => 'Dart Core' + 'SDK' * SDK name is no longer special
1 parent 44dfb15 commit 356139e

20 files changed

+87
-88
lines changed

bin/dartdoc.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ main(List<String> arguments) async {
5252
final bool sdkDocs = args['sdk-docs'];
5353
final bool showProgress = args['show-progress'];
5454

55-
final String readme = args['sdk-readme'];
56-
if (readme != null && !(new File(readme).existsSync())) {
57-
stderr.writeln(
58-
" fatal error: unable to locate the SDK description file at $readme.");
59-
exit(1);
55+
Directory inputDir;
56+
if (sdkDocs) {
57+
inputDir = sdkDir;
58+
} else if (args['input'] == null) {
59+
inputDir = Directory.current;
60+
} else {
61+
inputDir = args['input'];
6062
}
6163

62-
Directory inputDir = new Directory(args['input']);
6364
if (!inputDir.existsSync()) {
6465
stderr.writeln(
6566
" fatal error: unable to locate the input directory at ${inputDir
@@ -188,12 +189,12 @@ main(List<String> arguments) async {
188189
});
189190
}
190191

191-
PackageMeta packageMeta = sdkDocs
192-
? new PackageMeta.fromSdk(sdkDir,
193-
sdkReadmePath: readme,
194-
displayAsPackages:
195-
args['use-categories'] || args['display-as-packages'])
196-
: new PackageMeta.fromDir(inputDir);
192+
PackageMeta packageMeta = new PackageMeta.fromDir(inputDir);
193+
194+
if (packageMeta == null) {
195+
stderr.writeln(' fatal error: Unable to generate documentation: no pubspec.yaml found');
196+
exit(1);
197+
}
197198

198199
if (!packageMeta.isValid) {
199200
final String firstError = packageMeta.getInvalidReasons().first;
@@ -211,6 +212,7 @@ main(List<String> arguments) async {
211212
}
212213
}
213214

215+
214216
logInfo("Generating documentation for '${packageMeta}' into "
215217
"${outputDir.absolute.path}${Platform.pathSeparator}");
216218

@@ -310,9 +312,9 @@ ArgParser _createArgsParser() {
310312
help: 'Display progress indications to console stdout', negatable: false);
311313
parser.addOption('sdk-readme',
312314
help:
313-
'Path to the SDK description file; use if generating Dart SDK docs.');
315+
'Path to the SDK description file. Deprecated (ignored)');
314316
parser.addOption('input',
315-
help: 'Path to source directory.', defaultsTo: Directory.current.path);
317+
help: 'Path to source directory.');
316318
parser.addOption('output',
317319
help: 'Path to output directory.', defaultsTo: defaultOutDir);
318320
parser.addMultiOption('header',

lib/src/model.dart

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,6 @@ class Library extends ModelElement {
17101710
List<TopLevelVariable> _variables;
17111711
Namespace _exportedNamespace;
17121712
String _name;
1713-
String _packageName;
17141713
factory Library(LibraryElement element, PackageGraph packageGraph) {
17151714
return packageGraph.findOrCreateLibraryFor(element);
17161715
}
@@ -2015,16 +2014,7 @@ class Library extends ModelElement {
20152014

20162015
/// The real package, as opposed to the package we are documenting it with,
20172016
/// [PackageGraph.name]
2018-
String get packageName {
2019-
if (_packageName == null) {
2020-
if (name.startsWith('dart:')) {
2021-
_packageName = 'Dart Core';
2022-
} else {
2023-
_packageName = packageMeta?.name ?? '';
2024-
}
2025-
}
2026-
return _packageName;
2027-
}
2017+
String get packageName => packageMeta?.name ?? '';
20282018

20292019
/// The real packageMeta, as opposed to the package we are documenting with.
20302020
PackageMeta _packageMeta;
@@ -2187,16 +2177,7 @@ class Library extends ModelElement {
21872177

21882178
static PackageMeta getPackageMeta(LibraryElement element) {
21892179
String sourcePath = element.source.fullName;
2190-
File file = new File(pathLib.canonicalize(sourcePath));
2191-
Directory dir = file.parent;
2192-
while (dir.parent.path != dir.path && dir.existsSync()) {
2193-
File pubspec = new File(pathLib.join(dir.path, 'pubspec.yaml'));
2194-
if (pubspec.existsSync()) {
2195-
return new PackageMeta.fromDir(dir);
2196-
}
2197-
dir = dir.parent;
2198-
}
2199-
return null;
2180+
return new PackageMeta.fromDir(new File(pathLib.canonicalize(sourcePath)).parent);
22002181
}
22012182

22022183
static String getLibraryName(LibraryElement element) {
@@ -4204,7 +4185,7 @@ class PackageGraph extends Canonicalization with Nameable, Warnable {
42044185
String get name => packageMeta.name;
42054186

42064187
String get kind =>
4207-
(packageMeta.displayAsPackages || packageGraph.isSdk) ? '' : 'package';
4188+
(packageGraph.isSdk) ? 'SDK' : 'package';
42084189

42094190
@override
42104191
String get oneLineDoc => '';
@@ -4543,14 +4524,14 @@ class Package implements Comparable<Package> {
45434524
/// Returns:
45444525
/// -1 if this package is listed in --package-order.
45454526
/// 0 if this package is the original package we are documenting.
4546-
/// 1 if this group represents the Dart SDK.
4547-
/// 2 if this group has a name that contains the name of the original
4527+
/// 1 if this package represents the Dart SDK.
4528+
/// 2 if this package has a name that contains the name of the original
45484529
/// package we are documenting.
45494530
/// 3 otherwise.
45504531
int get _group {
45514532
if (config.packageOrder.contains(name)) return -1;
45524533
if (name.toLowerCase() == packageGraph.name.toLowerCase()) return 0;
4553-
if (name == "Dart Core") return 1;
4534+
if (isSdk) return 1;
45544535
if (name.toLowerCase().contains(packageGraph.name.toLowerCase())) return 2;
45554536
return 3;
45564537
}

lib/src/package_meta.dart

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,49 @@ library dartdoc.package_meta;
66

77
import 'dart:io';
88

9+
import 'package:dartdoc/src/sdk.dart';
910
import 'package:path/path.dart' as pathLib;
1011
import 'package:yaml/yaml.dart';
1112

1213
import 'logging.dart';
1314

15+
16+
Map<String, PackageMeta> _packageMetaCache = {};
17+
1418
abstract class PackageMeta {
1519
final Directory dir;
16-
final bool displayAsPackages;
17-
18-
PackageMeta(this.dir, {this.displayAsPackages: false});
1920

20-
factory PackageMeta.fromDir(Directory dir) => new _FilePackageMeta(dir);
21-
factory PackageMeta.fromSdk(Directory sdkDir,
22-
{String sdkReadmePath, bool displayAsPackages}) =>
23-
new _SdkMeta(sdkDir,
24-
sdkReadmePath: sdkReadmePath, displayAsPackages: displayAsPackages);
21+
PackageMeta(this.dir);
22+
23+
/// This factory is guaranteed to return the same object for any given
24+
/// [dir.absolute.path]. Multiple [dir.absolute.path]s will resolve to the
25+
/// same object if they are part of the same package. Returns null
26+
/// if the directory is not part of a known package.
27+
factory PackageMeta.fromDir(Directory dir) {
28+
Directory original = dir.absolute;
29+
dir = original;
30+
if (!_packageMetaCache.containsKey(dir.path)) {
31+
PackageMeta packageMeta;
32+
// There are pubspec.yaml files inside the SDK. Ignore them.
33+
if (pathLib.isWithin(getSdkDir().absolute.path, dir.path) || getSdkDir().path == dir.path) {
34+
packageMeta = new _SdkMeta(getSdkDir());
35+
} else {
36+
while (dir.existsSync()) {
37+
File pubspec = new File(pathLib.join(dir.path, 'pubspec.yaml'));
38+
if (pubspec.existsSync()) {
39+
packageMeta = new _FilePackageMeta(dir);
40+
break;
41+
}
42+
// Allow a package to be at root (possible in a Windows setting with
43+
// drive letter mappings).
44+
if (dir.path == dir.parent.absolute.path) break;
45+
dir = dir.parent.absolute;
46+
}
47+
}
48+
_packageMetaCache[dir.absolute.path] = packageMeta;
49+
}
50+
return _packageMetaCache[dir.absolute.path];
51+
}
2552

2653
bool get isSdk;
2754
bool get needsPubGet => false;
@@ -46,7 +73,7 @@ abstract class PackageMeta {
4673
FileContents getChangelogContents();
4774

4875
/// Returns true if we are a valid package, valid enough to generate docs.
49-
bool get isValid;
76+
bool get isValid => getInvalidReasons().isEmpty;
5077

5178
/// Returns a list of reasons this package is invalid, or an
5279
/// empty list if no reasons found.
@@ -151,8 +178,6 @@ class _FilePackageMeta extends PackageMeta {
151178
return _changelog;
152179
}
153180

154-
@override
155-
bool get isValid => getInvalidReasons().isEmpty;
156181

157182
/// Returns a list of reasons this package is invalid, or an
158183
/// empty list if no reasons found.
@@ -184,10 +209,12 @@ File _locate(Directory dir, List<String> fileNames) {
184209
}
185210

186211
class _SdkMeta extends PackageMeta {
187-
final String sdkReadmePath;
212+
String sdkReadmePath;
188213

189-
_SdkMeta(Directory dir, {this.sdkReadmePath, bool displayAsPackages})
190-
: super(dir, displayAsPackages: displayAsPackages);
214+
_SdkMeta(Directory dir)
215+
: super(dir) {
216+
sdkReadmePath = pathLib.join(dir.path, 'lib', 'api_readme.md');
217+
}
191218

192219
@override
193220
bool get isSdk => true;
@@ -198,7 +225,7 @@ class _SdkMeta extends PackageMeta {
198225
}
199226

200227
@override
201-
String get name => 'Dart SDK';
228+
String get name => 'Dart';
202229
@override
203230
String get version =>
204231
new File(pathLib.join(dir.path, 'version')).readAsStringSync().trim();
@@ -211,15 +238,10 @@ class _SdkMeta extends PackageMeta {
211238

212239
@override
213240
FileContents getReadmeContents() {
214-
File f = sdkReadmePath != null
215-
? new File(sdkReadmePath)
216-
: new File(pathLib.join(dir.path, 'lib', 'api_readme.md'));
241+
File f = new File(pathLib.join(dir.path, 'lib', 'api_readme.md'));
217242
return f.existsSync() ? new FileContents(f) : null;
218243
}
219244

220-
@override
221-
bool get isValid => true;
222-
223245
@override
224246
List<String> getInvalidReasons() => [];
225247

lib/templates/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{>head}}
22

33
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
4-
<h5>{{self.name}} {{self.kind}}</h5>
4+
<h5><span class="package-name">{{self.name}}</span> <span class="package-kind">{{self.kind}}</span></h5>
55

66
{{#displayAsPackages}}
77
<ol>
@@ -11,7 +11,6 @@ <h5>{{self.name}} {{self.kind}}</h5>
1111
<li>{{{linkedName}}}</li>
1212
{{/publicLibraries}}
1313
{{/packageGraph.publicPackages}}
14-
1514
</ol>
1615
{{/displayAsPackages}}
1716

lib/templates/library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{>head}}
22

33
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
4-
<h5>{{parent.name}} {{parent.kind}}</h5>
4+
<h5><span class="package-name">{{parent.name}}</span> <span class="package-kind">{{parent.kind}}</span></h5>
55
{{#displayAsPackages}}
66
<ol>
77
{{#packageGraph.publicPackages}}

test/model_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ void main() {
104104
});
105105

106106
test('sdk name', () {
107-
expect(sdkAsPackageGraph.name, equals('Dart SDK'));
107+
expect(sdkAsPackageGraph.name, equals('Dart'));
108+
expect(sdkAsPackageGraph.kind, equals('SDK'));
108109
});
109110

110111
test('sdk homepage', () {

test/package_meta_test.dart

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,19 @@ import 'dart:io';
88

99
import 'package:dartdoc/src/package_meta.dart';
1010
import 'package:dartdoc/src/sdk.dart';
11-
import 'package:path/path.dart' as pathLib;
1211
import 'package:test/test.dart';
1312

1413
void main() {
1514
group('PackageMeta for a directory without a pubspec', () {
1615
PackageMeta p;
1716

1817
setUp(() {
19-
var d = new Directory(pathLib.join(
20-
Directory.current.path, 'testing/test_package_not_valid'));
21-
if (!d.existsSync()) {
22-
throw "$d cannot be found";
23-
}
18+
var d = Directory.systemTemp.createTempSync('test_package_not_valid');
2419
p = new PackageMeta.fromDir(d);
2520
});
2621

2722
test('is not valid', () {
28-
expect(p.isValid, isFalse);
29-
expect(p.getInvalidReasons(), isNotEmpty);
30-
expect(p.getInvalidReasons(), contains('no pubspec.yaml found'));
23+
expect(p, isNull);
3124
});
3225
});
3326

@@ -76,15 +69,16 @@ void main() {
7669
});
7770

7871
group('PackageMeta.fromSdk', () {
79-
PackageMeta p = new PackageMeta.fromSdk(getSdkDir());
72+
PackageMeta p = new PackageMeta.fromDir(getSdkDir());
8073

8174
test('has a name', () {
82-
expect(p.name, 'Dart SDK');
75+
expect(p.name, 'Dart');
8376
});
8477

8578
test('is valid', () {
8679
expect(p.isValid, isTrue);
8780
expect(p.getInvalidReasons(), isEmpty);
81+
expect(p.isSdk, isTrue);
8882
});
8983

9084
test('has a version', () {

test/src/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void delete(Directory dir) {
3434

3535
init() async {
3636
sdkDir = getSdkDir();
37-
sdkPackageMeta = new PackageMeta.fromSdk(sdkDir);
37+
sdkPackageMeta = new PackageMeta.fromDir(sdkDir);
3838
setConfig();
3939

4040
testPackageGraph = await bootBasicPackage(

testing/test_package_docs/anonymous_library/anonymous_library-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

testing/test_package_docs/another_anonymous_lib/another_anonymous_lib-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

testing/test_package_docs/code_in_comments/code_in_comments-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

testing/test_package_docs/css/css-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

testing/test_package_docs/ex/ex-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

testing/test_package_docs/fake/fake-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<main>
3636

3737
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
38-
<h5>test_package package</h5>
38+
<h5><span class="package-name">test_package</span> <span class="package-kind">package</span></h5>
3939

4040
<ol>
4141
<li class="section-title"><a href="index.html#libraries">Libraries</a></li>

0 commit comments

Comments
 (0)