Skip to content

Add config option for output format #2132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2020
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
2 changes: 1 addition & 1 deletion bin/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Future<void> main(List<String> arguments) async {
startLogging(config);

Dartdoc dartdoc = config.generateDocs
? await Dartdoc.withDefaultGenerators(config)
? await Dartdoc.fromContext(config)
: await Dartdoc.withEmptyGenerator(config);
dartdoc.onCheckProgress.listen(logProgress);
try {
Expand Down
22 changes: 21 additions & 1 deletion lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,36 @@ class Dartdoc extends PackageBuilder {

/// An asynchronous factory method that builds Dartdoc's file writers
/// and returns a Dartdoc object with them.
@Deprecated('Prefer withContext() instead')
static Future<Dartdoc> withDefaultGenerators(
DartdocGeneratorOptionContext config) async {
return Dartdoc._(config, await initHtmlGenerator(config));
}

/// An asynchronous factory method that builds
/// Asynchronous factory method that builds Dartdoc with an empty generator.
static Future<Dartdoc> withEmptyGenerator(DartdocOptionContext config) async {
return Dartdoc._(config, await initEmptyGenerator(config));
}

/// Asynchronous factory method that builds Dartdoc with a generator
/// determined by the given context.
static Future<Dartdoc> fromContext(
DartdocGeneratorOptionContext context) async {
Generator generator;
switch (context.format) {
case 'html':
generator = await initHtmlGenerator(context);
break;
case 'md':
// TODO(jdkoren): use a real generator
generator = await initEmptyGenerator(context);
break;
default:
throw DartdocFailure('Unsupported output format: ${context.format}');
}
return Dartdoc._(context, generator);
}

Stream<String> get onCheckProgress => _onCheckProgress.stream;

PackageGraph packageGraph;
Expand Down
7 changes: 6 additions & 1 deletion lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ mixin GeneratorContext on DartdocOptionContextBase {

String get templatesDir => optionSet['templatesDir'].valueAt(context);

/// Output format, e.g. 'html', 'md'
String get format => optionSet['format'].valueAt(context);

// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
}
Expand Down Expand Up @@ -137,6 +140,8 @@ Future<List<DartdocOption>> createGeneratorOptions() async {
'top_level_property, typedef. Partial templates are supported; '
'they must begin with an underscore, and references to them must '
'omit the leading underscore (e.g. use {{>foo}} to reference the '
'partial template _foo.html).'),
'partial template named _foo).'),
// TODO(jdkoren): Unhide when we have good support for another format.
DartdocOptionArgOnly<String>('format', 'html', hide: true),
];
}
16 changes: 13 additions & 3 deletions test/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ void main() {

Future<Dartdoc> buildDartdoc(
List<String> argv, Directory packageRoot, Directory tempDir) async {
return await Dartdoc.withDefaultGenerators(await generatorContextFromArgv(
argv
..addAll(['--input', packageRoot.path, '--output', tempDir.path])));
return await Dartdoc.fromContext(await generatorContextFromArgv(argv
..addAll(['--input', packageRoot.path, '--output', tempDir.path])));
}

group('Option handling', () {
Expand Down Expand Up @@ -425,5 +424,16 @@ void main() {
expect(level2.readAsStringSync(),
contains('<link rel="canonical" href="$prefix/ex/Apple/m.html">'));
});

test('generate docs with bad output format', () async {
try {
await buildDartdoc(['--format', 'bad'], testPackageDir, tempDir);
fail('dartdoc should fail with bad output format');
} catch (e) {
expect(e is DartdocFailure, isTrue);
expect((e as DartdocFailure).message,
startsWith('Unsupported output format'));
}
});
}, timeout: Timeout.factor(8));
}