Chromium Code Reviews| Index: pkg/docgen/lib/docgen.dart |
| diff --git a/pkg/docgen/lib/docgen.dart b/pkg/docgen/lib/docgen.dart |
| index 5159c05b421e89a71b489771e52f4cb81bb6a850..ca87cfca063247c4d0dd747a2f576921b0dc9f71 100644 |
| --- a/pkg/docgen/lib/docgen.dart |
| +++ b/pkg/docgen/lib/docgen.dart |
| @@ -19,7 +19,6 @@ import 'dart:io'; |
| import 'dart:json'; |
| import 'dart:async'; |
| -import 'package:args/args.dart'; |
| import 'package:logging/logging.dart'; |
| import 'package:markdown/markdown.dart' as markdown; |
| import 'package:pathos/path.dart' as path; |
| @@ -51,49 +50,37 @@ MemberMirror _currentMember; |
| /// Resolves reference links in doc comments. |
| markdown.Resolver linkResolver; |
| -/// Package directory of directory being analyzed. |
| -String packageDir; |
| - |
| -bool outputToYaml; |
| -bool outputToJson; |
| -bool includePrivate; |
| -/// State for whether imported SDK libraries should also be outputted. |
| -bool includeSdk; |
| -/// State for whether all SDK libraries should be outputted. |
| -bool parseSdk; |
| - |
| /** |
| * Docgen constructor initializes the link resolver for markdown parsing. |
| * Also initializes the command line arguments. |
| + * |
| + * [packageRoot] is the packages directory of the directory being analyzed. |
| + * If [includeSdk] is 'true', then any SDK libraries explicitly imported will |
| + * also be documented. |
| + * If [parseSdk] is 'true', then all Dart SDK libraries will be documented. |
| + * This option is useful when only the SDK libraries are needed. |
| */ |
| -void docgen(ArgResults argResults) { |
| - _setCommandLineArguments(argResults); |
| +void docgen(List<String> files, {String packageRoot, bool outputToYaml: true, |
| + bool includePrivate: false, bool includeSdk: false, bool parseSdk: false}) { |
| + if (packageRoot == null) { |
| + packageRoot = _findPackageRoot(files.first); |
| + } |
| + logger.info('Package Root: ${packageRoot}'); |
| linkResolver = (name) => |
| fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| - getMirrorSystem(argResults.rest).then((MirrorSystem mirrorSystem) { |
| - if (mirrorSystem.libraries.values.isEmpty) { |
| - throw new StateError('No Library Mirrors.'); |
| + var mirrorSystem = getMirrorSystem(files, packageRoot, parseSdk: parseSdk); |
|
Andrei Mouravski
2013/07/02 18:30:18
This isn't what I meant. I meant for docgen() to r
Andrei Mouravski
2013/07/03 07:31:19
Did you forget about this one?
Emily Fortuna
2013/07/03 16:29:40
We couldn't figure out what you meant, so I sugges
|
| + mirrorSystem.then((MirrorSystem mirrorSystem) { |
| + if (mirrorSystem.libraries.isEmpty) { |
| + throw new StateError('No Library Mirrors were created.'); |
|
Andrei Mouravski
2013/07/02 18:30:18
Bad capitalization.
janicejl
2013/07/02 22:06:14
Done.
|
| } |
| - _documentLibraries(mirrorSystem.libraries.values); |
| + _documentLibraries(mirrorSystem.libraries.values, |
| + includeSdk: includeSdk, includePrivate: includePrivate, |
| + outputToYaml: outputToYaml); |
| }); |
| } |
| -void _setCommandLineArguments(ArgResults argResults) { |
| - outputToYaml = argResults['yaml'] || argResults['output-format'] == 'yaml'; |
| - outputToJson = argResults['json'] || argResults['output-format'] == 'json'; |
| - if (outputToYaml && outputToJson) { |
| - throw new ArgumentError('Cannot have contradictory output flags.'); |
| - } |
| - outputToYaml = outputToYaml || !outputToJson; |
| - includePrivate = argResults['include-private']; |
| - parseSdk = argResults['parse-sdk']; |
| - includeSdk = parseSdk || argResults['include-sdk']; |
| - packageDir = argResults['package-root']; |
| - if (packageDir != null) logger.info('Package Root: ${packageDir}'); |
| -} |
| - |
| List<String> _listLibraries(List<String> args) { |
| // TODO(janicejl): At the moment, only have support to have either one file, |
| // or one directory. This is because there can only be one package directory |
| @@ -113,12 +100,6 @@ List<String> _listLibraries(List<String> args) { |
| List<String> _listDartFromDir(String args) { |
| var files = listDir(args, recursive: true); |
| - if (packageDir == null) { |
| - packageDir = files.firstWhere((f) => |
| - f.endsWith('/pubspec.yaml'), orElse: () => ''); |
| - if (packageDir != '') packageDir = path.dirname(packageDir) + '/packages'; |
| - logger.info('Package Directory: $packageDir'); |
| - } |
| // To avoid anaylzing package files twice, only files with paths not |
| // containing '/packages' will be added. The only exception is if the file to |
| // analyze already has a '/package' in its path. |
| @@ -127,6 +108,16 @@ List<String> _listDartFromDir(String args) { |
| ..forEach((lib) => logger.info('Added to libraries: $lib')); |
| } |
| +String _findPackageRoot(String args) { |
|
Andrei Mouravski
2013/07/02 18:30:18
Nit: You should have better parameter names than a
janicejl
2013/07/02 22:06:14
Done.
|
| + var files = listDir(args, recursive: true); |
| + String packageRoot = files.firstWhere((f) => |
| + f.endsWith('/pubspec.yaml'), orElse: () => ''); |
|
Andrei Mouravski
2013/07/02 18:30:18
What does it mean for this method to return ''?
janicejl
2013/07/02 22:06:14
Return '' means that there was no pubspec.yaml and
Andrei Mouravski
2013/07/03 07:31:19
Okay, well, this is kind of weird, because it's ve
|
| + if (packageRoot != '') { |
| + packageRoot = path.dirname(packageRoot) + '/packages'; |
| + } |
| + return packageRoot; |
| +} |
| + |
| List<String> _listSdk() { |
| var sdk = new List<String>(); |
| LIBRARIES.forEach((String name, LibraryInfo info) { |
| @@ -142,7 +133,8 @@ List<String> _listSdk() { |
| * Analyzes set of libraries by getting a mirror system and triggers the |
| * documentation of the libraries. |
| */ |
| -Future<MirrorSystem> getMirrorSystem(List<String> args) { |
| +Future<MirrorSystem> getMirrorSystem(List<String> args, String packageRoot, |
| + {bool parseSdk:false}) { |
| var libraries = !parseSdk ? _listLibraries(args) : _listSdk(); |
| if (libraries.isEmpty) throw new StateError('No Libraries.'); |
| // DART_SDK should be set to the root of the SDK library. |
| @@ -156,7 +148,7 @@ Future<MirrorSystem> getMirrorSystem(List<String> args) { |
| logger.info('SDK Root: ${sdkRoot}'); |
| } |
| - return _getMirrorSystemHelper(libraries, sdkRoot, packageRoot: packageDir); |
| + return _analyzeLibraries(libraries, sdkRoot, packageRoot: packageRoot); |
| } |
| // TODO(janicejl): Should make docgen fail gracefully, or output a friendly |
| @@ -167,7 +159,7 @@ Future<MirrorSystem> getMirrorSystem(List<String> args) { |
| * Analyzes set of libraries and provides a mirror system which can be used |
| * for static inspection of the source code. |
| */ |
| -Future<MirrorSystem> _getMirrorSystemHelper(List<String> libraries, |
| +Future<MirrorSystem> _analyzeLibraries(List<String> libraries, |
| String libraryRoot, {String packageRoot}) { |
| SourceFileProvider provider = new SourceFileProvider(); |
| api.DiagnosticHandler diagnosticHandler = |
| @@ -197,12 +189,13 @@ Future<MirrorSystem> _getMirrorSystemHelper(List<String> libraries, |
| /** |
| * Creates documentation for filtered libraries. |
| */ |
| -void _documentLibraries(List<LibraryMirror> libraries) { |
| +void _documentLibraries(List<LibraryMirror> libraries, |
| + {bool includeSdk:false, bool includePrivate:false, bool outputToYaml:true}) { |
|
Andrei Mouravski
2013/07/02 18:30:18
Well, I guess they don't fit, since you really sho
janicejl
2013/07/02 22:06:14
Done.
|
| libraries.forEach((lib) { |
| // Files belonging to the SDK have a uri that begins with 'dart:'. |
| if (includeSdk || !lib.uri.toString().startsWith('dart:')) { |
| - var library = generateLibrary(lib); |
| - _outputLibrary(library); |
| + var library = generateLibrary(lib, includePrivate: includePrivate); |
| + _writeLibraryToFile(library, outputToYaml); |
| } |
| }); |
| // Outputs a text file with a list of files available after creating all |
| @@ -211,22 +204,24 @@ void _documentLibraries(List<LibraryMirror> libraries) { |
| _writeToFile(listDir("docs").join('\n'), 'library_list.txt'); |
| } |
| -Library generateLibrary(dart2js.Dart2JsLibraryMirror library) { |
| +Library generateLibrary(dart2js.Dart2JsLibraryMirror library, |
| + {bool includePrivate:false}) { |
| _currentLibrary = library; |
| var result = new Library(library.qualifiedName, _getComment(library), |
| - _getVariables(library.variables), _getMethods(library.functions), |
| - _getClasses(library.classes)); |
| + _getVariables(library.variables, includePrivate), |
| + _getMethods(library.functions, includePrivate), |
| + _getClasses(library.classes, includePrivate)); |
| logger.fine('Generated library for ${result.name}'); |
| return result; |
| } |
| -void _outputLibrary(Library result) { |
| - if (outputToJson) { |
| - _writeToFile(stringify(result.toMap()), '${result.name}.json'); |
| - } |
| +void _writeLibraryToFile(Library result, bool outputToYaml) { |
| if (outputToYaml) { |
| _writeToFile(getYamlString(result.toMap()), '${result.name}.yaml'); |
| - } |
| + } else { |
| + _writeToFile(stringify(result.toMap()), '${result.name}.json'); |
| + } |
| + |
| } |
| /** |
| @@ -275,7 +270,8 @@ markdown.Node fixReference(String name, LibraryMirror currentLibrary, |
| /** |
| * Returns a map of [Variable] objects constructed from inputted mirrors. |
| */ |
| -Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| +Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, |
| + bool includePrivate) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| if (includePrivate || !mirror.isPrivate) { |
| @@ -291,7 +287,8 @@ Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| /** |
| * Returns a map of [Method] objects constructed from inputted mirrors. |
| */ |
| -Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| +Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap, |
| + bool includePrivate) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| if (includePrivate || !mirror.isPrivate) { |
| @@ -309,7 +306,8 @@ Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| /** |
| * Returns a map of [Class] objects constructed from inputted mirrors. |
| */ |
| -Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| +Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, |
| + bool includePrivate) { |
| var data = {}; |
| mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| if (includePrivate || !mirror.isPrivate) { |
| @@ -321,7 +319,8 @@ Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| data[mirrorName] = new Class(mirrorName, mirror.qualifiedName, |
| superclass, mirror.isAbstract, mirror.isTypedef, |
| _getComment(mirror), interfaces.toList(), |
| - _getVariables(mirror.variables), _getMethods(mirror.methods), |
| + _getVariables(mirror.variables, includePrivate), |
| + _getMethods(mirror.methods, includePrivate), |
| _getAnnotations(mirror)); |
| } |
| }); |