Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: pkg/docgen/lib/docgen.dart

Issue 196133023: pkg/docgen: more cleanup (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cl commetns Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/docgen/lib/src/io.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 linkResolver: linkResolver, inlineSyntaxes: _MARKDOWN_SYNTAXES); 351 linkResolver: linkResolver, inlineSyntaxes: _MARKDOWN_SYNTAXES);
352 } 352 }
353 353
354 /// Creates documentation for filtered libraries. 354 /// Creates documentation for filtered libraries.
355 static void _documentLibraries(List<LibraryMirror> libs, 355 static void _documentLibraries(List<LibraryMirror> libs,
356 {bool includeSdk: false, bool outputToYaml: true, bool append: false, 356 {bool includeSdk: false, bool outputToYaml: true, bool append: false,
357 bool parseSdk: false, String introFileName: '', String startPage}) { 357 bool parseSdk: false, String introFileName: '', String startPage}) {
358 libs.forEach((lib) { 358 libs.forEach((lib) {
359 // Files belonging to the SDK have a uri that begins with 'dart:'. 359 // Files belonging to the SDK have a uri that begins with 'dart:'.
360 if (includeSdk || !lib.uri.toString().startsWith('dart:')) { 360 if (includeSdk || !lib.uri.toString().startsWith('dart:')) {
361 var library = generateLibrary(lib); 361 generateLibrary(lib);
362 } 362 }
363 }); 363 });
364 364
365 var filteredEntities = new Set<Indexable>(); 365 var filteredEntities = new Set<Indexable>();
366 for (Map<String, Set<Indexable>> firstLevel in 366 for (Map<String, Set<Indexable>> firstLevel in
367 Indexable._mirrorToDocgen.values) { 367 Indexable._mirrorToDocgen.values) {
368 for (Set<Indexable> items in firstLevel.values) { 368 for (Set<Indexable> items in firstLevel.values) {
369 for (Indexable item in items) { 369 for (Indexable item in items) {
370 if (_isFullChainVisible(item)) { 370 if (_isFullChainVisible(item)) {
371 if (item is! Method || 371 if (item is! Method ||
372 (item is Method && item.methodInheritedFrom == null)) { 372 (item is Method && item.methodInheritedFrom == null)) {
373 filteredEntities.add(item); 373 filteredEntities.add(item);
374 } 374 }
375 } 375 }
376 } 376 }
377 } 377 }
378 } 378 }
379 379
380 // Outputs a JSON file with all libraries and their preview comments. 380 // Outputs a JSON file with all libraries and their preview comments.
381 // This will help the viewer know what libraries are available to read in. 381 // This will help the viewer know what libraries are available to read in.
382 var libraryMap; 382 Map<String, dynamic> libraryMap;
383 383
384 if (append) { 384 if (append) {
385 var docsDir = listDir(_outputDirectory); 385 var docsDir = listDir(_outputDirectory);
386 if (!docsDir.contains('$_outputDirectory/library_list.json')) { 386 if (!docsDir.contains('$_outputDirectory/library_list.json')) {
387 throw new StateError('No library_list.json'); 387 throw new StateError('No library_list.json');
388 } 388 }
389 libraryMap = 389 libraryMap =
390 JSON.decode(new File( 390 JSON.decode(new File(
391 '$_outputDirectory/library_list.json').readAsStringSync()); 391 '$_outputDirectory/library_list.json').readAsStringSync());
392 libraryMap['libraries'].addAll(filteredEntities 392 libraryMap['libraries'].addAll(filteredEntities
(...skipping 11 matching lines...) Expand all
404 'introduction' : _readIntroductionFile(introFileName, includeSdk), 404 'introduction' : _readIntroductionFile(introFileName, includeSdk),
405 'filetype' : outputToYaml ? 'yaml' : 'json' 405 'filetype' : outputToYaml ? 'yaml' : 'json'
406 }; 406 };
407 } 407 }
408 _writeOutputFiles(libraryMap, filteredEntities, outputToYaml, append, 408 _writeOutputFiles(libraryMap, filteredEntities, outputToYaml, append,
409 startPage); 409 startPage);
410 } 410 }
411 411
412 /// Output all of the libraries and classes into json or yaml files for 412 /// Output all of the libraries and classes into json or yaml files for
413 /// consumption by a viewer. 413 /// consumption by a viewer.
414 static void _writeOutputFiles(libraryMap, 414 static void _writeOutputFiles(Map<String, dynamic> libraryMap,
415 Iterable<Indexable> filteredEntities, bool outputToYaml, bool append, 415 Iterable<Indexable> filteredEntities, bool outputToYaml, bool append,
416 String startPage) { 416 String startPage) {
417 if (startPage != null) libraryMap['start-page'] = startPage; 417 if (startPage != null) libraryMap['start-page'] = startPage;
418 418
419 _writeToFile(JSON.encode(libraryMap), 'library_list.json'); 419 _writeToFile(JSON.encode(libraryMap), 'library_list.json');
420 420
421 // Output libraries and classes to file after all information is generated. 421 // Output libraries and classes to file after all information is generated.
422 filteredEntities.where((e) => e is Class || e is Library).forEach((output) { 422 filteredEntities.where((e) => e is Class || e is Library).forEach((output) {
423 _writeIndexableToFile(output, outputToYaml); 423 _writeIndexableToFile(output, outputToYaml);
424 }); 424 });
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 static List<Uri> _findLibrariesToDocument(List<String> args, 551 static List<Uri> _findLibrariesToDocument(List<String> args,
552 bool includeDependentPackages) { 552 bool includeDependentPackages) {
553 if (includeDependentPackages) { 553 if (includeDependentPackages) {
554 args.addAll(_allDependentPackageDirs(args.first)); 554 args.addAll(_allDependentPackageDirs(args.first));
555 } 555 }
556 556
557 var libraries = new List<Uri>(); 557 var libraries = new List<Uri>();
558 for (var arg in args) { 558 for (var arg in args) {
559 if (FileSystemEntity.typeSync(arg) == FileSystemEntityType.FILE) { 559 if (FileSystemEntity.typeSync(arg) == FileSystemEntityType.FILE) {
560 if (arg.endsWith('.dart')) { 560 if (arg.endsWith('.dart')) {
561 libraries.add(new Uri.file(path.absolute(arg))); 561 var lib = new Uri.file(path.absolute(arg));
562 logger.info('Added to libraries: ${libraries.last}'); 562 libraries.add(lib);
563 logger.info('Added to libraries: $lib');
563 } 564 }
564 } else { 565 } else {
565 libraries.addAll(_findFilesToDocumentInPackage(arg)); 566 libraries.addAll(_findFilesToDocumentInPackage(arg));
566 } 567 }
567 } 568 }
568 return libraries; 569 return libraries;
569 } 570 }
570 571
571 /// Given a package name, explore the directory and pull out all top level 572 /// Given a package name, explore the directory and pull out all top level
572 /// library files in the "lib" directory to document. 573 /// library files in the "lib" directory to document.
573 static List<Uri> _findFilesToDocumentInPackage(String packageName) { 574 static List<Uri> _findFilesToDocumentInPackage(String packageName) {
574 var libraries = []; 575 var libraries = [];
575 // To avoid anaylzing package files twice, only files with paths not 576 // To avoid anaylzing package files twice, only files with paths not
576 // containing '/packages' will be added. The only exception is if the file 577 // containing '/packages' will be added. The only exception is if the file
577 // to analyze already has a '/package' in its path. 578 // to analyze already has a '/package' in its path.
578 var files = listDir(packageName, recursive: true).where( 579 var files = listDir(packageName, recursive: true).where(
579 (f) => f.endsWith('.dart') && (!f.contains('${path.separator}packages') 580 (f) => f.endsWith('.dart') && (!f.contains('${path.separator}packages')
580 || packageName.contains('${path.separator}packages'))).toList(); 581 || packageName.contains('${path.separator}packages'))).toList();
581 582
582 files.forEach((String f) { 583 files.forEach((String lib) {
583 // Only include libraries at the top level of "lib" 584 // Only include libraries at the top level of "lib"
584 if (path.basename(path.dirname(f)) == 'lib') { 585 if (path.basename(path.dirname(lib)) == 'lib') {
585 // Only add the file if it does not contain 'part of' 586 // Only add the file if it does not contain 'part of'
586 // TODO(janicejl): Remove when Issue(12406) is resolved. 587 // TODO(janicejl): Remove when Issue(12406) is resolved.
587 var contents = new File(f).readAsStringSync(); 588 var contents = new File(lib).readAsStringSync();
588 if (!(contents.contains(new RegExp('\npart of ')) || 589 if (!(contents.contains(new RegExp('\npart of ')) ||
589 contents.startsWith(new RegExp('part of ')))) { 590 contents.startsWith(new RegExp('part of ')))) {
590 libraries.add(new Uri.file(path.normalize(path.absolute(f)))); 591 libraries.add(new Uri.file(path.normalize(path.absolute(lib))));
591 logger.info('Added to libraries: $f'); 592 logger.info('Added to libraries: $lib');
592 } 593 }
593 } 594 }
594 }); 595 });
595 return libraries; 596 return libraries;
596 } 597 }
597 598
598 /// All of the directories for our dependent packages 599 /// All of the directories for our dependent packages
599 /// If this is not a package, return an empty list. 600 /// If this is not a package, return an empty list.
600 static List<String> _allDependentPackageDirs(String packageDirectory) { 601 static List<String> _allDependentPackageDirs(String packageDirectory) {
601 var packageName = Library.packageNameFor(packageDirectory); 602 var packageName = Library.packageNameFor(packageDirectory);
(...skipping 20 matching lines...) Expand all
622 return sdk; 623 return sdk;
623 } 624 }
624 625
625 /// Return true if this item and all of its owners are all visible. 626 /// Return true if this item and all of its owners are all visible.
626 static bool _isFullChainVisible(Indexable item) { 627 static bool _isFullChainVisible(Indexable item) {
627 return _includePrivate || (!item.isPrivate && (item.owner != null ? 628 return _includePrivate || (!item.isPrivate && (item.owner != null ?
628 _isFullChainVisible(item.owner) : true)); 629 _isFullChainVisible(item.owner) : true));
629 } 630 }
630 631
631 /// Currently left public for testing purposes. :-/ 632 /// Currently left public for testing purposes. :-/
632 static Library generateLibrary(dart2js_mirrors.Dart2JsLibraryMirror library) { 633 static void generateLibrary(dart2js_mirrors.Dart2JsLibraryMirror library) {
633 var result = new Library(library); 634 var result = new Library(library);
634 result._findPackage(library); 635 result._updateLibraryPackage(library);
635 logger.fine('Generated library for ${result.name}'); 636 logger.fine('Generated library for ${result.name}');
636 return result;
637 } 637 }
638 } 638 }
639 639
640 /// Convenience methods wrapped up in a class to pull down the docgen viewer for 640 /// Convenience methods wrapped up in a class to pull down the docgen viewer for
641 /// a viewable website, and start up a server for viewing. 641 /// a viewable website, and start up a server for viewing.
642 class _Viewer { 642 class _Viewer {
643 static String _dartdocViewerString = path.join(Directory.current.path, 643 static String _dartdocViewerString = path.join(Directory.current.path,
644 'dartdoc-viewer'); 644 'dartdoc-viewer');
645 static Directory _dartdocViewerDir = new Directory(_dartdocViewerString); 645 static Directory _dartdocViewerDir = new Directory(_dartdocViewerString);
646 static Directory _topLevelTempDir; 646 static Directory _topLevelTempDir;
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
1209 return new DummyMirror(mirror, owner); 1209 return new DummyMirror(mirror, owner);
1210 } 1210 }
1211 1211
1212 /// Returns true if [mirror] is the correct type of mirror that this Docgen 1212 /// Returns true if [mirror] is the correct type of mirror that this Docgen
1213 /// object wraps. (Workaround for the fact that Types are not first class.) 1213 /// object wraps. (Workaround for the fact that Types are not first class.)
1214 bool _isValidMirror(DeclarationMirror mirror); 1214 bool _isValidMirror(DeclarationMirror mirror);
1215 } 1215 }
1216 1216
1217 /// A class containing contents of a Dart library. 1217 /// A class containing contents of a Dart library.
1218 class Library extends Indexable { 1218 class Library extends Indexable {
1219 final Map<String, Class> classes = {};
1220 final Map<String, Typedef> typedefs = {};
1221 final Map<String, Class> errors = {};
1219 1222
1220 /// Top-level variables in the library. 1223 /// Top-level variables in the library.
1221 Map<String, Variable> variables; 1224 Map<String, Variable> variables;
1222 1225
1223 /// Top-level functions in the library. 1226 /// Top-level functions in the library.
1224 Map<String, Method> functions; 1227 Map<String, Method> functions;
1225 1228
1226 Map<String, Class> classes = {};
1227 Map<String, Typedef> typedefs = {};
1228 Map<String, Class> errors = {};
1229
1230 String packageName = ''; 1229 String packageName = '';
1231 bool _hasBeenCheckedForPackage = false; 1230 bool _hasBeenCheckedForPackage = false;
1232 String packageIntro; 1231 String packageIntro;
1233 1232
1234 Library get _owningLibrary => this; 1233 Library get _owningLibrary => this;
1235 1234
1236 /// Returns the [Library] for the given [mirror] if it has already been 1235 /// Returns the [Library] for the given [mirror] if it has already been
1237 /// created, else creates it. 1236 /// created, else creates it.
1238 factory Library(LibraryMirror mirror) { 1237 factory Library(LibraryMirror mirror) {
1239 var library = Indexable.getDocgenObject(mirror); 1238 var library = Indexable.getDocgenObject(mirror);
1240 if (library is DummyMirror) { 1239 if (library is DummyMirror) {
1241 library = new Library._(mirror); 1240 library = new Library._(mirror);
1242 } 1241 }
1243 return library; 1242 return library;
1244 } 1243 }
1245 1244
1246 Library._(LibraryMirror libraryMirror) : super(libraryMirror) { 1245 Library._(LibraryMirror libraryMirror) : super(libraryMirror) {
1247 var exported = _calcExportedItems(libraryMirror); 1246 var exported = _calcExportedItems(libraryMirror);
1248 var exportedClasses = _addAll(exported['classes'], 1247 var exportedClasses = _addAll(exported['classes'],
1249 dart2js_util.typesOf(libraryMirror.declarations)); 1248 dart2js_util.typesOf(libraryMirror.declarations));
1250 _findPackage(mirror); 1249 _updateLibraryPackage(mirror);
1251 classes = {};
1252 typedefs = {};
1253 errors = {};
1254 exportedClasses.forEach((String mirrorName, TypeMirror mirror) { 1250 exportedClasses.forEach((String mirrorName, TypeMirror mirror) {
1255 if (mirror is TypedefMirror) { 1251 if (mirror is TypedefMirror) {
1256 // This is actually a Dart2jsTypedefMirror, and it does define value, 1252 // This is actually a Dart2jsTypedefMirror, and it does define value,
1257 // but we don't have visibility to that type. 1253 // but we don't have visibility to that type.
1258 if (_Generator._includePrivate || !mirror.isPrivate) { 1254 if (_Generator._includePrivate || !mirror.isPrivate) {
1259 typedefs[dart2js_util.nameOf(mirror)] = new Typedef(mirror, this); 1255 typedefs[dart2js_util.nameOf(mirror)] = new Typedef(mirror, this);
1260 } 1256 }
1261 } else if (mirror is ClassMirror) { 1257 } else if (mirror is ClassMirror) {
1262 var clazz = new Class(mirror, this); 1258 var clazz = new Class(mirror, this);
1263 1259
(...skipping 23 matching lines...) Expand all
1287 var result = Indexable.getDocgenObject(libraryScope, this); 1283 var result = Indexable.getDocgenObject(libraryScope, this);
1288 if (result is DummyMirror) return packagePrefix + result.docName; 1284 if (result is DummyMirror) return packagePrefix + result.docName;
1289 return result.packagePrefix + result.docName; 1285 return result.packagePrefix + result.docName;
1290 } 1286 }
1291 return super.findElementInScope(name); 1287 return super.findElementInScope(name);
1292 } 1288 }
1293 1289
1294 String _mdnComment() => ''; 1290 String _mdnComment() => '';
1295 1291
1296 /// Helper that maps [mirrors] to their simple name in map. 1292 /// Helper that maps [mirrors] to their simple name in map.
1297 Map _addAll(Map map, Iterable<DeclarationMirror> mirrors) { 1293 static Map _addAll(Map map, Iterable<DeclarationMirror> mirrors) {
1298 for (var mirror in mirrors) { 1294 for (var mirror in mirrors) {
1299 map[dart2js_util.nameOf(mirror)] = mirror; 1295 map[dart2js_util.nameOf(mirror)] = mirror;
1300 } 1296 }
1301 return map; 1297 return map;
1302 } 1298 }
1303 1299
1304 /// For a library's [mirror], determine the name of the package (if any) we 1300 /// For a library's [mirror], determine the name of the package (if any) we
1305 /// believe it came from (because of its file URI). 1301 /// believe it came from (because of its file URI).
1306 /// 1302 ///
1307 /// If no package could be determined, we return an empty string. 1303 /// If no package could be determined, we return an empty string.
1308 String _findPackage(LibraryMirror mirror) { 1304 void _updateLibraryPackage(LibraryMirror mirror) {
1309 if (mirror == null) return ''; 1305 if (mirror == null) return;
1310 if (_hasBeenCheckedForPackage) return packageName; 1306 if (_hasBeenCheckedForPackage) return;
1311 _hasBeenCheckedForPackage = true; 1307 _hasBeenCheckedForPackage = true;
1312 if (mirror.uri.scheme != 'file') return ''; 1308 if (mirror.uri.scheme != 'file') return;
1313 packageName = _packageName(mirror); 1309 packageName = _packageName(mirror);
1314 // Associate the package readme with all the libraries. This is a bit 1310 // Associate the package readme with all the libraries. This is a bit
1315 // wasteful, but easier than trying to figure out which partial match 1311 // wasteful, but easier than trying to figure out which partial match
1316 // is best. 1312 // is best.
1317 packageIntro = _packageIntro(_getPackageDirectory(mirror)); 1313 packageIntro = _packageIntro(_getPackageDirectory(mirror));
1318 return packageName;
1319 } 1314 }
1320 1315
1321 String _packageIntro(packageDir) { 1316 String _packageIntro(packageDir) {
1322 if (packageDir == null) return null; 1317 if (packageDir == null) return null;
1323 var dir = new Directory(packageDir); 1318 var dir = new Directory(packageDir);
1324 var files = dir.listSync(); 1319 var files = dir.listSync();
1325 var readmes = files.where((FileSystemEntity each) => (each is File && 1320 var readmes = files.where((FileSystemEntity each) => (each is File &&
1326 each.path.substring(packageDir.length + 1, each.path.length) 1321 each.path.substring(packageDir.length + 1, each.path.length)
1327 .startsWith('README'))).toList(); 1322 .startsWith('README'))).toList();
1328 if (readmes.isEmpty) return ''; 1323 if (readmes.isEmpty) return '';
(...skipping 822 matching lines...) Expand 10 before | Expand all | Expand 10 after
2151 var annotations = []; 2146 var annotations = [];
2152 annotationMirrors.forEach((annotation) { 2147 annotationMirrors.forEach((annotation) {
2153 var docgenAnnotation = new Annotation(annotation, owningLibrary); 2148 var docgenAnnotation = new Annotation(annotation, owningLibrary);
2154 if (!_SKIPPED_ANNOTATIONS.contains( 2149 if (!_SKIPPED_ANNOTATIONS.contains(
2155 dart2js_util.qualifiedNameOf(docgenAnnotation.mirror))) { 2150 dart2js_util.qualifiedNameOf(docgenAnnotation.mirror))) {
2156 annotations.add(docgenAnnotation); 2151 annotations.add(docgenAnnotation);
2157 } 2152 }
2158 }); 2153 });
2159 return annotations; 2154 return annotations;
2160 } 2155 }
OLDNEW
« no previous file with comments | « no previous file | pkg/docgen/lib/src/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698