OLD | NEW |
(Empty) | |
| 1 library docs; |
| 2 |
| 3 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.da
rt'; |
| 4 import '../../../../sdk/lib/_internal/dartdoc/lib/src/json_serializer.dart'; |
| 5 import '../../../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; |
| 6 import '../../../../utils/apidoc/lib/metadata.dart'; |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 final HTML_LIBRARY_NAMES = ['dart:html', |
| 11 'dart:svg', |
| 12 'dart:web_audio', |
| 13 'dart:indexed_db']; |
| 14 /** |
| 15 * Converts the libraries in [HTML_LIBRARY_NAMES] to a json file at [jsonPath] |
| 16 * given the library path at [libPath]. |
| 17 * |
| 18 * The json output looks like: |
| 19 * { |
| 20 * $library_name: { |
| 21 * $interface_name: { |
| 22 * comment: "$comment" |
| 23 * members: { |
| 24 * $member: "$comment", |
| 25 * ... |
| 26 * } |
| 27 * }, |
| 28 * ... |
| 29 * }, |
| 30 * ... |
| 31 * } |
| 32 */ |
| 33 bool convert(Path libPath, Path jsonPath) { |
| 34 var anyErrors = false; |
| 35 |
| 36 var paths = <Path>[]; |
| 37 for (var libraryName in HTML_LIBRARY_NAMES) { |
| 38 paths.add(new Path(libraryName)); |
| 39 } |
| 40 |
| 41 final compilation = new Compilation.library(paths, libPath, null, |
| 42 <String>['--preserve-comments']); |
| 43 |
| 44 var convertedJson = _generateJsonFromLibraries(compilation); |
| 45 |
| 46 anyErrors = anyErrors || _exportJsonToFile(jsonPath, convertedJson); |
| 47 print(prettySerialize(convertedJson)); |
| 48 |
| 49 return anyErrors; |
| 50 } |
| 51 |
| 52 bool _exportJsonToFile(Path jsonPath, Map convertedJson) { |
| 53 final jsonFile = new File.fromPath(jsonPath); |
| 54 var writeJson = prettySerialize(convertedJson); |
| 55 |
| 56 var outputStream = jsonFile.openOutputStream(); |
| 57 outputStream.writeString(writeJson); |
| 58 |
| 59 return false; |
| 60 } |
| 61 |
| 62 Map _generateJsonFromLibraries(Compilation compilation) { |
| 63 var convertedJson = {}; |
| 64 |
| 65 // Sort the libraries by name (not key). |
| 66 var sortedLibraries = new List<LibraryMirror>.from( |
| 67 compilation.mirrors.libraries.values.where( |
| 68 (e) => HTML_LIBRARY_NAMES.indexOf(e.uri.toString()) >= 0)) |
| 69 ..sort((x, y) => |
| 70 x.uri.toString().toUpperCase().compareTo( |
| 71 y.uri.toString().toUpperCase())); |
| 72 |
| 73 for (LibraryMirror libMirror in sortedLibraries) { |
| 74 var libraryJson = {}; |
| 75 var sortedClasses = _sortAndFilterMirrors( |
| 76 libMirror.classes.values.toList()); |
| 77 |
| 78 for (ClassMirror classMirror in sortedClasses) { |
| 79 var classJson = {}; |
| 80 var sortedMembers = _sortAndFilterMirrors( |
| 81 classMirror.members.values.toList()); |
| 82 |
| 83 var membersJson = {}; |
| 84 for (var memberMirror in sortedMembers) { |
| 85 var memberDomName = domNames(memberMirror)[0]; |
| 86 var memberComment = computeComment(memberMirror); |
| 87 if (memberComment != null) { |
| 88 membersJson.putIfAbsent(memberDomName, () => memberComment); |
| 89 } |
| 90 } |
| 91 |
| 92 if (computeComment(classMirror) != null) { |
| 93 classJson.putIfAbsent('comment', () => |
| 94 computeComment(classMirror)); |
| 95 } |
| 96 if (!membersJson.isEmpty) { |
| 97 classJson.putIfAbsent('members', () => |
| 98 membersJson); |
| 99 } |
| 100 |
| 101 if (!classJson.isEmpty) { |
| 102 libraryJson.putIfAbsent(domNames(classMirror)[0], () => |
| 103 classJson); |
| 104 } |
| 105 } |
| 106 |
| 107 if (!libraryJson.isEmpty) { |
| 108 convertedJson.putIfAbsent(libMirror.displayName, () => |
| 109 libraryJson); |
| 110 } |
| 111 } |
| 112 |
| 113 return convertedJson; |
| 114 } |
| 115 |
| 116 List<DeclarationMirror> _sortAndFilterMirrors(List<DeclarationMirror> mirrors) { |
| 117 return mirrors.where((DeclarationMirror c) => |
| 118 !domNames(c).isEmpty && |
| 119 !c.displayName.startsWith('_') && |
| 120 (findMetadata(c.metadata, 'DocsEditable') != null)) |
| 121 .toList() |
| 122 ..sort((x, y) => |
| 123 domNames(x)[0].toUpperCase().compareTo( |
| 124 domNames(y)[0].toUpperCase())); |
| 125 } |
| 126 |
| 127 /// Given the class mirror, returns the names found or an empty list. |
| 128 List<String> domNames(DeclarationMirror mirror) { |
| 129 var domNameMetadata = findMetadata(mirror.metadata, 'DomName'); |
| 130 |
| 131 if (domNameMetadata != null) { |
| 132 var domNames = <String>[]; |
| 133 var tags = deprecatedFutureValue(domNameMetadata.getField('name')); |
| 134 for (var s in tags.reflectee.split(',')) { |
| 135 domNames.add(s.trim()); |
| 136 } |
| 137 |
| 138 if (domNames.length == 1 && domNames[0] == 'none') return <String>[]; |
| 139 return domNames; |
| 140 } else { |
| 141 return <String>[]; |
| 142 } |
| 143 } |
OLD | NEW |