OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// **docgen** is a tool for creating machine readable representations of Dart | 5 /// **docgen** is a tool for creating machine readable representations of Dart |
6 /// code metadata, including: classes, members, comments and annotations. | 6 /// code metadata, including: classes, members, comments and annotations. |
7 /// | 7 /// |
8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. | 8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. |
9 /// | 9 /// |
10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] | 10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] |
(...skipping 24 matching lines...) Expand all Loading... | |
35 import '../../../sdk/lib/_internal/libraries.dart'; | 35 import '../../../sdk/lib/_internal/libraries.dart'; |
36 | 36 |
37 var logger = new Logger('Docgen'); | 37 var logger = new Logger('Docgen'); |
38 | 38 |
39 const DEFAULT_OUTPUT_DIRECTORY = 'docs'; | 39 const DEFAULT_OUTPUT_DIRECTORY = 'docs'; |
40 | 40 |
41 var _outputDirectory; | 41 var _outputDirectory; |
42 | 42 |
43 const String USAGE = 'Usage: dart docgen.dart [OPTIONS] fooDir/barFile'; | 43 const String USAGE = 'Usage: dart docgen.dart [OPTIONS] fooDir/barFile'; |
44 | 44 |
45 | |
46 List<String> skippedAnnotations = const [ | 45 List<String> skippedAnnotations = const [ |
47 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', | 46 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', |
48 '_js_helper.Returns']; | 47 '_js_helper.Returns']; |
49 | 48 |
49 /// If we can't find the SDK introduction text, which will happen if running | |
50 /// from a snapshot and using --parse-sdk or --include-sdk, then use this | |
51 /// hard-coded version. This should be updated to be consistent with the text | |
52 /// in docgen/doc/sdk-introduction.md | |
ricow1
2014/01/08 06:39:49
any reason to keep it in that file then? if we onl
Alan Knight
2014/01/08 22:19:02
Sounds reasonable. Done.
| |
53 const DEFAULT_SDK_INTRODUCTION = """ | |
54 Welcome to the Dart API reference documentation, | |
55 covering the official Dart API libraries. | |
56 Some of the most fundamental Dart libraries include: | |
57 | |
58 * [dart:core](#dart:core): | |
59 Core functionality such as strings, numbers, collections, errors, | |
60 dates, and URIs. | |
61 * [dart:html](#dart:html): | |
62 DOM manipulation for web apps. | |
63 * [dart:io](#dart:io): | |
64 I/O for command-line apps. | |
65 | |
66 Except for dart:core, you must import a library before you can use it. | |
67 Here's an example of importing dart:html, dart:math, and a | |
68 third popular library called | |
69 [polymer.dart](http://www.dartlang.org/polymer-dart/): | |
70 | |
71 import 'dart:html'; | |
72 import 'dart:math'; | |
73 import 'package:polymer/polymer.dart'; | |
74 | |
75 Polymer.dart is an example of a library that isn't | |
76 included in the Dart download, | |
77 but is easy to get and update using the _pub package manager_. | |
78 For information on finding, using, and publishing libraries (and more) | |
79 with pub, see | |
80 [pub.dartlang.org](http://pub.dartlang.org). | |
81 | |
82 The main site for learning and using Dart is | |
83 [www.dartlang.org](http://www.dartlang.org). | |
84 Check out these pages: | |
85 | |
86 * [Dart homepage](http://www.dartlang.org) | |
87 * [Tutorials](http://www.dartlang.org/docs/tutorials/) | |
88 * [Programmer's Guide](http://www.dartlang.org/docs/) | |
89 * [Samples](http://www.dartlang.org/samples/) | |
90 * [A Tour of the Dart Libraries](http://www.dartlang.org/docs/dart-up-and-runn ing/contents/ch03.html) | |
91 | |
92 This API reference is automatically generated from the source code in the | |
93 [Dart project](https://code.google.com/p/dart/). | |
94 If you'd like to contribute to this documentation, see | |
95 [Contributing](https://code.google.com/p/dart/wiki/Contributing) | |
96 and | |
97 [Writing API Documentation](https://code.google.com/p/dart/wiki/WritingApiDocume ntation). | |
98 """; | |
99 | |
50 /// Set of libraries declared in the SDK, so libraries that can be accessed | 100 /// Set of libraries declared in the SDK, so libraries that can be accessed |
51 /// when running dart by default. | 101 /// when running dart by default. |
52 Iterable<LibraryMirror> _sdkLibraries; | 102 Iterable<LibraryMirror> _sdkLibraries; |
53 | 103 |
54 /// The dart:core library, which contains all types that are always available | 104 /// The dart:core library, which contains all types that are always available |
55 /// without import. | 105 /// without import. |
56 LibraryMirror _coreLibrary; | 106 LibraryMirror _coreLibrary; |
57 | 107 |
58 /// Support for [:foo:]-style code comments to the markdown parser. | 108 /// Support for [:foo:]-style code comments to the markdown parser. |
59 List<markdown.InlineSyntax> markdownSyntaxes = | 109 List<markdown.InlineSyntax> markdownSyntaxes = |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 librariesToDocument.removeWhere((x) => _excluded.contains(x.simpleName)); | 195 librariesToDocument.removeWhere((x) => _excluded.contains(x.simpleName)); |
146 _documentLibraries(librariesToDocument, includeSdk: includeSdk, | 196 _documentLibraries(librariesToDocument, includeSdk: includeSdk, |
147 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk, | 197 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk, |
148 introduction: introduction); | 198 introduction: introduction); |
149 return true; | 199 return true; |
150 }); | 200 }); |
151 } | 201 } |
152 | 202 |
153 /// All of the directories for our dependent packages | 203 /// All of the directories for our dependent packages |
154 List<String> allDependentPackageDirs(String packageDirectory) { | 204 List<String> allDependentPackageDirs(String packageDirectory) { |
155 var dependentsJson = Process.runSync('pub', ['list-package-dirs'], | 205 var dependentsJson = Process.runSync('/Users/alanknight/dart-git/dart/xcodebui ld/ReleaseIA32/dart-sdk/bin/pub', ['list-package-dirs'], |
kustermann
2014/01/08 08:39:18
Why do you have a hardcoded path to pub here?
Alan Knight
2014/01/08 22:19:02
Yikes. That was debugging code. Removed, thanks.
| |
156 workingDirectory: packageDirectory, runInShell: true); | 206 workingDirectory: packageDirectory, runInShell: true); |
157 if (dependentsJson.exitCode != 0) { | 207 if (dependentsJson.exitCode != 0) { |
158 print(dependentsJson.stderr); | 208 print(dependentsJson.stderr); |
159 } | 209 } |
160 var dependents = JSON.decode(dependentsJson.stdout)['packages']; | 210 var dependents = JSON.decode(dependentsJson.stdout)['packages']; |
161 return dependents.values.toList(); | 211 return dependents.values.toList(); |
162 } | 212 } |
163 | 213 |
164 /// For a library's [mirror], determine the name of the package (if any) we | 214 /// For a library's [mirror], determine the name of the package (if any) we |
165 /// believe it came from (because of its file URI). | 215 /// believe it came from (because of its file URI). |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 } | 301 } |
252 | 302 |
253 String _findPackageRoot(String directory) { | 303 String _findPackageRoot(String directory) { |
254 var files = listDir(directory, recursive: true); | 304 var files = listDir(directory, recursive: true); |
255 // Return '' means that there was no pubspec.yaml and therefor no packageRoot. | 305 // Return '' means that there was no pubspec.yaml and therefor no packageRoot. |
256 String packageRoot = files.firstWhere((f) => | 306 String packageRoot = files.firstWhere((f) => |
257 f.endsWith('${path.separator}pubspec.yaml'), orElse: () => ''); | 307 f.endsWith('${path.separator}pubspec.yaml'), orElse: () => ''); |
258 if (packageRoot != '') { | 308 if (packageRoot != '') { |
259 packageRoot = path.join(path.dirname(packageRoot), 'packages'); | 309 packageRoot = path.join(path.dirname(packageRoot), 'packages'); |
260 } | 310 } |
261 return packageRoot; | 311 return path.normalize(path.absolute(packageRoot)); |
262 } | 312 } |
263 | 313 |
264 /// Read a pubspec and return the library name. | 314 /// Read a pubspec and return the library name. |
265 String _packageName(String pubspecName) { | 315 String _packageName(String pubspecName) { |
266 File pubspec = new File(pubspecName); | 316 File pubspec = new File(pubspecName); |
267 if (!pubspec.existsSync()) return ''; | 317 if (!pubspec.existsSync()) return ''; |
268 var contents = pubspec.readAsStringSync(); | 318 var contents = pubspec.readAsStringSync(); |
269 var spec = loadYaml(contents); | 319 var spec = loadYaml(contents); |
270 return spec["name"]; | 320 return spec["name"]; |
271 } | 321 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 // Currently, a string is thrown when it fails to create a mirror | 377 // Currently, a string is thrown when it fails to create a mirror |
328 // system, and it is not possible to use the stack trace. BUG(#11622) | 378 // system, and it is not possible to use the stack trace. BUG(#11622) |
329 // To avoid printing the stack trace. | 379 // To avoid printing the stack trace. |
330 exit(1); | 380 exit(1); |
331 }); | 381 }); |
332 } | 382 } |
333 | 383 |
334 /// Creates documentation for filtered libraries. | 384 /// Creates documentation for filtered libraries. |
335 void _documentLibraries(List<LibraryMirror> libs, {bool includeSdk: false, | 385 void _documentLibraries(List<LibraryMirror> libs, {bool includeSdk: false, |
336 bool outputToYaml: true, bool append: false, bool parseSdk: false, | 386 bool outputToYaml: true, bool append: false, bool parseSdk: false, |
337 String introduction: ''}) { | 387 String introduction: ''}) { |
kustermann
2014/01/08 08:39:18
Since you pass 'introduction' to 'readIntro(String
Alan Knight
2014/01/08 22:19:02
Done. Also re-arranged a bit since we no longer re
| |
338 libs.forEach((lib) { | 388 libs.forEach((lib) { |
339 // Files belonging to the SDK have a uri that begins with 'dart:'. | 389 // Files belonging to the SDK have a uri that begins with 'dart:'. |
340 if (includeSdk || !lib.uri.toString().startsWith('dart:')) { | 390 if (includeSdk || !lib.uri.toString().startsWith('dart:')) { |
341 var library = generateLibrary(lib); | 391 var library = generateLibrary(lib); |
342 entityMap[library.name] = library; | 392 entityMap[library.name] = library; |
343 } | 393 } |
344 }); | 394 }); |
345 // After everything is created, do a pass through all classes to make sure no | 395 // After everything is created, do a pass through all classes to make sure no |
346 // intermediate classes created by mixins are included, all the links to | 396 // intermediate classes created by mixins are included, all the links to |
347 // exported members point to the new library. | 397 // exported members point to the new library. |
348 entityMap.values.where((e) => e is Class).forEach( | 398 entityMap.values.where((e) => e is Class).forEach( |
349 (c) => c.updateLinksAndRemoveIntermediaryClasses()); | 399 (c) => c.updateLinksAndRemoveIntermediaryClasses()); |
350 // Everything is a subclass of Object, therefore empty the list to avoid a | 400 // Everything is a subclass of Object, therefore empty the list to avoid a |
351 // giant list of subclasses to be printed out. | 401 // giant list of subclasses to be printed out. |
352 if (includeSdk) (entityMap['dart-core.Object'] as Class).subclasses.clear(); | 402 if (includeSdk) (entityMap['dart-core.Object'] as Class).subclasses.clear(); |
353 | 403 |
354 var filteredEntities = entityMap.values.where(_isVisible); | 404 var filteredEntities = entityMap.values.where(_isVisible); |
355 | 405 |
356 // Outputs a JSON file with all libraries and their preview comments. | 406 // Outputs a JSON file with all libraries and their preview comments. |
357 // This will help the viewer know what libraries are available to read in. | 407 // This will help the viewer know what libraries are available to read in. |
358 var libraryMap; | 408 var libraryMap; |
359 var linkResolver = (name) => fixReference(name, null, null, null); | 409 var linkResolver = (name) => fixReference(name, null, null, null); |
410 | |
411 String readIntro(String introFileName) { | |
kustermann
2014/01/08 08:39:18
Rename to readIntroductionFile?
Alan Knight
2014/01/08 22:19:02
Done.
| |
412 var introFile = new File(introFileName); | |
413 var introText = introFile.existsSync() ? introFile.readAsStringSync() : | |
414 DEFAULT_SDK_INTRODUCTION; | |
415 return markdown.markdownToHtml(introText, | |
416 linkResolver: linkResolver, inlineSyntaxes: markdownSyntaxes); | |
417 } | |
418 | |
360 if (append) { | 419 if (append) { |
361 var docsDir = listDir(_outputDirectory); | 420 var docsDir = listDir(_outputDirectory); |
362 if (!docsDir.contains('$_outputDirectory/library_list.json')) { | 421 if (!docsDir.contains('$_outputDirectory/library_list.json')) { |
363 throw new StateError('No library_list.json'); | 422 throw new StateError('No library_list.json'); |
364 } | 423 } |
365 libraryMap = | 424 libraryMap = |
366 JSON.decode(new File( | 425 JSON.decode(new File( |
367 '$_outputDirectory/library_list.json').readAsStringSync()); | 426 '$_outputDirectory/library_list.json').readAsStringSync()); |
368 libraryMap['libraries'].addAll(filteredEntities | 427 libraryMap['libraries'].addAll(filteredEntities |
369 .where((e) => e is Library) | 428 .where((e) => e is Library) |
370 .map((e) => e.previewMap)); | 429 .map((e) => e.previewMap)); |
371 if (introduction.isNotEmpty) { | 430 if (introduction.isNotEmpty) { |
372 var intro = libraryMap['introduction']; | 431 var intro = libraryMap['introduction']; |
373 if (intro.isNotEmpty) intro += '<br/><br/>'; | 432 if (intro.isNotEmpty) intro += '<br/><br/>'; |
374 intro += markdown.markdownToHtml( | 433 libraryMap['introduction'] = "$intro${readIntro(introduction)}"; |
375 new File(introduction).readAsStringSync(), | |
376 linkResolver: linkResolver, inlineSyntaxes: markdownSyntaxes); | |
377 libraryMap['introduction'] = intro; | |
378 } | 434 } |
379 outputToYaml = libraryMap['filetype'] == 'yaml'; | 435 outputToYaml = libraryMap['filetype'] == 'yaml'; |
380 } else { | 436 } else { |
381 libraryMap = { | 437 libraryMap = { |
382 'libraries' : filteredEntities.where((e) => | 438 'libraries' : filteredEntities.where((e) => |
383 e is Library).map((e) => e.previewMap).toList(), | 439 e is Library).map((e) => e.previewMap).toList(), |
384 'introduction' : introduction == '' ? | 440 'introduction' : introduction.isEmpty ? '' : readIntro(introduction), |
385 '' : markdown.markdownToHtml(new File(introduction) | |
386 .readAsStringSync(), linkResolver: linkResolver, | |
387 inlineSyntaxes: markdownSyntaxes), | |
388 'filetype' : outputToYaml ? 'yaml' : 'json' | 441 'filetype' : outputToYaml ? 'yaml' : 'json' |
389 }; | 442 }; |
390 } | 443 } |
391 _writeToFile(JSON.encode(libraryMap), 'library_list.json'); | 444 _writeToFile(JSON.encode(libraryMap), 'library_list.json'); |
392 | 445 |
393 // Output libraries and classes to file after all information is generated. | 446 // Output libraries and classes to file after all information is generated. |
394 filteredEntities.where((e) => e is Class || e is Library).forEach((output) { | 447 filteredEntities.where((e) => e is Class || e is Library).forEach((output) { |
395 _writeIndexableToFile(output, outputToYaml); | 448 _writeIndexableToFile(output, outputToYaml); |
396 }); | 449 }); |
397 | 450 |
(...skipping 1303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1701 /// Remove statics from the map of inherited items before adding them. | 1754 /// Remove statics from the map of inherited items before adding them. |
1702 Map _filterStatics(Map items) { | 1755 Map _filterStatics(Map items) { |
1703 var result = {}; | 1756 var result = {}; |
1704 items.forEach((name, item) { | 1757 items.forEach((name, item) { |
1705 if (!item.isStatic) { | 1758 if (!item.isStatic) { |
1706 result[name] = item; | 1759 result[name] = item; |
1707 } | 1760 } |
1708 }); | 1761 }); |
1709 return result; | 1762 return result; |
1710 } | 1763 } |
OLD | NEW |