Skip to content

Commit 1bb7bc5

Browse files
committed
Merge pull request #1132 from dart-lang/headers
extend --include and --header
2 parents 453db90 + 1668517 commit 1bb7bc5

File tree

12 files changed

+299
-133
lines changed

12 files changed

+299
-133
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 not
7+
directly inside) the project (`--include-external`)
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: 30 additions & 27 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,21 +68,24 @@ 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'];
73+
List<String> includeExternals = args['include-external'];
7574

7675
String url = args['hosted-url'];
77-
String footerFilePath = _resolveTildePath(args['footer']);
78-
if (footerFilePath != null && !new File(footerFilePath).existsSync()) {
79-
print("Error: unable to locate the file with footer at ${footerFilePath}.");
80-
exit(1);
76+
List<String> footerFilePaths = args['footer'].map(_resolveTildePath).toList();
77+
for (String footerFilePath in footerFilePaths) {
78+
if (!new File(footerFilePath).existsSync()) {
79+
print("Error: unable to locate footer file: ${footerFilePath}.");
80+
exit(1);
81+
}
8182
}
82-
String headerFilePath = _resolveTildePath(args['header']);
83-
if (headerFilePath != null && !new File(headerFilePath).existsSync()) {
84-
print("Error: unable to locate the file with header at ${headerFilePath}.");
85-
exit(1);
83+
List<String> headerFilePaths = args['header'].map(_resolveTildePath).toList();
84+
for (String headerFilePath in footerFilePaths) {
85+
if (!new File(headerFilePath).existsSync()) {
86+
print("Error: unable to locate header file: ${headerFilePath}.");
87+
exit(1);
88+
}
8689
}
8790

8891
Directory outputDir =
@@ -112,7 +115,7 @@ main(List<String> arguments) async {
112115
print('');
113116

114117
var generators = await initGenerators(
115-
url, headerFilePath, footerFilePath, args['rel-canonical-prefix']);
118+
url, headerFilePaths, footerFilePaths, args['rel-canonical-prefix']);
116119

117120
for (var generator in generators) {
118121
generator.onFileCreated.listen(_onProgress);
@@ -124,7 +127,8 @@ main(List<String> arguments) async {
124127
initializeConfig(addCrossdart: addCrossdart, includeSource: includeSource);
125128

126129
var dartdoc = new DartDoc(inputDir, excludeLibraries, sdkDir, generators,
127-
outputDir, packageMeta, includeLibraries);
130+
outputDir, packageMeta, includeLibraries,
131+
includeExternals: includeExternals);
128132

129133
Chain.capture(() async {
130134
DartDocResults results = await dartdoc.generateDocs();
@@ -163,7 +167,7 @@ ArgParser _createArgsParser() {
163167
parser.addFlag('version',
164168
help: 'Display the version for $name.', negatable: false);
165169
parser.addFlag('add-crossdart',
166-
help: 'Add Crossdart links to the source code pieces',
170+
help: 'Add Crossdart links to the source code pieces.',
167171
negatable: false,
168172
defaultsTo: false);
169173
parser.addOption('dart-sdk',
@@ -181,26 +185,25 @@ ArgParser _createArgsParser() {
181185
parser.addOption('output',
182186
help: 'Path to output directory.', defaultsTo: defaultOutDir);
183187
parser.addOption('header',
184-
help:
185-
'path to file containing HTML text, inserted into the header of every page.');
188+
allowMultiple: true, help: 'path to file containing HTML text.');
186189
parser.addOption('footer',
187-
help:
188-
'path to file containing HTML text, inserted into the footer of every page.');
190+
allowMultiple: true, help: 'path to file containing HTML text.');
189191
parser.addOption('exclude',
190-
help: 'Comma-separated list of library names to ignore.');
192+
allowMultiple: true, help: 'library names to ignore');
191193
parser.addOption('include',
192-
help: 'Comma-separated list of library names to generate docs for.');
194+
allowMultiple: true, help: 'library names to generate docs for');
195+
parser.addOption('include-external',
196+
allowMultiple: true, help: 'additional (external) libraries to include');
193197
parser.addOption('hosted-url',
194198
help:
195199
'URL where the docs will be hosted (used to generate the sitemap).');
196200
parser.addOption('rel-canonical-prefix',
197-
help: 'If provided, add a rel="canonical" prefixed with provided value. '
201+
help:
202+
'If provided, add a rel="canonical" prefixed with provided value. \n'
198203
'Consider using if building many versions of the docs for public SEO. '
199-
'Learn more at https://goo.gl/gktN6F');
204+
'Learn more at https://goo.gl/gktN6F.');
200205
parser.addFlag('include-source',
201-
help: 'If source code blocks should be shown, if they exist.',
202-
negatable: true,
203-
defaultsTo: true);
206+
help: 'Show source code blocks', negatable: true, defaultsTo: true);
204207
return parser;
205208
}
206209

lib/dartdoc.dart

Lines changed: 21 additions & 10 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';
@@ -44,13 +44,13 @@ const String version = '0.9.2';
4444
final String defaultOutDir = p.join('doc', 'api');
4545

4646
/// Initialize and setup the generators.
47-
Future<List<Generator>> initGenerators(String url, String headerFilePath,
48-
String footerFilePath, String relCanonicalPrefix) async {
47+
Future<List<Generator>> initGenerators(String url, List<String> headerFilePaths,
48+
List<String> footerFilePaths, String relCanonicalPrefix) async {
4949
return [
5050
await HtmlGenerator.create(
5151
url: url,
52-
header: headerFilePath,
53-
footer: footerFilePath,
52+
headers: headerFilePaths,
53+
footers: footerFilePaths,
5454
relCanonicalPrefix: relCanonicalPrefix,
5555
toolVersion: version)
5656
];
@@ -60,17 +60,19 @@ Future<List<Generator>> initGenerators(String url, String headerFilePath,
6060
/// directory.
6161
class DartDoc {
6262
final Directory rootDir;
63-
final List<String> excludes;
6463
final Directory sdkDir;
6564
final List<Generator> generators;
6665
final Directory outputDir;
6766
final PackageMeta packageMeta;
6867
final List<String> includes;
68+
final List<String> includeExternals;
69+
final List<String> excludes;
6970

7071
Stopwatch _stopwatch;
7172

7273
DartDoc(this.rootDir, this.excludes, this.sdkDir, this.generators,
73-
this.outputDir, this.packageMeta, this.includes);
74+
this.outputDir, this.packageMeta, this.includes,
75+
{this.includeExternals: const []});
7476

7577
/// Generate DartDoc documentation.
7678
///
@@ -85,7 +87,7 @@ class DartDoc {
8587
? const []
8688
: findFilesToDocumentInPackage(rootDir.path).toList();
8789

88-
List<LibraryElement> libraries = _parseLibraries(files);
90+
List<LibraryElement> libraries = _parseLibraries(files, includeExternals);
8991

9092
if (includes != null && includes.isNotEmpty) {
9193
Iterable knownLibraryNames = libraries.map((l) => l.name);
@@ -106,7 +108,7 @@ class DartDoc {
106108
}
107109

108110
if (includes.isNotEmpty || excludes.isNotEmpty) {
109-
print('Generating docs for libraries ${libraries.join(', ')}\n');
111+
print('generating docs for libraries ${libraries.join(', ')}\n');
110112
}
111113

112114
Package package = new Package(libraries, packageMeta);
@@ -131,7 +133,8 @@ class DartDoc {
131133
return new DartDocResults(packageMeta, package, outputDir);
132134
}
133135

134-
List<LibraryElement> _parseLibraries(List<String> files) {
136+
List<LibraryElement> _parseLibraries(
137+
List<String> files, List<String> includeExternals) {
135138
Set<LibraryElement> libraries = new Set();
136139
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
137140
List<UriResolver> resolvers = [];
@@ -216,6 +219,14 @@ class DartDoc {
216219
result = context.performAnalysisTask();
217220
}
218221

222+
// Use the includeExternals.
223+
for (Source source in context.librarySources) {
224+
LibraryElement library = context.computeLibraryElement(source);
225+
if (includeExternals.contains(Library.getLibraryName(library))) {
226+
libraries.add(library);
227+
}
228+
}
229+
219230
List<AnalysisErrorInfo> errorInfos = [];
220231

221232
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/html_generator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class HtmlGenerator extends Generator {
4848
/// [url] - optional URL for where the docs will be hosted.
4949
static Future<HtmlGenerator> create(
5050
{String url,
51-
String header,
52-
String footer,
51+
List<String> headers,
52+
List<String> footers,
5353
String relCanonicalPrefix,
5454
String toolVersion}) async {
5555
var templates =
56-
await Templates.create(headerPath: header, footerPath: footer);
56+
await Templates.create(headerPaths: headers, footerPaths: footers);
5757

5858
if (toolVersion == null) {
5959
toolVersion = 'unknown';

lib/src/html/templates.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,31 @@ const _partials = const <String>[
3232
];
3333

3434
Future<Map<String, String>> _loadPartials(
35-
String headerPath, String footerPath) async {
35+
List<String> headerPaths, List<String> footerPaths) async {
36+
headerPaths ??= [];
37+
footerPaths ??= [];
38+
3639
var partials = <String, String>{};
3740

3841
Future<String> _loadPartial(String templatePath) async {
3942
String template = await _getTemplateFile(templatePath);
40-
// TODO: revisit, not sure this is the right place for this logic
41-
if (templatePath.contains('_footer') && footerPath != null) {
42-
String footerValue = await new File(footerPath).readAsString();
43+
if (templatePath.contains('_head') && headerPaths.isNotEmpty) {
44+
String headerValue = headerPaths
45+
.map((path) => new File(path).readAsStringSync())
46+
.join('\n');
4347
template =
44-
template.replaceAll('<!-- Footer Placeholder -->', footerValue);
48+
template.replaceAll('<!-- Header Placeholder -->', headerValue);
49+
template =
50+
template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
4551
}
46-
if (templatePath.contains('_head') && headerPath != null) {
47-
String headerValue = await new File(headerPath).readAsString();
52+
if (templatePath.contains('_footer') && footerPaths.isNotEmpty) {
53+
String footerValue = footerPaths
54+
.map((path) => new File(path).readAsStringSync())
55+
.join('\n');
4856
template =
49-
template.replaceAll('<!-- Header Placeholder -->', headerValue);
57+
template.replaceAll('<!-- Footer Placeholder -->', footerValue);
58+
template =
59+
template.replaceAll(' <!-- Do not remove placeholder -->\n', '');
5060
}
5161
return template;
5262
}
@@ -75,8 +85,8 @@ class Templates {
7585
final TemplateRenderer typeDefTemplate;
7686

7787
static Future<Templates> create(
78-
{String headerPath, String footerPath}) async {
79-
var partials = await _loadPartials(headerPath, footerPath);
88+
{List<String> headerPaths, List<String> footerPaths}) async {
89+
var partials = await _loadPartials(headerPaths, footerPaths);
8090

8191
String _partial(String name) {
8292
String partial = partials[name];

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)