Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions bin/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,34 @@ void main(List<String> 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<String> 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();
}


/// 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);
}

Expand All @@ -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;
}
40 changes: 28 additions & 12 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,29 @@ class DartDoc {
Directory _rootDir;
Directory out;
Directory _sdkDir;
bool _sdkDocs;
Set<LibraryElement> libraries = new Set();
List<Generator> _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<LibraryElement> 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);

Expand All @@ -65,29 +73,32 @@ 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;
print('');
print("Documented ${libraries.length} " "librar${libraries.length == 1 ? 'y' : 'ies'} in " "${seconds.toStringAsFixed(1)} seconds.");
}

List<LibraryElement> parseLibraries(List<String> files) {


List<LibraryElement> _parseLibraries(List<String> files) {
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(_sdkDir.path));

ContentCache contentCache = new ContentCache();
List<UriResolver> resolvers = [new DartUriResolver(sdk), new FileUriResolver()];
JavaFile packagesDir = new JavaFile.relative(new JavaFile(_rootDir.path), 'packages');
List<UriResolver> resolvers = [new DartUriResolver(sdk),
new FileUriResolver()];
JavaFile packagesDir =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always confuses me. When can we rename JavaFile? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will talk to the analyzer folks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new JavaFile.relative(new JavaFile(_rootDir.path), 'packages');
if (packagesDir.exists()) {
resolvers.add(new PackageUriResolver([packagesDir]));
}
SourceFactory sourceFactory = new SourceFactory(/*contentCache,*/ resolvers);
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));
Expand All @@ -100,4 +111,9 @@ class DartDoc {
});
return libraries.toList();
}

String _getSdkVersion() {
var versionFile = joinFile(_sdkDir, ['version']);
return versionFile.readAsStringSync();
}
}
7 changes: 5 additions & 2 deletions lib/src/html_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,6 +51,8 @@ body {

void generatePackage() {
var data = {
'css' : bootstrapCss,
'theme' : bootstrapTheme,
'packageName': package.name,
'packageDesc': package.description,
'packageVersion': package.version,
Expand Down
22 changes: 17 additions & 5 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,25 @@ abstract class ModelElement {

class Package {
String _rootDirPath;

List<Library> _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<Library> get libraries => _libraries;

Package(Iterable<LibraryElement> libraryElements, this._rootDirPath) {
Package(Iterable<LibraryElement> libraryElements,
this._rootDirPath,
[this._sdkVersion,
this._isSdk = false]) {
libraryElements.forEach((element) {
_libraries.add(new Library(element));
});
Expand All @@ -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<Library> get exported =>
_library.exportedLibraries.map((lib) => new Library(lib)).toList();

Expand Down
21 changes: 21 additions & 0 deletions lib/src/model_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -31,6 +35,23 @@ int elementCompare(Element a, Element b) => a.name.compareTo(b.name);

bool isPrivate(Element e) => e.name.startsWith('_');

List<LibraryElement> getSdkLibrariesToDocument(DartSdk sdk,
AnalysisContext context) {
List<LibraryElement> 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<InterfaceType> getAllSupertypes(ClassElement c) {
InterfaceType t = c.type;
return c.allSupertypes;
Expand Down
6 changes: 3 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Package {{packageName}}</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="{{css}}" rel="stylesheet" media="screen">
<link href="{{theme}}" rel="stylesheet">
<style>
body {
margin: 8px;
Expand Down Expand Up @@ -37,7 +37,7 @@ <h4>
<tbody>
{{ #libraries }}
<tr>
<td><a href="{{ filename }}">{{ name }}</a></td>
<td valign="top"><a href="{{ filename }}">{{ name }}</a></td>
<td>{{ descr }}</td>
</tr>
{{ /libraries }}
Expand Down
29 changes: 27 additions & 2 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ library dartdoc.model_test;

import 'dart:io';

import 'package:dartdoc/src/model.dart';
import 'package:grinder/grinder.dart' as grinder;
import 'package:unittest/unittest.dart';

Expand All @@ -18,6 +17,9 @@ import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/sdk_io.dart';
import 'package:analyzer/src/generated/source_io.dart';

import '../lib/src/model.dart';
import '../lib/src/model_utils.dart';

const SOURCE1 = r'''
library ex;

Expand Down Expand Up @@ -67,6 +69,20 @@ tests() {
test('is documented', () {
expect(p.isDocumented(l), true);
});

var p2 = new Package([e], Directory.current.path, '1.9.0-dev.3.0', true);

test('sdk name', () {
expect(p2.name, 'Dart API Reference');
});

test('sdk version', () {
expect(p2.version, '1.9.0-dev.3.0');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this change over time? will that make it hard to keep updated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is just a test and does not need to be updated.

});

test('sdk description', () {
expect(p2.description, 'Dart API Libraries');
});
});


Expand All @@ -75,6 +91,13 @@ tests() {
test('name', () {
expect(l.name, 'ex');
});

Library sdkLib = new Library(getSdkLibrariesToDocument(helper.sdk, helper.context)[0]);

test('sdk library name', () {
expect(sdkLib.name, 'dart:async');
});

});

group('Class', () {
Expand Down Expand Up @@ -273,14 +296,15 @@ tests() {

class AnalyzerHelper {
AnalysisContext context;
DartSdk sdk;

AnalyzerHelper() {
_initAnalyzer();
}

void _initAnalyzer() {
Directory sdkDir = grinder.getSdkDir(['']);
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir.path));
List<UriResolver> resolvers = [new DartUriResolver(sdk), new FileUriResolver()];

SourceFactory sourceFactory = new SourceFactory(resolvers);
Expand All @@ -304,4 +328,5 @@ class AnalyzerHelper {
}

LibraryElement resolve(Source librarySource) => context.computeLibraryElement(librarySource);

}