Chromium Code Reviews| Index: pkg/docgen/lib/docgen.dart |
| diff --git a/pkg/docgen/bin/docgen.dart b/pkg/docgen/lib/docgen.dart |
| similarity index 62% |
| copy from pkg/docgen/bin/docgen.dart |
| copy to pkg/docgen/lib/docgen.dart |
| index 308df289271e71bab2ec5e94b542b2fccffffff0..e7b9ebf1d3dbd840ddc09a4dd305b9ec6534aaff 100644 |
| --- a/pkg/docgen/bin/docgen.dart |
| +++ b/pkg/docgen/lib/docgen.dart |
| @@ -18,36 +18,71 @@ library docgen; |
| import 'dart:io'; |
| import 'dart:json'; |
| import 'dart:async'; |
| -import '../lib/dart2yaml.dart'; |
| -import '../lib/src/dart2js_mirrors.dart'; |
| import 'package:markdown/markdown.dart' as markdown; |
| -import '../../args/lib/args.dart'; |
| -import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; |
| -import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dart'; |
| +import 'package:args/args.dart'; |
| +import 'dart2yaml.dart'; |
| +import 'package:compiler_unsupported/implementation/mirrors/dart2js_mirror.dart'; |
| +import 'package:compiler_unsupported/implementation/mirrors/mirrors.dart'; |
| +import 'package:compiler_unsupported/implementation/mirrors/mirrors_util.dart'; |
| + |
| +/// Unique ID, will get incremented everytime an ID is requested. |
| +int _uid = 0; |
| + |
| +int getID() => _uid++; |
| /** |
| - * Entry function to create YAML documentation from Dart files. |
| + * Returns a ArgParser with all the flags and options created. |
| */ |
| -void main() { |
| - // TODO(tmandel): Use args library once flags are clear. |
| - Options opts = new Options(); |
| - Docgen docgen = new Docgen(); |
| - |
| - if (opts.arguments.length > 0) { |
| - List<Path> libraries = [new Path(opts.arguments[0])]; |
| - Path sdkDirectory = new Path("../../../sdk/"); |
| - var workingMirrors = analyze(libraries, sdkDirectory, |
| - options: ['--preserve-comments', '--categories=Client,Server']); |
| - workingMirrors.then( (MirrorSystem mirrorSystem) { |
| - var mirrors = mirrorSystem.libraries.values; |
| - if (mirrors.isEmpty) { |
| - print("no LibraryMirrors"); |
| - } else { |
| - docgen.libraries = mirrors; |
| - docgen.documentLibraries(); |
| - } |
| - }); |
| +ArgParser initArgParser() { |
| + var parser = new ArgParser(); |
| + parser.addFlag("help", abbr: "h", |
| + help: "Prints help and usage information", |
| + negatable: false, |
| + callback: (help) { |
| + if (help) print(parser.getUsage()); |
| + }); |
| + parser.addFlag("yaml", abbr: "y", |
| + help: "Outputs to YAML", |
| + defaultsTo: true, negatable: true); |
| + parser.addFlag("json", abbr: "j", |
| + help: "Outputs to JSON", |
| + defaultsTo: false, negatable: true); |
| + parser.addFlag("hide-private", |
| + help: "Hides private declarations" , |
| + defaultsTo: false, negatable: false); |
| + parser.addFlag("sdk", |
| + help: "Flag to parse SDK Library files", |
| + defaultsTo: true, negatable: true); |
| + |
| + return parser; |
| +} |
| + |
| +List<Path> listLibraries(List<String> args) { |
| + if (args.length != 1) { |
| + throw new UnsupportedError("Usage: dart docgen.dart [OPTIONS] [FILE/DIR]"); |
| } |
| + var libraries = new List<Path>(); |
| + var type = FileSystemEntity.typeSync(args[0]); |
| + |
| + if (type == FileSystemEntityType.LINK) { |
| + throw new UnsupportedError("""File should not be a link. |
| +Usage: dart docgen.dart [OPTIONS] [FILE/DIR]"""); |
|
Emily Fortuna
2013/06/19 17:23:34
why not support links?
janicejl
2013/06/19 18:53:38
Done.
|
| + } else if (type == FileSystemEntityType.NOT_FOUND) { |
| + throw new UnsupportedError("""File does not exist. |
| +Usage: dart docgen.dart [OPTIONS] [FILE/DIR]"""); |
| + } else if (type == FileSystemEntityType.FILE) { |
| + libraries.add(new Path(args[0])); |
| + } else if (type == FileSystemEntityType.DIRECTORY) { |
| + new Directory(args[0]).listSync(recursive: true, |
| + followLinks: true).forEach((file) { |
| + if (new Path(file.path).extension == "dart") { |
| + if (!file.path.contains("/packages/")) { |
| + libraries.add(new Path(file.path)); |
| + } |
| + } |
| + }); |
| + } |
| + return libraries; |
| } |
| /** |
| @@ -59,7 +94,9 @@ class Docgen { |
| List<LibraryMirror> _libraries; |
| /// Saves list of libraries for Docgen object. |
| - void set libraries(value) => _libraries = value; |
| + void set libraries(value) { |
| + _libraries = value; |
| + } |
| /// Current library being documented to be used for comment links. |
| LibraryMirror _currentLibrary; |
| @@ -70,36 +107,53 @@ class Docgen { |
| /// Current member being documented to be used for comment links. |
| MemberMirror _currentMember; |
| - /// Should the output file type be JSON? |
| - // TODO(tmandel): Add flag to allow for output to JSON. |
| - bool outputToJson = false; |
| - |
| /// Resolves reference links |
| markdown.Resolver linkResolver; |
| /** |
| * Docgen constructor initializes the link resolver for markdown parsing. |
| */ |
| - Docgen() { |
| + Docgen({Path sdkRoot, ArgResults argResults}) { |
|
Emily Fortuna
2013/06/19 17:23:34
Docgen({this.sdkRoot, ArgResults argResults}) { ..
|
| + this.sdkRoot = sdkRoot; |
| + outputToYaml = argResults["yaml"]; |
| + outputToJson = argResults["json"]; |
| + hidePrivate = argResults["hide-private"]; |
| + sdk = argResults["sdk"]; |
| + |
| this.linkResolver = (name) => |
| fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| } |
| + /// Path to the SDK Libraries. |
| + Path sdkRoot; |
| + |
| + /// Should the output file type be YAML? |
| + bool outputToYaml; |
| + /// Should the output file type be JSON? |
| + bool outputToJson; |
| + /// Should the output file hide private declarations? |
| + bool hidePrivate; |
| + /// Should the output include SDK libraries? |
| + bool sdk; |
| + |
| /** |
| * Creates documentation for filtered libraries. |
| */ |
| void documentLibraries() { |
| - //TODO(tmandel): Filter libraries and determine output type using flags. |
| _libraries.forEach((library) { |
| - _currentLibrary = library; |
| - var result = new Library(library.qualifiedName, _getComment(library), |
| - _getVariables(library.variables), _getMethods(library.functions), |
| - _getClasses(library.classes)); |
| - if (outputToJson) { |
| - _writeToFile(stringify(result.toMap()), "${result.name}.json"); |
| - } else { |
| - _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); |
| - } |
| + // Files belong to the SDK have a uri that begins with "dart:". |
| + if (sdk || !library.uri.toString().startsWith("dart:")) { |
| + _currentLibrary = library; |
| + var result = new Library(library.qualifiedName, _getComment(library), |
| + _getVariables(library.variables), _getMethods(library.functions), |
| + _getClasses(library.classes), getID()); |
| + if (outputToJson) { |
| + _writeToFile(stringify(result.toMap()), "${result.name}.json"); |
| + } |
| + if (outputToYaml) { |
|
Emily Fortuna
2013/06/19 17:23:34
seems like you might want to have a default output
janicejl
2013/06/19 18:53:38
Defaults to yaml through setting up the command li
|
| + _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); |
| + } |
| + } |
| }); |
| } |
| @@ -121,8 +175,9 @@ class Docgen { |
| } |
| } |
| }); |
| - return commentText == null ? "" : |
| + commentText = commentText == null ? "" : |
| markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); |
|
Emily Fortuna
2013/06/19 17:23:34
continuation of previous line should be indented t
janicejl
2013/06/19 18:53:38
I have changed it to replace all \n to "". This is
|
| + return commentText.replaceAll("\n", "<br/>"); |
| } |
| /** |
| @@ -141,9 +196,12 @@ class Docgen { |
| Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| - _currentMember = mirror; |
| - data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| - mirror.isStatic, mirror.type.toString(), _getComment(mirror)); |
| + if (!hidePrivate || !mirror.isPrivate) { |
| + _currentMember = mirror; |
| + data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| + mirror.isStatic, mirror.type.toString(), _getComment(mirror), |
| + getID()); |
| + } |
| }); |
| return data; |
| } |
| @@ -154,11 +212,13 @@ class Docgen { |
| Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| - _currentMember = mirror; |
| - data[mirrorName] = new Method(mirrorName, mirror.isSetter, |
| - mirror.isGetter, mirror.isConstructor, mirror.isOperator, |
| - mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), |
| - _getParameters(mirror.parameters)); |
| + if (!hidePrivate || !mirror.isPrivate) { |
| + _currentMember = mirror; |
| + data[mirrorName] = new Method(mirrorName, mirror.isSetter, |
| + mirror.isGetter, mirror.isConstructor, mirror.isOperator, |
| + mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), |
| + _getParameters(mirror.parameters), getID()); |
| + } |
| }); |
| return data; |
| } |
| @@ -169,16 +229,17 @@ class Docgen { |
| Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| - _currentClass = mirror; |
| - var superclass; |
| - if (mirror.superclass != null) { |
| - superclass = mirror.superclass.qualifiedName; |
| + if (!hidePrivate || !mirror.isPrivate) { |
| + _currentClass = mirror; |
| + var superclass = (mirror.superclass != null) ? |
| + mirror.superclass.qualifiedName : ""; |
| + var interfaces = |
| + mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| + data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
| + mirror.isTypedef, _getComment(mirror), interfaces.toList(), |
| + _getVariables(mirror.variables), _getMethods(mirror.methods), |
| + getID()); |
| } |
| - var interfaces = |
| - mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| - data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
| - mirror.isTypedef, _getComment(mirror), interfaces, |
| - _getVariables(mirror.variables), _getMethods(mirror.methods)); |
| }); |
| return data; |
| } |
| @@ -192,7 +253,7 @@ class Docgen { |
| _currentMember = mirror; |
| data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
| - mirror.type.toString(), mirror.defaultValue); |
| + mirror.type.toString(), mirror.defaultValue, getID()); |
| }); |
| return data; |
| } |
| @@ -214,6 +275,9 @@ Map recurseMap(Map inputMap) { |
| */ |
| class Library { |
| + /// Unique ID number for resolving links. |
| + int id; |
| + |
| /// Documentation comment with converted markdown. |
| String comment; |
| @@ -229,11 +293,12 @@ class Library { |
| String name; |
| Library(this.name, this.comment, this.variables, |
| - this.functions, this.classes); |
| + this.functions, this.classes, this.id); |
| /// Generates a map describing the [Library] object. |
| Map toMap() { |
| var libraryMap = {}; |
| + libraryMap["id"] = id; |
| libraryMap["name"] = name; |
| libraryMap["comment"] = comment; |
| libraryMap["variables"] = recurseMap(variables); |
| @@ -249,6 +314,9 @@ class Library { |
| // TODO(tmandel): Figure out how to do typedefs (what is needed) |
| class Class { |
| + /// Unique ID number for resolving links. |
| + int id; |
| + |
| /// Documentation comment with converted markdown. |
| String comment; |
| @@ -267,11 +335,12 @@ class Class { |
| bool isTypedef; |
| Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
| - this.comment, this.interfaces, this.variables, this.methods); |
| + this.comment, this.interfaces, this.variables, this.methods, this.id); |
| /// Generates a map describing the [Class] object. |
| Map toMap() { |
| var classMap = {}; |
| + classMap["id"] = id; |
| classMap["name"] = name; |
| classMap["comment"] = comment; |
| classMap["superclass"] = superclass; |
| @@ -289,6 +358,9 @@ class Class { |
| */ |
| class Variable { |
| + /// Unique ID number for resolving links. |
| + int id; |
| + |
| /// Documentation comment with converted markdown. |
| String comment; |
| @@ -297,11 +369,13 @@ class Variable { |
| bool isStatic; |
| String type; |
| - Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); |
| + Variable(this.name, this.isFinal, this.isStatic, this.type, |
| + this.comment, this.id); |
| /// Generates a map describing the [Variable] object. |
| Map toMap() { |
| var variableMap = {}; |
| + variableMap["id"] = id; |
| variableMap["name"] = name; |
| variableMap["comment"] = comment; |
| variableMap["final"] = isFinal.toString(); |
| @@ -316,6 +390,9 @@ class Variable { |
| */ |
| class Method { |
| + /// Unique ID number for resolving links. |
| + int id; |
| + |
| /// Documentation comment with converted markdown. |
| String comment; |
| @@ -332,11 +409,12 @@ class Method { |
| Method(this.name, this.isSetter, this.isGetter, this.isConstructor, |
| this.isOperator, this.isStatic, this.returnType, this.comment, |
| - this.parameters); |
| + this.parameters, this.id); |
| /// Generates a map describing the [Method] object. |
| Map toMap() { |
| var methodMap = {}; |
| + methodMap["id"] = id; |
| methodMap["name"] = name; |
| methodMap["comment"] = comment; |
| methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : |
| @@ -353,6 +431,9 @@ class Method { |
| */ |
| class Parameter { |
| + /// Unique ID number for resolving links. |
| + int id; |
| + |
| String name; |
| bool isOptional; |
| bool isNamed; |
| @@ -361,11 +442,12 @@ class Parameter { |
| String defaultValue; |
| Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
| - this.type, this.defaultValue); |
| + this.type, this.defaultValue, this.id); |
| /// Generates a map describing the [Parameter] object. |
| Map toMap() { |
| var parameterMap = {}; |
| + parameterMap["id"] = id; |
| parameterMap["name"] = name; |
| parameterMap["optional"] = isOptional.toString(); |
| parameterMap["named"] = isNamed.toString(); |
| @@ -385,7 +467,7 @@ void _writeToFile(String text, String filename) { |
| dir.createSync(); |
| } |
| File file = new File('docs/$filename'); |
| - if (!file.exists()) { |
| + if (!file.existsSync()) { |
| file.createSync(); |
| } |
| file.openSync(); |