Skip to content

Commit 7da2117

Browse files
mit-mitCommit Bot
authored and
Commit Bot
committed
Initial 'dart doc' developer command
Initial 'dart doc' command for the unified 'dart' developer tool, over time replacing the exiting 'bin/dartdoc' tool. This is a second attempt after the previous got reverted: https://dart-review.googlesource.com/c/sdk/+/217980 The first attempt, which was already reviewed, is in patchset 1. Change-Id: Id9e0e572944ba032c32f3cebec579ab23d0df036 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220744 Commit-Queue: Michael Thomsen <[email protected]> Reviewed-by: Ben Konyi <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent e774227 commit 7da2117

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

pkg/dartdev/lib/dartdev.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'src/commands/analyze.dart';
2121
import 'src/commands/compile.dart';
2222
import 'src/commands/create.dart';
2323
import 'src/commands/debug_adapter.dart';
24+
import 'src/commands/doc.dart';
2425
import 'src/commands/fix.dart';
2526
import 'src/commands/language_server.dart';
2627
import 'src/commands/migrate.dart';
@@ -114,6 +115,7 @@ class DartdevRunner extends CommandRunner<int> {
114115
addCommand(CreateCommand(verbose: verbose));
115116
addCommand(DebugAdapterCommand(verbose: verbose));
116117
addCommand(CompileCommand(verbose: verbose));
118+
addCommand(DocCommand(verbose: verbose));
117119
addCommand(DevToolsCommand(
118120
verbose: verbose,
119121
customDevToolsPath: sdk.devToolsBinaries,

pkg/dartdev/lib/src/commands/doc.dart

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'dart:async';
6+
import 'dart:io' as io;
7+
8+
import 'package:dartdoc/dartdoc.dart';
9+
import 'package:dartdoc/options.dart';
10+
import 'package:path/path.dart' as path;
11+
12+
import '../core.dart';
13+
import '../sdk.dart';
14+
15+
/// A command to create a new project from a set of templates.
16+
class DocCommand extends DartdevCommand {
17+
static const String cmdName = 'doc';
18+
19+
DocCommand({bool verbose = false})
20+
: super(
21+
cmdName,
22+
'Generate HTML API documentation from Dart documentation comments.',
23+
verbose,
24+
) {
25+
argParser.addOption(
26+
'output-dir',
27+
abbr: 'o',
28+
defaultsTo: path.join('.', 'doc', 'api'),
29+
help: 'Output directory',
30+
);
31+
argParser.addFlag(
32+
'validate-links',
33+
negatable: true,
34+
help: 'Display context aware warnings for broken links (slow)',
35+
);
36+
}
37+
38+
@override
39+
String get invocation => '${super.invocation} <input directory>';
40+
41+
@override
42+
FutureOr<int> run() async {
43+
// At least one argument, the input directory, is required.
44+
if (argResults.rest.isEmpty) {
45+
usageException("Error: Input directory not specified");
46+
}
47+
48+
// Determine input directory.
49+
final dir = io.Directory(argResults.rest[0]);
50+
if (!dir.existsSync()) {
51+
usageException("Error: Input directory doesn't exist: ${dir.path}");
52+
}
53+
54+
// Parse options.
55+
final options = [
56+
'--input=${dir.path}',
57+
'--output=${argResults['output-dir']}',
58+
];
59+
if (argResults['validate-links']) {
60+
options.add('--validate-links');
61+
} else {
62+
options.add('--no-validate-links');
63+
}
64+
65+
// Specify where dartdoc resources are located.
66+
final resourcesPath =
67+
path.absolute(sdk.sdkPath, 'bin', 'resources', 'dartdoc', 'resources');
68+
options.add('--resources-dir=$resourcesPath');
69+
70+
final config = await parseOptions(pubPackageMetaProvider, options);
71+
if (config == null) {
72+
// There was an error while parsing options.
73+
return 2;
74+
}
75+
76+
// Call dartdoc.
77+
if (verbose) {
78+
log.stdout('Calling dartdoc with the following options: $options');
79+
}
80+
final packageConfigProvider = PhysicalPackageConfigProvider();
81+
final packageBuilder = PubPackageBuilder(
82+
config, pubPackageMetaProvider, packageConfigProvider);
83+
final dartdoc = config.generateDocs
84+
? await Dartdoc.fromContext(config, packageBuilder)
85+
: await Dartdoc.withEmptyGenerator(config, packageBuilder);
86+
dartdoc.executeGuarded();
87+
return 0;
88+
}
89+
}

pkg/dartdev/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies:
1717
dart2native:
1818
path: ../dart2native
1919
dart_style: any
20+
dartdoc: any
2021
dds:
2122
path: ../dds
2223
devtools_server: any
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:test/test.dart';
6+
7+
import '../utils.dart';
8+
9+
const int compileErrorExitCode = 64;
10+
11+
void main() {
12+
group('doc', defineCompileTests, timeout: longTimeout);
13+
}
14+
15+
void defineCompileTests() {
16+
test('Passing no args fails', () async {
17+
final p = project();
18+
var result = await p.run(['doc']);
19+
expect(result.stderr, contains('Input directory not specified'));
20+
expect(result.exitCode, compileErrorExitCode);
21+
});
22+
23+
test('--help', () async {
24+
final p = project();
25+
final result = await p.run(['doc', '--help']);
26+
expect(
27+
result.stdout,
28+
contains('Usage: dart doc [arguments] <input directory>'),
29+
);
30+
31+
expect(result.exitCode, 0);
32+
});
33+
34+
test('Document a library', () async {
35+
final source = '''
36+
/// This is Foo. It uses [Bar].
37+
class Foo {
38+
Bar bar;
39+
}
40+
41+
/// Bar is very nice.
42+
class Bar {
43+
_i = 42;
44+
}
45+
''';
46+
47+
final p = project(mainSrc: 'void main() { print("Hello, World"); }');
48+
p.file('lib/foo.dart', source);
49+
final result = await p.run(['doc', '--validate-links', p.dirPath]);
50+
print(
51+
'exit: ${result.exitCode}, stderr:\n${result.stderr}\nstdout:\n${result.stdout}');
52+
expect(result.stdout, contains('Documenting dartdev_temp'));
53+
});
54+
55+
test('Document a library with broken link is flagged', () async {
56+
final source = '''
57+
/// This is Foo. It uses [Baz].
58+
class Foo {
59+
// Bar bar;
60+
}
61+
''';
62+
63+
final p = project(mainSrc: 'void main() { print("Hello, World"); }');
64+
p.file('lib/foo.dart', source);
65+
final result = await p.run(['doc', '--validate-links', p.dirPath]);
66+
print(
67+
'exit: ${result.exitCode}, stderr:\n${result.stderr}\nstdout:\n${result.stdout}');
68+
expect(result.stdout, contains('Documenting dartdev_temp'));
69+
});
70+
}

sdk/bin/dartdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ SNAPSHOT="$BIN_DIR/snapshots/dartdoc.dart.snapshot"
2323

2424
# We are running the snapshot in the built SDK.
2525
DART="$BIN_DIR/dart"
26-
exec "$DART" "--packages=$BIN_DIR/resources/dartdoc/.packages" "$SNAPSHOT" "$@"
26+
exec "$DART" "--packages=$BIN_DIR/resources/dartdoc/.packages" "$SNAPSHOT" "--resources-dir=$BIN_DIR/resources/dartdoc/resources/" "$@"

0 commit comments

Comments
 (0)