Skip to content

Commit 62b5a7b

Browse files
committed
support external --include s
1 parent c11ef44 commit 62b5a7b

File tree

12 files changed

+265
-125
lines changed

12 files changed

+265
-125
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
# Don’t commit the following directories created by pub.
21
build/
32
doc/api/
43
test_package/doc
54
test_package_small/doc
65
packages
76
.buildlog
87
.pub
8+
99
.idea
10+
.atom/
11+
1012
.packages
1113
.settings/
1214
.DS_Store
1315
*.iml
1416

1517
pub.dartlang.org/
1618

17-
# Or the files created by dart2js.
1819
*.dart.js
1920
*.js.deps
2021
*.js.map

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
## 0.9.3
2-
* [enhancement] Added support for URL-based search. If a query parameter named
2+
* [enhancement] added support for URL-based search. If a query parameter named
33
"search" is passed, the page navigates to the first search result for its
44
value.
5+
* [enhancement] added support for passing more than one `--header` or `--footer`
6+
* [enhancement] added the ability to `--include` libraries referenced by (but
7+
not directly inside) the project
8+
* [bug] rev the analyzer version used to fix an issue generating docs for
9+
Flutter
510

611
## 0.9.2
712
* [bug] do not generate docs for `dart:_internal` and `dart:nativewrappers`, when defined
8-
in the `_embedder.yaml` file.
13+
in the `_embedder.yaml` file.
914
* [enhancement] print message to run pub if dartdoc does not find any libraries to
1015
document.
1116

bin/dartdoc.dart

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import 'dart:io';
99
import 'package:args/args.dart';
1010
import 'package:cli_util/cli_util.dart' as cli_util;
1111
import 'package:dartdoc/dartdoc.dart';
12-
import 'package:dartdoc/src/package_meta.dart';
1312
import 'package:dartdoc/src/config.dart';
13+
import 'package:dartdoc/src/package_meta.dart';
1414
import 'package:path/path.dart' as path;
1515
import 'package:stack_trace/stack_trace.dart';
1616

@@ -68,22 +68,18 @@ main(List<String> arguments) async {
6868
exit(1);
6969
}
7070

71-
List<String> excludeLibraries =
72-
args['exclude'] == null ? [] : args['exclude'].split(',');
73-
List<String> includeLibraries =
74-
args['include'] == null ? [] : args['include'].split(',');
71+
List<String> excludeLibraries = args['exclude'];
72+
List<String> includeLibraries = args['include'];
7573

7674
String url = args['hosted-url'];
7775
List<String> footerFilePaths = args['footer'].map(_resolveTildePath).toList();
78-
print(footerFilePaths);
7976
for (String footerFilePath in footerFilePaths) {
8077
if (!new File(footerFilePath).existsSync()) {
8178
print("Error: unable to locate footer file: ${footerFilePath}.");
8279
exit(1);
8380
}
8481
}
8582
List<String> headerFilePaths = args['header'].map(_resolveTildePath).toList();
86-
print(headerFilePaths);
8783
for (String headerFilePath in footerFilePaths) {
8884
if (!new File(headerFilePath).existsSync()) {
8985
print("Error: unable to locate header file: ${headerFilePath}.");
@@ -187,26 +183,23 @@ ArgParser _createArgsParser() {
187183
parser.addOption('output',
188184
help: 'Path to output directory.', defaultsTo: defaultOutDir);
189185
parser.addOption('header',
190-
allowMultiple: true,
191-
help: 'path to file containing HTML text.');
186+
allowMultiple: true, help: 'path to file containing HTML text.');
192187
parser.addOption('footer',
193-
allowMultiple: true,
194-
help: 'path to file containing HTML text.');
188+
allowMultiple: true, help: 'path to file containing HTML text.');
195189
parser.addOption('exclude',
196-
help: 'Comma-separated list of library names to ignore.');
190+
allowMultiple: true, help: 'library names to ignore');
197191
parser.addOption('include',
198-
help: 'Comma-separated list of library names to generate docs for.');
192+
allowMultiple: true, help: 'library names to generate docs for');
199193
parser.addOption('hosted-url',
200194
help:
201195
'URL where the docs will be hosted (used to generate the sitemap).');
202196
parser.addOption('rel-canonical-prefix',
203-
help: 'If provided, add a rel="canonical" prefixed with provided value. \n'
197+
help:
198+
'If provided, add a rel="canonical" prefixed with provided value. \n'
204199
'Consider using if building many versions of the docs for public SEO. '
205200
'Learn more at https://goo.gl/gktN6F.');
206201
parser.addFlag('include-source',
207-
help: 'If source code blocks should be shown, if they exist.',
208-
negatable: true,
209-
defaultsTo: true);
202+
help: 'Show source code blocks', negatable: true, defaultsTo: true);
210203
return parser;
211204
}
212205

lib/dartdoc.dart

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:analyzer/source/package_map_provider.dart';
1515
import 'package:analyzer/source/package_map_resolver.dart';
1616
import 'package:analyzer/source/pub_package_map_provider.dart';
1717
import 'package:analyzer/source/sdk_ext.dart';
18-
import 'package:analyzer/src/generated/element.dart';
18+
import 'package:analyzer/dart/element/element.dart';
1919
import 'package:analyzer/src/generated/engine.dart';
2020
import 'package:analyzer/src/generated/error.dart';
2121
import 'package:analyzer/src/generated/java_io.dart';
@@ -85,28 +85,17 @@ class DartDoc {
8585
? const []
8686
: findFilesToDocumentInPackage(rootDir.path).toList();
8787

88-
List<LibraryElement> libraries = _parseLibraries(files);
88+
List<LibraryElement> libraries = _parseLibraries(files, includes);
8989

90-
if (includes != null && includes.isNotEmpty) {
91-
Iterable knownLibraryNames = libraries.map((l) => l.name);
92-
Set notFound =
93-
new Set.from(includes).difference(new Set.from(knownLibraryNames));
94-
if (notFound.isNotEmpty) {
95-
throw 'Did not find: [${notFound.join(', ')}] in '
96-
'known libraries: [${knownLibraryNames.join(', ')}]';
97-
}
98-
libraries.removeWhere((lib) => !includes.contains(lib.name));
99-
} else {
100-
// remove excluded libraries
101-
excludes.forEach((pattern) {
102-
libraries.removeWhere((lib) {
103-
return lib.name.startsWith(pattern) || lib.name == pattern;
104-
});
90+
// remove excluded libraries
91+
excludes.forEach((pattern) {
92+
libraries.removeWhere((lib) {
93+
return lib.name.startsWith(pattern) || lib.name == pattern;
10594
});
106-
}
95+
});
10796

10897
if (includes.isNotEmpty || excludes.isNotEmpty) {
109-
print('Generating docs for libraries ${libraries.join(', ')}\n');
98+
print('generating docs for libraries ${libraries.join(', ')}\n');
11099
}
111100

112101
Package package = new Package(libraries, packageMeta);
@@ -131,7 +120,8 @@ class DartDoc {
131120
return new DartDocResults(packageMeta, package, outputDir);
132121
}
133122

134-
List<LibraryElement> _parseLibraries(List<String> files) {
123+
List<LibraryElement> _parseLibraries(
124+
List<String> files, List<String> includes) {
135125
Set<LibraryElement> libraries = new Set();
136126
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
137127
List<UriResolver> resolvers = [];
@@ -216,6 +206,14 @@ class DartDoc {
216206
result = context.performAnalysisTask();
217207
}
218208

209+
// Use the includes.
210+
for (Source source in context.librarySources) {
211+
LibraryElement library = context.computeLibraryElement(source);
212+
if (includes.contains(Library.getLibraryName(library))) {
213+
libraries.add(library);
214+
}
215+
}
216+
219217
List<AnalysisErrorInfo> errorInfos = [];
220218

221219
for (Source source in sources) {

lib/src/element_type.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/// The models used to represent Dart code.
66
library dartdoc.element_type;
77

8+
import 'package:analyzer/dart/element/element.dart';
89
import 'package:analyzer/src/generated/element.dart';
910

1011
import 'model.dart';

lib/src/html/templates.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,23 @@ Future<Map<String, String>> _loadPartials(
4141
Future<String> _loadPartial(String templatePath) async {
4242
String template = await _getTemplateFile(templatePath);
4343
if (templatePath.contains('_head') && headerPaths.isNotEmpty) {
44-
String headerValue = headerPaths.map((path) => new File(path).readAsStringSync()).join('\n');
44+
String headerValue = headerPaths
45+
.map((path) => new File(path).readAsStringSync())
46+
.join('\n');
4547
template =
4648
template.replaceAll('<!-- Header Placeholder -->', headerValue);
49+
template =
50+
template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
4751
}
4852
if (templatePath.contains('_footer') && footerPaths.isNotEmpty) {
49-
String footerValue = footerPaths.map((path) => new File(path).readAsStringSync()).join('\n');
53+
String footerValue = footerPaths
54+
.map((path) => new File(path).readAsStringSync())
55+
.join('\n');
5056
template =
5157
template.replaceAll('<!-- Footer Placeholder -->', footerValue);
58+
template =
59+
template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
5260
}
53-
template = template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
5461
return template;
5562
}
5663

lib/src/markdown_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library dartdoc.markdown_processor;
88

99
import 'dart:convert';
1010

11-
import 'package:analyzer/src/generated/ast.dart';
11+
import 'package:analyzer/dart/ast/ast.dart';
1212
import 'package:analyzer/src/generated/element.dart'
1313
show
1414
LibraryElement,

lib/src/model.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ library dartdoc.models;
77

88
import 'dart:convert';
99

10-
import 'package:analyzer/src/generated/ast.dart'
10+
import 'package:analyzer/dart/ast/ast.dart'
1111
show AnnotatedNode, Annotation, Declaration;
1212
import 'package:analyzer/src/generated/element.dart';
1313
import 'package:analyzer/src/generated/resolver.dart'
@@ -902,6 +902,17 @@ abstract class GetterSetterCombo {
902902
class Library extends ModelElement {
903903
static final Map<String, Library> _libraryMap = <String, Library>{};
904904

905+
static String getLibraryName(LibraryElement element) {
906+
String name = element.name;
907+
908+
if (name == null || name.isEmpty) {
909+
name = element.definingCompilationUnit.name;
910+
name = name.substring(0, name.length - '.dart'.length);
911+
}
912+
913+
return name;
914+
}
915+
905916
final Package package;
906917
List<Class> _classes;
907918
List<Class> _enums;

0 commit comments

Comments
 (0)