Skip to content

Commit ec0ddb4

Browse files
committed
add ability to include certain libraries, correctly omit errors and exceptions from class list, more tests,
Closes #724 LGTM given by: @sethladd Squashed commit of the following: commit ad6d368 Author: Seth Ladd <[email protected]> Date: Thu Jul 23 16:18:07 2015 -0700 add ability to include certain libraries, correctly omit errors and exceptions from class list, more tests,
1 parent ba79e47 commit ec0ddb4

File tree

6 files changed

+99
-30
lines changed

6 files changed

+99
-30
lines changed

bin/dartdoc.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ main(List<String> arguments) async {
5353

5454
List<String> excludeLibraries =
5555
args['exclude'] == null ? [] : args['exclude'].split(',');
56+
List<String> includeLibraries =
57+
args['include'] == null ? [] : args['include'].split(',');
58+
5659
String url = args['hosted-url'];
5760
String footerFilePath = _resolveTildePath(args['footer']);
5861
if (footerFilePath != null && !new File(footerFilePath).existsSync()) {
@@ -108,7 +111,7 @@ main(List<String> arguments) async {
108111
var generators = initGenerators(url, headerFilePath, footerFilePath);
109112

110113
var dartdoc = new DartDoc(inputDir, excludeLibraries, sdkDir, generators,
111-
outputDir, packageRootDir, packageMeta, urlMappings);
114+
outputDir, packageRootDir, packageMeta, urlMappings, includeLibraries);
112115

113116
try {
114117
DartDocResults results = await dartdoc.generateDocs();
@@ -164,6 +167,8 @@ ArgParser _createArgsParser() {
164167
splitCommas: false);
165168
parser.addOption('exclude',
166169
help: 'Comma-separated list of library names to ignore.');
170+
parser.addOption('include',
171+
help: 'Comma-separated list of library names to generate docs for.');
167172
parser.addOption('hosted-url',
168173
help: 'URL where the docs will be hosted (used to generate the sitemap).');
169174
return parser;

lib/dartdoc.dart

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class DartDoc {
5353
final Directory packageRootDir;
5454
final PackageMeta packageMeta;
5555
final Map<String, String> urlMappings;
56-
57-
final Set<LibraryElement> libraries = new Set();
56+
final List<String> includes;
5857

5958
Stopwatch _stopwatch;
6059

6160
DartDoc(this.rootDir, this.excludes, this.sdkDir, this.generators,
62-
this.outputDir, this.packageRootDir, this.packageMeta, this.urlMappings);
61+
this.outputDir, this.packageRootDir, this.packageMeta, this.urlMappings,
62+
this.includes);
6363

6464
/// Generate DartDoc documentation.
6565
///
@@ -72,16 +72,30 @@ class DartDoc {
7272

7373
if (packageRootDir != null) loader.packageRootPath = packageRootDir.path;
7474

75-
var files =
75+
List<String> files =
7676
packageMeta.isSdk ? [] : findFilesToDocumentInPackage(rootDir.path);
7777

78-
List<LibraryElement> libs = [];
79-
libs.addAll(_parseLibraries(files));
80-
// remove excluded libraries
81-
excludes.forEach(
82-
(pattern) => libs.removeWhere((l) => l.name.startsWith(pattern)));
83-
libs.removeWhere((library) => excludes.contains(library.name));
84-
libraries.addAll(libs);
78+
List<LibraryElement> libraries = _parseLibraries(files);
79+
80+
if (includes != null && includes.isNotEmpty) {
81+
Set notFound = new Set.from(includes)
82+
.difference(new Set.from(libraries.map((l) => l.name)));
83+
if (notFound.isNotEmpty) {
84+
return new Future.error('Some libraries not found: $notFound');
85+
}
86+
libraries.removeWhere((lib) => !includes.contains(lib.name));
87+
} else {
88+
// remove excluded libraries
89+
excludes.forEach((pattern) {
90+
libraries.removeWhere((lib) {
91+
return lib.name.startsWith(pattern) || lib.name == pattern;
92+
});
93+
});
94+
}
95+
96+
if (includes.isNotEmpty || excludes.isNotEmpty) {
97+
print('Generating docs for libraries ${libraries.join(', ')}\n');
98+
}
8599

86100
Package package = new Package(libraries, packageMeta);
87101

@@ -101,6 +115,7 @@ class DartDoc {
101115
}
102116

103117
List<LibraryElement> _parseLibraries(List<String> files) {
118+
Set<LibraryElement> libraries = new Set();
104119
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
105120
List<UriResolver> resolvers = [
106121
new DartUriResolver(sdk),

lib/src/html_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class HtmlGeneratorInstance {
233233
'self': package
234234
};
235235

236-
if (package.hasDocumentation) {
236+
if (package.hasDocumentationFile) {
237237
FileContents readme = package.documentationFile;
238238
data['markdown'] = readme.isMarkdown ? renderMarkdown : renderPlainText;
239239
}
@@ -242,7 +242,7 @@ class HtmlGeneratorInstance {
242242
}
243243

244244
void generateLibrary(Package package, Library lib) {
245-
print('generating docs for library ${lib.path}...');
245+
print('generating docs for library ${lib.name} from ${lib.path}...');
246246

247247
if (!lib.hasDocumentation) {
248248
print(" warning: library '${lib.name}' has no documentation");

lib/src/model.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,14 @@ class Package {
368368

369369
String get version => packageMeta.version;
370370

371-
bool get hasDocumentation => documentationFile != null;
371+
bool get hasDocumentationFile => documentationFile != null;
372372

373373
FileContents get documentationFile => packageMeta.getReadmeContents();
374374

375375
// TODO: Clients should use [documentationFile] so they can act differently on
376376
// plain text or markdown.
377377
String get documentation =>
378-
hasDocumentation ? documentationFile.contents : null;
378+
hasDocumentationFile ? documentationFile.contents : null;
379379

380380
String get documentationAsHtml => renderMarkdownToHtml(documentation);
381381

@@ -508,7 +508,11 @@ class Library extends ModelElement {
508508
_name = element.name;
509509
}
510510

511-
// calculate this once, instead on every invocation of name getter
511+
// So, if the library is a system library, it's name is not
512+
// dart:___, it's dart.___. Apparently the way to get to the dart:___
513+
// name is to get source.encoding.
514+
// This may be wrong or misleading, but developers expect the name
515+
// of dart:____
512516
var source = _library.definingCompilationUnit.source;
513517
_name = source.isInSystemLibrary ? source.encoding : _name;
514518

@@ -643,12 +647,7 @@ class Library extends ModelElement {
643647
return _classes;
644648
}
645649

646-
/// if SDK, return all classes
647-
/// if package, return classes that are not [Error] or [Exception]
648650
List<Class> get classes {
649-
if (package.isSdk) {
650-
return _allClasses;
651-
}
652651
return _allClasses.where((c) => !c.isErrorOrException).toList(
653652
growable: false);
654653
}
@@ -1065,6 +1064,9 @@ class Class extends ModelElement {
10651064
(type.name == 'Exception' || type.name == 'Error'));
10661065
}
10671066

1067+
// if this class is itself Error or Exception, return true
1068+
if (_doCheck(_cls.type)) return true;
1069+
10681070
return _cls.allSupertypes.any(_doCheck);
10691071
}
10701072

test/dartdoc_test.dart

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,77 @@ void main() {
2727
delete(tempDir);
2828
});
2929

30-
test('generateDocs ${path.basename(testPackageDir.path)}', () async {
30+
test('generate docs for ${path.basename(testPackageDir.path)} works',
31+
() async {
3132
PackageMeta meta = new PackageMeta.fromDir(testPackageDir);
3233
DartDoc dartdoc = new DartDoc(
33-
testPackageDir, [], getSdkDir(), [], tempDir, null, meta, null);
34+
testPackageDir, [], getSdkDir(), [], tempDir, null, meta, null, []);
3435

3536
DartDocResults results = await dartdoc.generateDocs();
3637
expect(results.package, isNotNull);
3738

3839
Package p = results.package;
3940
expect(p.name, 'test_package');
40-
expect(p.hasDocumentation, true);
41-
expect(p.libraries.length, 7);
41+
expect(p.hasDocumentationFile, isTrue);
42+
expect(p.libraries, hasLength(7));
4243
});
4344

44-
test('generateDocs ${path.basename(testPackageBadDir.path)}', () async {
45+
test('generate docs for ${path.basename(testPackageBadDir.path)} fails',
46+
() async {
4547
PackageMeta meta = new PackageMeta.fromDir(testPackageBadDir);
46-
DartDoc dartdoc = new DartDoc(
47-
testPackageBadDir, [], getSdkDir(), [], tempDir, null, meta, null);
48+
DartDoc dartdoc = new DartDoc(testPackageBadDir, [], getSdkDir(), [],
49+
tempDir, null, meta, null, []);
4850

4951
try {
5052
await dartdoc.generateDocs();
5153
fail('dartdoc should fail on analysis errors');
5254
} catch (e) {
53-
expect(e is DartDocFailure, true);
55+
expect(e is DartDocFailure, isTrue);
5456
}
5557
});
58+
59+
test('generate docs for a package that does not have a readme', () async {
60+
PackageMeta meta = new PackageMeta.fromDir(testPackageWithNoReadme);
61+
DartDoc dartdoc = new DartDoc(testPackageWithNoReadme, [], getSdkDir(),
62+
[], tempDir, null, meta, null, []);
63+
64+
DartDocResults results = await dartdoc.generateDocs();
65+
expect(results.package, isNotNull);
66+
67+
Package p = results.package;
68+
expect(p.name, 'test_package_small');
69+
expect(p.hasDocumentationFile, isFalse);
70+
expect(p.libraries, hasLength(1));
71+
});
72+
73+
test('generate docs including a single library', () async {
74+
PackageMeta meta = new PackageMeta.fromDir(testPackageDir);
75+
DartDoc dartdoc = new DartDoc(testPackageDir, [], getSdkDir(), [],
76+
tempDir, null, meta, null, ['fake']);
77+
78+
DartDocResults results = await dartdoc.generateDocs();
79+
expect(results.package, isNotNull);
80+
81+
Package p = results.package;
82+
expect(p.name, 'test_package');
83+
expect(p.hasDocumentationFile, isTrue);
84+
expect(p.libraries, hasLength(1));
85+
expect(p.libraries.map((lib) => lib.name), contains('fake'));
86+
});
87+
88+
test('generate docs excluding a single library', () async {
89+
PackageMeta meta = new PackageMeta.fromDir(testPackageDir);
90+
DartDoc dartdoc = new DartDoc(testPackageDir, ['fake'], getSdkDir(), [],
91+
tempDir, null, meta, null, []);
92+
93+
DartDocResults results = await dartdoc.generateDocs();
94+
expect(results.package, isNotNull);
95+
96+
Package p = results.package;
97+
expect(p.name, 'test_package');
98+
expect(p.hasDocumentationFile, isTrue);
99+
expect(p.libraries, hasLength(6));
100+
expect(p.libraries.map((lib) => lib.name).contains('fake'), isFalse);
101+
});
56102
});
57103
}

test/src/utils.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:io';
88

99
final Directory testPackageDir = new Directory('test_package');
1010
final Directory testPackageBadDir = new Directory('test_package_bad');
11+
final Directory testPackageWithNoReadme = new Directory('test_package_small');
1112

1213
void delete(Directory dir) {
1314
if (dir.existsSync()) dir.deleteSync(recursive: true);

0 commit comments

Comments
 (0)