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

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

Issue 19309003: "Reverting 25035" (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 | « pkg/docgen/lib/dart2yaml.dart ('k') | 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 /** 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 *
11 * $ dart docgen.dart [OPTIONS] [FILE/DIR] 11 * $ dart docgen.dart [OPTIONS] [FILE/DIR]
12 * 12 *
13 * This creates files called `docs/<library_name>.yaml` in your current 13 * This creates files called `docs/<library_name>.yaml` in your current
14 * working directory. 14 * working directory.
15 */ 15 */
16 library docgen; 16 library docgen;
17 17
18 import 'dart:io'; 18 import 'dart:io';
19 import 'dart:json'; 19 import 'dart:json';
20 import 'dart:async'; 20 import 'dart:async';
21 21
22 import 'package:logging/logging.dart'; 22 import 'package:logging/logging.dart';
23 import 'package:markdown/markdown.dart' as markdown; 23 import 'package:markdown/markdown.dart' as markdown;
24 import 'package:pathos/path.dart' as path; 24 import 'package:path/path.dart' as path;
25 25
26 import 'dart2yaml.dart'; 26 import 'dart2yaml.dart';
27 import 'src/io.dart'; 27 import 'src/io.dart';
28 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api; 28 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api;
29 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; 29 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart';
30 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro r.dart' 30 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro r.dart'
31 as dart2js; 31 as dart2js;
32 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; 32 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ;
33 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart'; 33 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart';
34 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider. dart'; 34 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider. dart';
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 CommentInstanceMirror comment = metadata; 259 CommentInstanceMirror comment = metadata;
260 if (comment.isDocComment) { 260 if (comment.isDocComment) {
261 if (commentText == null) { 261 if (commentText == null) {
262 commentText = comment.trimmedText; 262 commentText = comment.trimmedText;
263 } else { 263 } else {
264 commentText = '$commentText ${comment.trimmedText}'; 264 commentText = '$commentText ${comment.trimmedText}';
265 } 265 }
266 } 266 }
267 } 267 }
268 }); 268 });
269 269 commentText = commentText == null ? '' :
270 commentText = commentText == null ? '' : 270 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver)
271 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); 271 .replaceAll('\n', ' ');
272 return commentText; 272 return commentText;
273 } 273 }
274 274
275 /** 275 /**
276 * Converts all [foo] references in comments to <a>libraryName.foo</a>. 276 * Converts all [foo] references in comments to <a>libraryName.foo</a>.
277 */ 277 */
278 markdown.Node fixReference(String name, LibraryMirror currentLibrary, 278 markdown.Node fixReference(String name, LibraryMirror currentLibrary,
279 ClassMirror currentClass, MemberMirror currentMember) { 279 ClassMirror currentClass, MemberMirror currentMember) {
280 var reference; 280 var reference;
281 var memberScope = currentMember == null ? 281 var memberScope = currentMember == null ?
(...skipping 11 matching lines...) Expand all
293 * Returns a map of [Variable] objects constructed from inputted mirrors. 293 * Returns a map of [Variable] objects constructed from inputted mirrors.
294 */ 294 */
295 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, 295 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap,
296 bool includePrivate) { 296 bool includePrivate) {
297 var data = {}; 297 var data = {};
298 // TODO(janicejl): When map to map feature is created, replace the below with 298 // TODO(janicejl): When map to map feature is created, replace the below with
299 // a filter. Issue(#9590). 299 // a filter. Issue(#9590).
300 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { 300 mirrorMap.forEach((String mirrorName, VariableMirror mirror) {
301 if (includePrivate || !mirror.isPrivate) { 301 if (includePrivate || !mirror.isPrivate) {
302 _currentMember = mirror; 302 _currentMember = mirror;
303 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, 303 data[mirrorName] = new Variable(mirrorName, mirror.qualifiedName,
304 mirror.isStatic, mirror.type.qualifiedName, _getComment(mirror), 304 mirror.isFinal, mirror.isStatic, mirror.type.qualifiedName,
305 _getAnnotations(mirror)); 305 _getComment(mirror), _getAnnotations(mirror));
306 } 306 }
307 }); 307 });
308 return data; 308 return data;
309 } 309 }
310 310
311 /** 311 /**
312 * Returns a map of [Method] objects constructed from inputted mirrors. 312 * Returns a map of [Method] objects constructed from inputted mirrors.
313 */ 313 */
314 Map<String, Map<String, Method>> _getMethods 314 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap,
315 (Map<String, MethodMirror> mirrorMap, bool includePrivate) { 315 bool includePrivate) {
316 316 var data = {};
317 var setters = {};
318 var getters = {};
319 var constructors = {};
320 var operators = {};
321 var methods = {};
322
323 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { 317 mirrorMap.forEach((String mirrorName, MethodMirror mirror) {
324 if (includePrivate || !mirror.isPrivate) { 318 if (includePrivate || !mirror.isPrivate) {
325 var method = new Method(mirrorName, mirror.isStatic,
326 mirror.returnType.qualifiedName, _getComment(mirror),
327 _getParameters(mirror.parameters), _getAnnotations(mirror));
328 _currentMember = mirror; 319 _currentMember = mirror;
329 if (mirror.isSetter) { 320 data[mirrorName] = new Method(mirrorName, mirror.qualifiedName,
330 setters[mirrorName] = method; 321 mirror.isSetter, mirror.isGetter, mirror.isConstructor,
331 } else if (mirror.isGetter) { 322 mirror.isOperator, mirror.isStatic, mirror.returnType.qualifiedName,
332 getters[mirrorName] = method; 323 _getComment(mirror), _getParameters(mirror.parameters),
333 } else if (mirror.isConstructor) { 324 _getAnnotations(mirror));
334 constructors[mirrorName] = method;
335 } else if (mirror.isOperator) {
336 operators[mirrorName] = method;
337 } else if (mirror.isRegularMethod) {
338 methods[mirrorName] = method;
339 } else {
340 throw new StateError('${mirror.qualifiedName} - no method type match');
341 }
342 } 325 }
343 }); 326 });
344 return {'setters' : setters, 327 return data;
345 'getters' : getters, 328 }
346 'constructors' : constructors,
347 'operators' : operators,
348 'methods' : methods};
349 }
350 329
351 /** 330 /**
352 * Returns a map of [Class] objects constructed from inputted mirrors. 331 * Returns a map of [Class] objects constructed from inputted mirrors.
353 */ 332 */
354 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, 333 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap,
355 bool includePrivate) { 334 bool includePrivate) {
356 var data = {}; 335 var data = {};
357 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { 336 mirrorMap.forEach((String mirrorName, ClassMirror mirror) {
358 if (includePrivate || !mirror.isPrivate) { 337 if (includePrivate || !mirror.isPrivate) {
359 _currentClass = mirror; 338 _currentClass = mirror;
360 var superclass = (mirror.superclass != null) ? 339 var superclass = (mirror.superclass != null) ?
361 mirror.superclass.qualifiedName : ''; 340 mirror.superclass.qualifiedName : '';
362 var interfaces = 341 var interfaces =
363 mirror.superinterfaces.map((interface) => interface.qualifiedName); 342 mirror.superinterfaces.map((interface) => interface.qualifiedName);
364 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, 343 data[mirrorName] = new Class(mirrorName, mirror.qualifiedName,
365 mirror.isTypedef, _getComment(mirror), interfaces.toList(), 344 superclass, mirror.isAbstract, mirror.isTypedef,
345 _getComment(mirror), interfaces.toList(),
366 _getVariables(mirror.variables, includePrivate), 346 _getVariables(mirror.variables, includePrivate),
367 _getMethods(mirror.methods, includePrivate), 347 _getMethods(mirror.methods, includePrivate),
368 _getAnnotations(mirror)); 348 _getAnnotations(mirror));
369 } 349 }
370 }); 350 });
371 return data; 351 return data;
372 } 352 }
373 353
374 /** 354 /**
375 * Returns a map of [Parameter] objects constructed from inputted mirrors. 355 * Returns a map of [Parameter] objects constructed from inputted mirrors.
376 */ 356 */
377 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { 357 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) {
378 var data = {}; 358 var data = {};
379 mirrorList.forEach((ParameterMirror mirror) { 359 mirrorList.forEach((ParameterMirror mirror) {
380 _currentMember = mirror; 360 _currentMember = mirror;
381 data[mirror.simpleName] = new Parameter(mirror.simpleName, 361 data[mirror.simpleName] = new Parameter(mirror.simpleName,
382 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, 362 mirror.qualifiedName, mirror.isOptional, mirror.isNamed,
383 mirror.type.qualifiedName, mirror.defaultValue, 363 mirror.hasDefaultValue, mirror.type.qualifiedName,
384 _getAnnotations(mirror)); 364 mirror.defaultValue, _getAnnotations(mirror));
385 }); 365 });
386 return data; 366 return data;
387 } 367 }
388 368
389 /** 369 /**
390 * Writes text to a file in the 'docs' directory. 370 * Writes text to a file in the 'docs' directory.
391 */ 371 */
392 void _writeToFile(String text, String filename) { 372 void _writeToFile(String text, String filename) {
393 Directory dir = new Directory('docs'); 373 Directory dir = new Directory('docs');
394 if (!dir.existsSync()) { 374 if (!dir.existsSync()) {
395 dir.createSync(); 375 dir.createSync();
396 } 376 }
397 File file = new File('docs/$filename'); 377 File file = new File('docs/$filename');
398 if (!file.existsSync()) { 378 if (!file.existsSync()) {
399 file.createSync(); 379 file.createSync();
400 } 380 }
401 file.openSync(); 381 file.openSync();
402 file.writeAsString(text); 382 file.writeAsString(text);
403 } 383 }
404 384
405 /** 385 /**
406 * Transforms the map by calling toMap on each value in it. 386 * Transforms the map by calling toMap on each value in it.
407 */ 387 */
408 Map recurseMap(Map inputMap) { 388 Map recurseMap(Map inputMap) {
409 var outputMap = {}; 389 var outputMap = {};
410 inputMap.forEach((key, value) { 390 inputMap.forEach((key, value) {
411 if (value is Map) { 391 outputMap[key] = value.toMap();
412 outputMap[key] = recurseMap(value);
413 } else {
414 outputMap[key] = value.toMap();
415 }
416 }); 392 });
417 return outputMap; 393 return outputMap;
418 } 394 }
419 395
420 /** 396 /**
421 * A class containing contents of a Dart library. 397 * A class containing contents of a Dart library.
422 */ 398 */
423 class Library { 399 class Library {
424 400
425 /// Documentation comment with converted markdown. 401 /// Documentation comment with converted markdown.
426 String comment; 402 String comment;
427 403
428 /// Top-level variables in the library. 404 /// Top-level variables in the library.
429 Map<String, Variable> variables; 405 Map<String, Variable> variables;
430 406
431 /// Top-level functions in the library. 407 /// Top-level functions in the library.
432 Map<String, Map<String, Method>> functions; 408 Map<String, Method> functions;
433 409
434 /// Classes defined within the library 410 /// Classes defined within the library
435 Map<String, Class> classes; 411 Map<String, Class> classes;
436 412
437 String name; 413 String name;
438 414
439 Library(this.name, this.comment, this.variables, 415 Library(this.name, this.comment, this.variables,
440 this.functions, this.classes); 416 this.functions, this.classes);
441 417
442 /// Generates a map describing the [Library] object. 418 /// Generates a map describing the [Library] object.
443 Map toMap() { 419 Map toMap() {
(...skipping 16 matching lines...) Expand all
460 /// Documentation comment with converted markdown. 436 /// Documentation comment with converted markdown.
461 String comment; 437 String comment;
462 438
463 /// List of the names of interfaces that this class implements. 439 /// List of the names of interfaces that this class implements.
464 List<String> interfaces; 440 List<String> interfaces;
465 441
466 /// Top-level variables in the class. 442 /// Top-level variables in the class.
467 Map<String, Variable> variables; 443 Map<String, Variable> variables;
468 444
469 /// Methods in the class. 445 /// Methods in the class.
470 Map<String, Map<String, Method>> methods; 446 Map<String, Method> methods;
471 447
472 String name; 448 String name;
449 String qualifiedName;
473 String superclass; 450 String superclass;
474 bool isAbstract; 451 bool isAbstract;
475 bool isTypedef; 452 bool isTypedef;
476 453
477 /// List of the meta annotations on the class. 454 /// List of the meta annotations on the class.
478 List<String> annotations; 455 List<String> annotations;
479 456
480 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, 457 Class(this.name, this.qualifiedName, this.superclass, this.isAbstract,
481 this.comment, this.interfaces, this.variables, this.methods, 458 this.isTypedef, this.comment, this.interfaces, this.variables,
482 this.annotations); 459 this.methods, this.annotations);
483 460
484 /// Generates a map describing the [Class] object. 461 /// Generates a map describing the [Class] object.
485 Map toMap() { 462 Map toMap() {
486 var classMap = {}; 463 var classMap = {};
487 classMap['name'] = name; 464 classMap['name'] = name;
465 classMap['qualifiedname'] = qualifiedName;
488 classMap['comment'] = comment; 466 classMap['comment'] = comment;
489 classMap['superclass'] = superclass; 467 classMap['superclass'] = superclass;
490 classMap['abstract'] = isAbstract.toString(); 468 classMap['abstract'] = isAbstract.toString();
491 classMap['typedef'] = isTypedef.toString(); 469 classMap['typedef'] = isTypedef.toString();
492 classMap['implements'] = new List.from(interfaces); 470 classMap['implements'] = new List.from(interfaces);
493 classMap['variables'] = recurseMap(variables); 471 classMap['variables'] = recurseMap(variables);
494 classMap['methods'] = recurseMap(methods); 472 classMap['methods'] = recurseMap(methods);
495 classMap['annotations'] = new List.from(annotations); 473 classMap['annotations'] = new List.from(annotations);
496 return classMap; 474 return classMap;
497 } 475 }
498 } 476 }
499 477
500 /** 478 /**
501 * A class containing properties of a Dart variable. 479 * A class containing properties of a Dart variable.
502 */ 480 */
503 class Variable { 481 class Variable {
504 482
505 /// Documentation comment with converted markdown. 483 /// Documentation comment with converted markdown.
506 String comment; 484 String comment;
507 485
508 String name; 486 String name;
487 String qualifiedName;
509 bool isFinal; 488 bool isFinal;
510 bool isStatic; 489 bool isStatic;
511 String type; 490 String type;
512 491
513 /// List of the meta annotations on the variable. 492 /// List of the meta annotations on the variable.
514 List<String> annotations; 493 List<String> annotations;
515 494
516 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment, 495 Variable(this.name, this.qualifiedName, this.isFinal, this.isStatic,
517 this.annotations); 496 this.type, this.comment, this.annotations);
518 497
519 /// Generates a map describing the [Variable] object. 498 /// Generates a map describing the [Variable] object.
520 Map toMap() { 499 Map toMap() {
521 var variableMap = {}; 500 var variableMap = {};
522 variableMap['name'] = name; 501 variableMap['name'] = name;
502 variableMap['qualifiedname'] = qualifiedName;
523 variableMap['comment'] = comment; 503 variableMap['comment'] = comment;
524 variableMap['final'] = isFinal.toString(); 504 variableMap['final'] = isFinal.toString();
525 variableMap['static'] = isStatic.toString(); 505 variableMap['static'] = isStatic.toString();
526 variableMap['type'] = type; 506 variableMap['type'] = type;
527 variableMap['annotations'] = new List.from(annotations); 507 variableMap['annotations'] = new List.from(annotations);
528 return variableMap; 508 return variableMap;
529 } 509 }
530 } 510 }
531 511
532 /** 512 /**
533 * A class containing properties of a Dart method. 513 * A class containing properties of a Dart method.
534 */ 514 */
535 class Method { 515 class Method {
536 516
537 /// Documentation comment with converted markdown. 517 /// Documentation comment with converted markdown.
538 String comment; 518 String comment;
539 519
540 /// Parameters for this method. 520 /// Parameters for this method.
541 Map<String, Parameter> parameters; 521 Map<String, Parameter> parameters;
542 522
543 String name; 523 String name;
524 String qualifiedName;
525 bool isSetter;
526 bool isGetter;
527 bool isConstructor;
528 bool isOperator;
544 bool isStatic; 529 bool isStatic;
545 String returnType; 530 String returnType;
546 531
547 /// List of the meta annotations on the method. 532 /// List of the meta annotations on the method.
548 List<String> annotations; 533 List<String> annotations;
549 534
550 Method(this.name, this.isStatic, this.returnType, this.comment, 535 Method(this.name, this.qualifiedName, this.isSetter, this.isGetter,
551 this.parameters, this.annotations); 536 this.isConstructor, this.isOperator, this.isStatic, this.returnType,
552 537 this.comment, this.parameters, this.annotations);
538
553 /// Generates a map describing the [Method] object. 539 /// Generates a map describing the [Method] object.
554 Map toMap() { 540 Map toMap() {
555 var methodMap = {}; 541 var methodMap = {};
556 methodMap['name'] = name; 542 methodMap['name'] = name;
543 methodMap['qualifiedname'] = qualifiedName;
557 methodMap['comment'] = comment; 544 methodMap['comment'] = comment;
545 methodMap['type'] = isSetter ? 'setter' : isGetter ? 'getter' :
546 isOperator ? 'operator' : isConstructor ? 'constructor' : 'method';
558 methodMap['static'] = isStatic.toString(); 547 methodMap['static'] = isStatic.toString();
559 methodMap['return'] = returnType; 548 methodMap['return'] = returnType;
560 methodMap['parameters'] = recurseMap(parameters); 549 methodMap['parameters'] = recurseMap(parameters);
561 methodMap['annotations'] = new List.from(annotations); 550 methodMap['annotations'] = new List.from(annotations);
562 return methodMap; 551 return methodMap;
563 } 552 }
564 } 553 }
565 554
566 /** 555 /**
567 * A class containing properties of a Dart method/function parameter. 556 * A class containing properties of a Dart method/function parameter.
568 */ 557 */
569 class Parameter { 558 class Parameter {
570 559
571 String name; 560 String name;
561 String qualifiedName;
572 bool isOptional; 562 bool isOptional;
573 bool isNamed; 563 bool isNamed;
574 bool hasDefaultValue; 564 bool hasDefaultValue;
575 String type; 565 String type;
576 String defaultValue; 566 String defaultValue;
577 567
578 /// List of the meta annotations on the parameter. 568 /// List of the meta annotations on the parameter.
579 List<String> annotations; 569 List<String> annotations;
580 570
581 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, 571 Parameter(this.name, this.qualifiedName, this.isOptional, this.isNamed,
582 this.type, this.defaultValue, this.annotations); 572 this.hasDefaultValue, this.type, this.defaultValue, this.annotations);
583 573
584 /// Generates a map describing the [Parameter] object. 574 /// Generates a map describing the [Parameter] object.
585 Map toMap() { 575 Map toMap() {
586 var parameterMap = {}; 576 var parameterMap = {};
587 parameterMap['name'] = name; 577 parameterMap['name'] = name;
578 parameterMap['qualifiedname'] = qualifiedName;
588 parameterMap['optional'] = isOptional.toString(); 579 parameterMap['optional'] = isOptional.toString();
589 parameterMap['named'] = isNamed.toString(); 580 parameterMap['named'] = isNamed.toString();
590 parameterMap['default'] = hasDefaultValue.toString(); 581 parameterMap['default'] = hasDefaultValue.toString();
591 parameterMap['type'] = type; 582 parameterMap['type'] = type;
592 parameterMap['value'] = defaultValue; 583 parameterMap['value'] = defaultValue;
593 parameterMap['annotations'] = new List.from(annotations); 584 parameterMap['annotations'] = new List.from(annotations);
594 return parameterMap; 585 return parameterMap;
595 } 586 }
596 } 587 }
OLDNEW
« no previous file with comments | « pkg/docgen/lib/dart2yaml.dart ('k') | pkg/docgen/lib/src/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698