OLD | NEW |
---|---|
(Empty) | |
1 /** | |
2 * A library for extracting the documentation from the various HTML libraries | |
3 * ([dart:html], [dart:svg], [dart:web_audio], [dart:indexed_db]) and saving | |
4 * those documentation comments to a JSON file. | |
5 */ | |
6 | |
7 library docs; | |
8 | |
9 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.da rt'; | |
10 import '../../../../sdk/lib/_internal/dartdoc/lib/src/json_serializer.dart'; | |
blois
2013/01/24 02:28:24
Sorta nice to import this as 'json'.
Andrei Mouravski
2013/01/24 19:28:51
Why? There's no namespace collision, and this keep
| |
11 import '../../../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; | |
12 import '../../../../utils/apidoc/lib/metadata.dart'; | |
13 import 'dart:async'; | |
14 import 'dart:io'; | |
15 | |
16 /// The various HTML libraries. | |
17 final List<String> HTML_LIBRARY_NAMES = ['dart:html', | |
blois
2013/01/24 02:28:24
indentation is weird- either use 2 spaces or line
Andrei Mouravski
2013/01/24 19:28:51
Done.
| |
18 'dart:svg', | |
19 'dart:web_audio', | |
20 'dart:indexed_db']; | |
21 /** | |
22 * Converts the libraries in [HTML_LIBRARY_NAMES] to a json file at [jsonPath] | |
23 * given the library path at [libPath]. | |
24 * | |
25 * The json output looks like: | |
26 * { | |
27 * $library_name: { | |
28 * $interface_name: { | |
29 * comment: "$comment" | |
30 * members: { | |
31 * $member: "$comment", | |
32 * ... | |
33 * } | |
34 * }, | |
35 * ... | |
36 * }, | |
37 * ... | |
38 * } | |
39 * | |
40 * Returns `true` if any errors were encountered, `false` otherwise. | |
41 */ | |
42 bool convert(Path libPath, Path jsonPath) { | |
43 var anyErrors = false; | |
44 | |
45 var paths = <Path>[]; | |
46 for (var libraryName in HTML_LIBRARY_NAMES) { | |
47 paths.add(new Path(libraryName)); | |
48 } | |
49 | |
50 final compilation = new Compilation.library(paths, libPath, null, | |
51 <String>['--preserve-comments']); | |
blois
2013/01/24 02:28:24
Array can probably just be: ['--preserve-comments'
Andrei Mouravski
2013/01/24 19:28:51
Done.
| |
52 | |
53 var convertedJson = _generateJsonFromLibraries(compilation); | |
54 | |
55 anyErrors = anyErrors || _exportJsonToFile(jsonPath, convertedJson); | |
blois
2013/01/24 02:28:24
where is anyErrors set before here? How are compil
Johnni Winther
2013/01/24 09:10:09
Currently, compilation error not reported. Shortly
Andrei Mouravski
2013/01/24 19:28:51
TODO added.
Andrei Mouravski
2013/01/24 19:28:51
TODO added.
| |
56 | |
57 return anyErrors; | |
58 } | |
59 | |
60 bool _exportJsonToFile(Path jsonPath, Map convertedJson) { | |
blois
2013/01/24 02:28:24
Nit- order of params.
exportJsonToFile should prob
Andrei Mouravski
2013/01/24 19:28:51
Done.
| |
61 final jsonFile = new File.fromPath(jsonPath); | |
62 var writeJson = prettySerialize(convertedJson); | |
63 | |
64 var outputStream = jsonFile.openOutputStream(); | |
65 outputStream.writeString(writeJson); | |
66 | |
67 return false; | |
68 } | |
69 | |
70 Map _generateJsonFromLibraries(Compilation compilation) { | |
71 var convertedJson = {}; | |
72 | |
73 // Sort the libraries by name (not key). | |
74 var sortedLibraries = new List<LibraryMirror>.from( | |
75 compilation.mirrors.libraries.values.where( | |
76 (e) => HTML_LIBRARY_NAMES.indexOf(e.uri.toString()) >= 0)) | |
77 ..sort((x, y) => | |
78 x.uri.toString().toUpperCase().compareTo( | |
79 y.uri.toString().toUpperCase())); | |
80 | |
81 for (LibraryMirror libMirror in sortedLibraries) { | |
82 print('Extracting documentation from ${libMirror.displayName}.'); | |
83 | |
84 var libraryJson = {}; | |
85 var sortedClasses = _sortAndFilterMirrors( | |
86 libMirror.classes.values.toList()); | |
87 | |
88 for (ClassMirror classMirror in sortedClasses) { | |
89 var classJson = {}; | |
90 var sortedMembers = _sortAndFilterMirrors( | |
91 classMirror.members.values.toList()); | |
92 | |
93 var membersJson = {}; | |
94 for (var memberMirror in sortedMembers) { | |
95 var memberDomName = domNames(memberMirror)[0]; | |
96 var memberComment = computeComment(memberMirror); | |
97 if (memberComment != null) { | |
98 membersJson.putIfAbsent(memberDomName, () => memberComment); | |
99 } | |
100 } | |
101 | |
102 if (computeComment(classMirror) != null) { | |
Johnni Winther
2013/01/24 09:10:09
Store the comment in a variable instead of calling
Andrei Mouravski
2013/01/24 19:28:51
Done.
| |
103 classJson.putIfAbsent('comment', () => | |
104 computeComment(classMirror)); | |
105 } | |
106 if (!membersJson.isEmpty) { | |
107 classJson.putIfAbsent('members', () => | |
108 membersJson); | |
109 } | |
110 | |
111 if (!classJson.isEmpty) { | |
112 libraryJson.putIfAbsent(domNames(classMirror)[0], () => | |
113 classJson); | |
114 } | |
115 } | |
116 | |
117 if (!libraryJson.isEmpty) { | |
118 convertedJson.putIfAbsent(libMirror.displayName, () => | |
119 libraryJson); | |
120 } | |
121 } | |
122 | |
123 return convertedJson; | |
124 } | |
125 | |
126 List<DeclarationMirror> _sortAndFilterMirrors(List<DeclarationMirror> mirrors) { | |
127 return mirrors.where((DeclarationMirror c) => | |
blois
2013/01/24 02:28:24
kudos for getting this all into one line. but was
Andrei Mouravski
2013/01/24 19:28:51
Done.
| |
128 !domNames(c).isEmpty && | |
129 !c.displayName.startsWith('_') && | |
130 (findMetadata(c.metadata, 'DocsEditable') != null)) | |
131 .toList() | |
132 ..sort((x, y) => | |
133 domNames(x)[0].toUpperCase().compareTo( | |
134 domNames(y)[0].toUpperCase())); | |
135 } | |
136 | |
137 /// Given the class mirror, returns the names found or an empty list. | |
138 List<String> domNames(DeclarationMirror mirror) { | |
139 var domNameMetadata = findMetadata(mirror.metadata, 'DomName'); | |
140 | |
141 if (domNameMetadata != null) { | |
142 var domNames = <String>[]; | |
143 var tags = deprecatedFutureValue(domNameMetadata.getField('name')); | |
144 for (var s in tags.reflectee.split(',')) { | |
145 domNames.add(s.trim()); | |
146 } | |
147 | |
148 if (domNames.length == 1 && domNames[0] == 'none') return <String>[]; | |
149 return domNames; | |
150 } else { | |
151 return <String>[]; | |
152 } | |
153 } | |
OLD | NEW |