| OLD | NEW |
| (Empty) | |
| 1 library docgen.models.library; |
| 2 |
| 3 import 'dart:io'; |
| 4 |
| 5 import 'package:markdown/markdown.dart' as markdown; |
| 6 |
| 7 import '../exports/source_mirrors.dart'; |
| 8 import '../exports/mirrors_util.dart' as dart2js_util; |
| 9 |
| 10 import '../library_helpers.dart'; |
| 11 import '../package_helpers.dart'; |
| 12 |
| 13 import 'class.dart'; |
| 14 import 'dummy_mirror.dart'; |
| 15 import 'indexable.dart'; |
| 16 import 'method.dart'; |
| 17 import 'model_helpers.dart'; |
| 18 import 'typedef.dart'; |
| 19 import 'variable.dart'; |
| 20 |
| 21 /// A class containing contents of a Dart library. |
| 22 class Library extends Indexable { |
| 23 final Map<String, Class> classes = {}; |
| 24 final Map<String, Typedef> typedefs = {}; |
| 25 final Map<String, Class> errors = {}; |
| 26 |
| 27 /// Top-level variables in the library. |
| 28 Map<String, Variable> variables; |
| 29 |
| 30 /// Top-level functions in the library. |
| 31 Map<String, Method> functions; |
| 32 |
| 33 String packageName = ''; |
| 34 bool _hasBeenCheckedForPackage = false; |
| 35 String packageIntro; |
| 36 |
| 37 Library get owningLibrary => this; |
| 38 |
| 39 /// Returns the [Library] for the given [mirror] if it has already been |
| 40 /// created, else creates it. |
| 41 factory Library(LibraryMirror mirror) { |
| 42 var library = getDocgenObject(mirror); |
| 43 if (library is DummyMirror) { |
| 44 library = new Library._(mirror); |
| 45 } |
| 46 return library; |
| 47 } |
| 48 |
| 49 Library._(LibraryMirror libraryMirror) : super(libraryMirror) { |
| 50 var exported = calcExportedItems(libraryMirror); |
| 51 var exportedClasses = addAll(exported['classes'], |
| 52 dart2js_util.typesOf(libraryMirror.declarations)); |
| 53 updateLibraryPackage(mirror); |
| 54 exportedClasses.forEach((String mirrorName, TypeMirror mirror) { |
| 55 if (mirror is TypedefMirror) { |
| 56 // This is actually a Dart2jsTypedefMirror, and it does define value, |
| 57 // but we don't have visibility to that type. |
| 58 if (includePrivateMembers || !mirror.isPrivate) { |
| 59 typedefs[dart2js_util.nameOf(mirror)] = new Typedef(mirror, this); |
| 60 } |
| 61 } else if (mirror is ClassMirror) { |
| 62 var clazz = new Class(mirror, this); |
| 63 |
| 64 if (clazz.isError()) { |
| 65 errors[dart2js_util.nameOf(mirror)] = clazz; |
| 66 } else { |
| 67 classes[dart2js_util.nameOf(mirror)] = clazz; |
| 68 } |
| 69 } else { |
| 70 throw new ArgumentError( |
| 71 '${dart2js_util.nameOf(mirror)} - no class type match. '); |
| 72 } |
| 73 }); |
| 74 this.functions = createMethods(addAll(exported['methods'], |
| 75 libraryMirror.declarations.values.where( |
| 76 (mirror) => mirror is MethodMirror)).values, this); |
| 77 this.variables = createVariables(addAll(exported['variables'], |
| 78 dart2js_util.variablesOf(libraryMirror.declarations)).values, this); |
| 79 } |
| 80 |
| 81 /// Look for the specified name starting with the current member, and |
| 82 /// progressively working outward to the current library scope. |
| 83 String findElementInScope(String name) { |
| 84 var lookupFunc = determineLookupFunc(name); |
| 85 var libraryScope = lookupFunc(mirror, name); |
| 86 if (libraryScope != null) { |
| 87 var result = getDocgenObject(libraryScope, this); |
| 88 if (result is DummyMirror) return packagePrefix + result.docName; |
| 89 return result.packagePrefix + result.docName; |
| 90 } |
| 91 return super.findElementInScope(name); |
| 92 } |
| 93 |
| 94 String getMdnComment() => ''; |
| 95 |
| 96 /// For a library's [mirror], determine the name of the package (if any) we |
| 97 /// believe it came from (because of its file URI). |
| 98 /// |
| 99 /// If no package could be determined, we return an empty string. |
| 100 void updateLibraryPackage(LibraryMirror mirror) { |
| 101 if (mirror == null) return; |
| 102 if (_hasBeenCheckedForPackage) return; |
| 103 _hasBeenCheckedForPackage = true; |
| 104 if (mirror.uri.scheme != 'file') return; |
| 105 packageName = getPackageName(mirror); |
| 106 // Associate the package readme with all the libraries. This is a bit |
| 107 // wasteful, but easier than trying to figure out which partial match |
| 108 // is best. |
| 109 packageIntro = _packageIntro(getPackageDirectory(mirror)); |
| 110 } |
| 111 |
| 112 String _packageIntro(packageDir) { |
| 113 if (packageDir == null) return null; |
| 114 var dir = new Directory(packageDir); |
| 115 var files = dir.listSync(); |
| 116 var readmes = files.where((FileSystemEntity each) => (each is File && |
| 117 each.path.substring(packageDir.length + 1, each.path.length) |
| 118 .startsWith('README'))).toList(); |
| 119 if (readmes.isEmpty) return ''; |
| 120 // If there are multiples, pick the shortest name. |
| 121 readmes.sort((a, b) => a.path.length.compareTo(b.path.length)); |
| 122 var readme = readmes.first; |
| 123 var linkResolver = (name) => globalFixReference(name); |
| 124 var contents = markdown.markdownToHtml(readme |
| 125 .readAsStringSync(), linkResolver: linkResolver, |
| 126 inlineSyntaxes: MARKDOWN_SYNTAXES); |
| 127 return contents; |
| 128 } |
| 129 |
| 130 String get packagePrefix => packageName == null || packageName.isEmpty ? |
| 131 '' : '$packageName/'; |
| 132 |
| 133 Map get previewMap { |
| 134 var map = {'packageName': packageName}; |
| 135 map.addAll(super.previewMap); |
| 136 if (packageIntro != null) { |
| 137 map['packageIntro'] = packageIntro; |
| 138 } |
| 139 return map; |
| 140 } |
| 141 |
| 142 String get name => docName; |
| 143 |
| 144 String get docName { |
| 145 return dart2js_util.qualifiedNameOf(mirror).replaceAll('.', '-'); |
| 146 } |
| 147 |
| 148 /// Generates a map describing the [Library] object. |
| 149 Map toMap() => { |
| 150 'name': name, |
| 151 'qualifiedName': qualifiedName, |
| 152 'comment': comment, |
| 153 'variables': recurseMap(variables), |
| 154 'functions': expandMethodMap(functions), |
| 155 'classes': { |
| 156 'class': classes.values.where((c) => c.isVisible) |
| 157 .map((e) => e.previewMap).toList(), |
| 158 'typedef': recurseMap(typedefs), |
| 159 'error': errors.values.where((e) => e.isVisible) |
| 160 .map((e) => e.previewMap).toList() |
| 161 }, |
| 162 'packageName': packageName, |
| 163 'packageIntro': packageIntro |
| 164 }; |
| 165 |
| 166 String get typeName => 'library'; |
| 167 |
| 168 bool isValidMirror(DeclarationMirror mirror) => mirror is LibraryMirror; |
| 169 } |
| OLD | NEW |