diff --git a/bin/dartdoc.dart b/bin/dartdoc.dart index f2145554a8..4c53f7792c 100644 --- a/bin/dartdoc.dart +++ b/bin/dartdoc.dart @@ -23,20 +23,26 @@ void main(List arguments) { print('$NAME version: $VERSION'); exit(0); } - + Directory sdkDir = grinder.getSdkDir(arguments); if (sdkDir == null) { print( "Warning: unable to locate the Dart SDK. Please use the --dart-sdk " "command line option or set the DART_SDK environment variable."); exit(1); - } + } + + bool sdkDocs = false; + if (results['sdk-docs']) { + sdkDocs = true; + } + List excludeLibraries = results['exclude'] == null ? [] : results['exclude'].split(','); String url = results['url']; - var currentDir = Directory.current; + var currentDir = Directory.current; var generators = initGenerators(url); - new DartDoc(currentDir, excludeLibraries, sdkDir, generators) + new DartDoc(currentDir, excludeLibraries, sdkDir, generators, sdkDocs) ..generateDocs(); } @@ -44,7 +50,7 @@ void main(List arguments) { /// Print help if we are passed the help option or invalid arguments. void _printUsageAndExit(ArgParser parser) { print(parser.usage); - print('Usage: dartdoc --dartsdk=sdkLocation [OPTIONS]'); + print('Usage: dartdoc [OPTIONS]'); exit(0); } @@ -61,5 +67,9 @@ ArgParser _createArgsParser() { abbr: 'h', negatable: false, help: 'show command help'); parser.addFlag('version', help: 'Display the version for $NAME', negatable: false); + parser.addFlag( + 'sdk-docs', + help: 'generate docs for the dart sdk.' + 'Use "--dart-sdk" option to specify path to sdk'); return parser; } diff --git a/lib/dartdoc.dart b/lib/dartdoc.dart index b5fc89a898..32fac5ef98 100644 --- a/lib/dartdoc.dart +++ b/lib/dartdoc.dart @@ -42,21 +42,29 @@ class DartDoc { Directory _rootDir; Directory out; Directory _sdkDir; + bool _sdkDocs; Set libraries = new Set(); List _generators; - DartDoc(this._rootDir, this._excludes, this._sdkDir, this._generators); + DartDoc(this._rootDir, + this._excludes, + this._sdkDir, + this._generators, + [this._sdkDocs = false]); /// Generate the documentation void generateDocs() { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); - var files = findFilesToDocumentInPackage(_rootDir.path); + + var files = _sdkDocs ? [] : findFilesToDocumentInPackage(_rootDir.path); List libs = []; - libs.addAll(parseLibraries(files)); + libs.addAll(_parseLibraries(files)); // remove excluded libraries - _excludes.forEach((pattern) => libs.removeWhere((l) => l.name.startsWith(pattern))); - libs.removeWhere((LibraryElement library) => _excludes.contains(library.name)); + _excludes.forEach((pattern) + => libs.removeWhere((l) => l.name.startsWith(pattern))); + libs.removeWhere((LibraryElement library) + => _excludes.contains(library.name)); libs.sort(elementCompare); libraries.addAll(libs); @@ -65,7 +73,8 @@ class DartDoc { if (!out.existsSync()) { out.createSync(recursive: true); } - Package package = new Package(libraries, _rootDir.path); + Package package = + new Package(libraries, _rootDir.path, _getSdkVersion(), _sdkDocs); _generators.forEach((generator) => generator.generate(package, out)); double seconds = stopwatch.elapsedMilliseconds / 1000.0; @@ -73,14 +82,13 @@ class DartDoc { print("Documented ${libraries.length} " "librar${libraries.length == 1 ? 'y' : 'ies'} in " "${seconds.toStringAsFixed(1)} seconds."); } - List parseLibraries(List files) { - - + List _parseLibraries(List files) { DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(_sdkDir.path)); - ContentCache contentCache = new ContentCache(); - List resolvers = [new DartUriResolver(sdk), new FileUriResolver()]; - JavaFile packagesDir = new JavaFile.relative(new JavaFile(_rootDir.path), 'packages'); + List resolvers = [new DartUriResolver(sdk), + new FileUriResolver()]; + JavaFile packagesDir = + new JavaFile.relative(new JavaFile(_rootDir.path), 'packages'); if (packagesDir.exists()) { resolvers.add(new PackageUriResolver([packagesDir])); } @@ -88,6 +96,9 @@ class DartDoc { AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); context.sourceFactory = sourceFactory; + if (_sdkDocs) { + libraries.addAll(getSdkLibrariesToDocument(sdk, context)); + } files.forEach((String filePath) { print('parsing ${filePath}...'); Source source = new FileBasedSource.con1(new JavaFile(filePath)); @@ -100,4 +111,9 @@ class DartDoc { }); return libraries.toList(); } + + String _getSdkVersion() { + var versionFile = joinFile(_sdkDir, ['version']); + return versionFile.readAsStringSync(); + } } diff --git a/lib/src/html_generator.dart b/lib/src/html_generator.dart index 41cbef1463..c385460e6e 100644 --- a/lib/src/html_generator.dart +++ b/lib/src/html_generator.dart @@ -19,14 +19,15 @@ import '../generator.dart'; class HtmlGenerator extends Generator { // The sitemap template file static final String siteMapTemplate = '/templates/sitemap.xml'; - static final String indexTemplate = '/templates/index.html'; - static final String bootstrapOverrides = ''' body { margin: 8px; }'''; + static final String bootstrapCss = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'; + static final String bootstrapTheme ='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css'; + HtmlPrinter html = new HtmlPrinter(); CSS css = new CSS(); HtmlGeneratorHelper helper; @@ -50,6 +51,8 @@ body { void generatePackage() { var data = { + 'css' : bootstrapCss, + 'theme' : bootstrapTheme, 'packageName': package.name, 'packageDesc': package.description, 'packageVersion': package.version, diff --git a/lib/src/model.dart b/lib/src/model.dart index 9159b5d9c8..86b4b5284f 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -195,18 +195,25 @@ abstract class ModelElement { class Package { String _rootDirPath; - List _libraries = []; + bool _isSdk; + String _sdkVersion; - String get name => getPackageName(_rootDirPath); + String get name => + _isSdk ? 'Dart API Reference' : getPackageName(_rootDirPath); - String get version => getPackageVersion(_rootDirPath); + String get version => + _isSdk ? _sdkVersion : getPackageVersion(_rootDirPath); - String get description => getPackageDescription(_rootDirPath); + String get description => + _isSdk ? 'Dart API Libraries' : getPackageDescription(_rootDirPath); List get libraries => _libraries; - Package(Iterable libraryElements, this._rootDirPath) { + Package(Iterable libraryElements, + this._rootDirPath, + [this._sdkVersion, + this._isSdk = false]) { libraryElements.forEach((element) { _libraries.add(new Library(element)); }); @@ -225,6 +232,11 @@ class Library extends ModelElement { Library(LibraryElement element) : super(element, null); + String get name { + var source = _library.definingCompilationUnit.source; + return source.isInSystemLibrary ? source.encoding : super.name; + } + List get exported => _library.exportedLibraries.map((lib) => new Library(lib)).toList(); diff --git a/lib/src/model_utils.dart b/lib/src/model_utils.dart index 85fd52170d..29c322e81f 100644 --- a/lib/src/model_utils.dart +++ b/lib/src/model_utils.dart @@ -4,8 +4,12 @@ library dartdoc.model_utils; +import 'package:analyzer/src/generated/ast.dart'; import 'package:analyzer/src/generated/constant.dart'; import 'package:analyzer/src/generated/element.dart'; +import 'package:analyzer/src/generated/engine.dart'; +import 'package:analyzer/src/generated/sdk.dart'; +import 'package:analyzer/src/generated/source_io.dart'; Object getConstantValue(PropertyInducingElement element) { if (element is ConstFieldElementImpl) { @@ -31,6 +35,23 @@ int elementCompare(Element a, Element b) => a.name.compareTo(b.name); bool isPrivate(Element e) => e.name.startsWith('_'); +List getSdkLibrariesToDocument(DartSdk sdk, + AnalysisContext context) { + List libraries = []; + var sdkApiLibs = + sdk.sdkLibraries.where((SdkLibrary sdkLib) + => !sdkLib.isInternal && sdkLib.isDocumented).toList(); + sdkApiLibs.sort((lib1, lib2) => lib1.shortName.compareTo(lib2.shortName)); + sdkApiLibs.forEach((SdkLibrary sdkLib) { + Source source = sdk.mapDartUri(sdkLib.shortName); + LibraryElement library = context.computeLibraryElement(source); + CompilationUnit unit = context.resolveCompilationUnit(source, library); + libraries.add(library); + libraries.addAll(library.exportedLibraries); + }); + return libraries; +} + List getAllSupertypes(ClassElement c) { InterfaceType t = c.type; return c.allSupertypes; diff --git a/templates/index.html b/templates/index.html index e5f71cec6f..958731b4d5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,8 +7,8 @@ Package {{packageName}} - - + +