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

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

Issue 17893008: Refactoring docgen to make it more unit testable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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 | no next file » | 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 /** 5 /**
6 * **docgen** is a tool for creating machine readable representations of Dart 6 * **docgen** is a tool for creating machine readable representations of Dart
7 * code metadata, including: classes, members, comments and annotations. 7 * code metadata, including: classes, members, comments and annotations.
8 * 8 *
9 * docgen is run on a `.dart` file or a directory containing `.dart` files. 9 * docgen is run on a `.dart` file or a directory containing `.dart` files.
10 * 10 *
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 /// State for whether imported SDK libraries should also be outputted. 68 /// State for whether imported SDK libraries should also be outputted.
69 bool includeSdk; 69 bool includeSdk;
70 /// State for whether all SDK libraries should be outputted. 70 /// State for whether all SDK libraries should be outputted.
71 bool parseSdk; 71 bool parseSdk;
72 72
73 /** 73 /**
74 * Docgen constructor initializes the link resolver for markdown parsing. 74 * Docgen constructor initializes the link resolver for markdown parsing.
75 * Also initializes the command line arguments. 75 * Also initializes the command line arguments.
76 */ 76 */
77 Docgen(ArgResults argResults) { 77 Docgen(ArgResults argResults) {
78 setCommandLineArguments(argResults);
79
80 this.linkResolver = (name) =>
81 fixReference(name, _currentLibrary, _currentClass, _currentMember);
82
83 getMirrorSystem(argResults.rest).then(analyzeMirrors);
84 }
85
86 void setCommandLineArguments(ArgResults argResults) {
78 outputToYaml = argResults['yaml'] || argResults['output-format'] == 'yaml'; 87 outputToYaml = argResults['yaml'] || argResults['output-format'] == 'yaml';
79 outputToJson = argResults['json'] || argResults['output-format'] == 'json'; 88 outputToJson = argResults['json'] || argResults['output-format'] == 'json';
80 if (outputToYaml && outputToJson) { 89 if (outputToYaml && outputToJson) {
81 throw new ArgumentError('Cannot have contradictory output flags.'); 90 throw new ArgumentError('Cannot have contradictory output flags.');
82 } 91 }
83 outputToYaml = outputToYaml || !outputToJson; 92 outputToYaml = outputToYaml || !outputToJson;
84 includePrivate = argResults['include-private']; 93 includePrivate = argResults['include-private'];
85 parseSdk = argResults['parse-sdk']; 94 parseSdk = argResults['parse-sdk'];
86 includeSdk = parseSdk || argResults['include-sdk']; 95 includeSdk = parseSdk || argResults['include-sdk'];
87
88 this.linkResolver = (name) =>
89 fixReference(name, _currentLibrary, _currentClass, _currentMember);
90
91 analyze(argResults.rest);
92 } 96 }
93 97
94 List<String> listLibraries(List<String> args) { 98 List<String> listLibraries(List<String> args) {
95 // TODO(janicejl): At the moment, only have support to have either one file, 99 // TODO(janicejl): At the moment, only have support to have either one file,
96 // or one directory. This is because there can only be one package directory 100 // or one directory. This is because there can only be one package directory
97 // since only one docgen is created per run. 101 // since only one docgen is created per run.
98 if (args.length != 1) throw new UnsupportedError(usage); 102 if (args.length != 1) throw new UnsupportedError(usage);
99 var libraries = new List<String>(); 103 var libraries = new List<String>();
100 var type = FileSystemEntity.typeSync(args[0]); 104 var type = FileSystemEntity.typeSync(args[0]);
101 105
(...skipping 21 matching lines...) Expand all
123 var sdk = new List<String>(); 127 var sdk = new List<String>();
124 LIBRARIES.forEach((String name, LibraryInfo info) { 128 LIBRARIES.forEach((String name, LibraryInfo info) {
125 if (info.documented) { 129 if (info.documented) {
126 sdk.add('dart:$name'); 130 sdk.add('dart:$name');
127 logger.info('Add to SDK: ${sdk.last}'); 131 logger.info('Add to SDK: ${sdk.last}');
128 } 132 }
129 }); 133 });
130 return sdk; 134 return sdk;
131 } 135 }
132 136
137 void analyzeMirrors(MirrorSystem mirrorSystem) {
Emily Fortuna 2013/06/26 19:16:43 consider renaming to something like "setLibraries"
138 if (mirrorSystem.libraries.values.isEmpty) {
139 throw new UnsupportedError('No Library Mirrors.');
140 }
141 this.libraries = mirrorSystem.libraries.values;
142 documentLibraries();
143 }
144
133 /** 145 /**
134 * Analyzes set of libraries by getting a mirror system and triggers the 146 * Analyzes set of libraries by getting a mirror system and triggers the
135 * documentation of the libraries. 147 * documentation of the libraries.
136 */ 148 */
137 void analyze(List<String> args) { 149 Future<MirrorSystem> getMirrorSystem(List<String> args) {
138 var libraries = !parseSdk ? listLibraries(args) : listSdk(); 150 var libraries = !parseSdk ? listLibraries(args) : listSdk();
139 if (libraries.isEmpty) throw new StateError('No Libraries.'); 151 if (libraries.isEmpty) throw new StateError('No Libraries.');
140 // DART_SDK should be set to the root of the SDK library. 152 // DART_SDK should be set to the root of the SDK library.
141 var sdkRoot = Platform.environment['DART_SDK']; 153 var sdkRoot = Platform.environment['DART_SDK'];
142 if (sdkRoot != null) { 154 if (sdkRoot != null) {
143 logger.info('Using DART_SDK to find SDK at $sdkRoot'); 155 logger.info('Using DART_SDK to find SDK at $sdkRoot');
144 } else { 156 } else {
145 // If DART_SDK is not defined in the environment, 157 // If DART_SDK is not defined in the environment,
146 // assuming the dart executable is from the Dart SDK folder inside bin. 158 // assuming the dart executable is from the Dart SDK folder inside bin.
147 sdkRoot = path.dirname(path.dirname(new Options().executable)); 159 sdkRoot = path.dirname(path.dirname(new Options().executable));
148 logger.info('SDK Root: ${sdkRoot}'); 160 logger.info('SDK Root: ${sdkRoot}');
149 } 161 }
150 162
151 getMirrorSystem(libraries, sdkRoot, packageRoot: packageDir) 163 return getMirrorSystemHelper(libraries, sdkRoot, packageRoot: packageDir);
152 .then((MirrorSystem mirrorSystem) {
153 if (mirrorSystem.libraries.values.isEmpty) {
154 throw new UnsupportedError('No Library Mirrors.');
155 }
156 this.libraries = mirrorSystem.libraries.values;
157 documentLibraries();
158 });
159 } 164 }
160 165
161 /** 166 /**
162 * Analyzes set of libraries and provides a mirror system which can be used 167 * Analyzes set of libraries and provides a mirror system which can be used
163 * for static inspection of the source code. 168 * for static inspection of the source code.
164 */ 169 */
165 Future<MirrorSystem> getMirrorSystem(List<String> libraries, 170 Future<MirrorSystem> getMirrorSystemHelper(List<String> libraries,
166 String libraryRoot, {String packageRoot}) { 171 String libraryRoot, {String packageRoot}) {
167 SourceFileProvider provider = new SourceFileProvider(); 172 SourceFileProvider provider = new SourceFileProvider();
168 api.DiagnosticHandler diagnosticHandler = 173 api.DiagnosticHandler diagnosticHandler =
169 new FormattingDiagnosticHandler(provider).diagnosticHandler; 174 new FormattingDiagnosticHandler(provider).diagnosticHandler;
170 Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); 175 Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot'));
171 Uri packageUri = null; 176 Uri packageUri = null;
172 if (packageRoot != null) { 177 if (packageRoot != null) {
173 packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); 178 packageUri = currentDirectory.resolve(appendSlash('$packageRoot'));
174 } 179 }
175 List<Uri> librariesUri = <Uri>[]; 180 List<Uri> librariesUri = <Uri>[];
176 libraries.forEach((library) { 181 libraries.forEach((library) {
177 librariesUri.add(currentDirectory.resolve(library)); 182 librariesUri.add(currentDirectory.resolve(library));
178 }); 183 });
179 return dart2js.analyze(librariesUri, libraryUri, packageUri, 184 return dart2js.analyze(librariesUri, libraryUri, packageUri,
180 provider.readStringFromUri, diagnosticHandler, 185 provider.readStringFromUri, diagnosticHandler,
181 ['--preserve-comments', '--categories=Client,Server']); 186 ['--preserve-comments', '--categories=Client,Server']);
182 } 187 }
183 188
184 /** 189 /**
185 * Creates documentation for filtered libraries. 190 * Creates documentation for filtered libraries.
186 */ 191 */
187 void documentLibraries() { 192 void documentLibraries() {
188 _libraries.forEach((library) { 193 _libraries.forEach((lib) {
189 // Files belonging to the SDK have a uri that begins with 'dart:'. 194 // Files belonging to the SDK have a uri that begins with 'dart:'.
190 if (includeSdk || !library.uri.toString().startsWith('dart:')) { 195 if (includeSdk || !lib.uri.toString().startsWith('dart:')) {
191 _currentLibrary = library; 196 var library = generateLibrary(lib);
192 var result = new Library(library.qualifiedName, _getComment(library), 197 outputLibrary(library);
193 _getVariables(library.variables), _getMethods(library.functions), 198 }
194 _getClasses(library.classes));
195 if (outputToJson) {
196 _writeToFile(stringify(result.toMap()), '${result.name}.json');
197 }
198 if (outputToYaml) {
199 _writeToFile(getYamlString(result.toMap()), '${result.name}.yaml');
200 }
201 }
202 }); 199 });
203 // Outputs a text file with a list of files available after creating all 200 // Outputs a text file with a list of files available after creating all
204 // the libraries. This will help the viewer know what files are available 201 // the libraries. This will help the viewer know what files are available
205 // to read in. 202 // to read in.
206 _writeToFile(listDir("docs").join('\n'), 'library_list.txt'); 203 _writeToFile(listDir("docs").join('\n'), 'library_list.txt');
207 } 204 }
205
206 Library generateLibrary(dart2js.Dart2JsLibraryMirror library) {
207 _currentLibrary = library;
208 var result = new Library(library.qualifiedName, _getComment(library),
209 _getVariables(library.variables), _getMethods(library.functions),
210 _getClasses(library.classes));
211 logger.fine('Generated library for ${result.name}');
212 return result;
213 }
214
215 void outputLibrary(Library result) {
216 if (outputToJson) {
217 _writeToFile(stringify(result.toMap()), '${result.name}.json');
218 }
219 if (outputToYaml) {
220 _writeToFile(getYamlString(result.toMap()), '${result.name}.yaml');
221 }
222 }
208 223
209 /// Saves list of libraries for Docgen object. 224 /// Saves list of libraries for Docgen object.
210 void set libraries(value){ 225 void set libraries(value){
211 _libraries = value; 226 _libraries = value;
212 } 227 }
213 228
214 /** 229 /**
215 * Returns any documentation comments associated with a mirror with 230 * Returns any documentation comments associated with a mirror with
216 * simple markdown converted to html. 231 * simple markdown converted to html.
217 */ 232 */
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 if (!dir.existsSync()) { 525 if (!dir.existsSync()) {
511 dir.createSync(); 526 dir.createSync();
512 } 527 }
513 File file = new File('docs/$filename'); 528 File file = new File('docs/$filename');
514 if (!file.existsSync()) { 529 if (!file.existsSync()) {
515 file.createSync(); 530 file.createSync();
516 } 531 }
517 file.openSync(); 532 file.openSync();
518 file.writeAsString(text); 533 file.writeAsString(text);
519 } 534 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698