Skip to content
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

Deprecate options.dart. #3591

Merged
merged 1 commit into from
Nov 30, 2023
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 @@ -6,8 +6,8 @@ library dartdoc.bin;

import 'dart:async';

import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_config_provider.dart';
import 'package:dartdoc/src/package_meta.dart';
Expand Down
158 changes: 6 additions & 152 deletions lib/options.dart
Original file line number Diff line number Diff line change
@@ -1,153 +1,7 @@
import 'dart:io' show stderr, exitCode;
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:args/args.dart';
import 'package:dartdoc/src/dartdoc.dart' show dartdocVersion, programName;
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/package_meta.dart';

/// Helper class that consolidates option contexts for instantiating generators.
class DartdocGeneratorOptionContext extends DartdocOptionContext {
DartdocGeneratorOptionContext(
super.optionSet, super.dir, super.resourceProvider);
DartdocGeneratorOptionContext.fromDefaultContextLocation(
super.optionSet, super.resourceProvider)
: super.fromDefaultContextLocation();

/// The joined contents of any 'header' files specified in options.
String get header =>
_joinCustomTextFiles(optionSet['header'].valueAt(context));

/// The joined contents of any 'footer' files specified in options.
String get footer =>
_joinCustomTextFiles(optionSet['footer'].valueAt(context));

/// The joined contents of any 'footer-text' files specified in options.
String get footerText =>
_joinCustomTextFiles(optionSet['footerText'].valueAt(context));

String _joinCustomTextFiles(Iterable<String> paths) => paths
.map((p) => resourceProvider.getFile(p).readAsStringSync())
.join('\n');

bool get prettyIndexJson => optionSet['prettyIndexJson'].valueAt(context);

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

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

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

// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
@override
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);

String? get resourcesDir => optionSet['resourcesDir'].valueAt(context);
}

class DartdocProgramOptionContext extends DartdocGeneratorOptionContext
with LoggingContext {
DartdocProgramOptionContext(
super.optionSet, super.dir, super.resourceProvider);

DartdocProgramOptionContext.fromDefaultContextLocation(
super.optionSet, super.resourceProvider)
: super.fromDefaultContextLocation();

/// Whether to generate docs or perform a dry run.
bool get generateDocs => optionSet['generateDocs'].valueAt(context);
bool get help => optionSet['help'].valueAt(context);
bool get version => optionSet['version'].valueAt(context);
}

List<DartdocOption<bool>> createDartdocProgramOptions(
PackageMetaProvider packageMetaProvider) {
var resourceProvider = packageMetaProvider.resourceProvider;
return [
DartdocOptionArgOnly<bool>('generateDocs', true, resourceProvider,
help:
'Generate docs into the output directory (or only display warnings '
'if false).',
negatable: true),
DartdocOptionArgOnly<bool>('help', false, resourceProvider,
abbr: 'h', help: 'Show command help.', negatable: false),
DartdocOptionArgOnly<bool>('version', false, resourceProvider,
help: 'Display the version for $programName.', negatable: false),
];
}

DartdocProgramOptionContext? parseOptions(
PackageMetaProvider packageMetaProvider,
List<String> arguments, {
// Additional options are given in google3.
OptionGenerator? additionalOptions,
}) {
var optionRoot = DartdocOptionRoot.fromOptionGenerators(
'dartdoc',
[
createDartdocOptions,
createDartdocProgramOptions,
createLoggingOptions,
createGeneratorOptions,
if (additionalOptions != null) additionalOptions,
],
packageMetaProvider);

try {
optionRoot.parseArguments(arguments);
} on FormatException catch (e) {
stderr.writeln(' fatal error: ${e.message}');
stderr.writeln('');
_printUsage(optionRoot.argParser);
// Do not use `exit()` as this bypasses `--pause-isolates-on-exit`.
exitCode = 64;
return null;
}
if (optionRoot['help'].valueAtCurrent() as bool) {
_printHelp(optionRoot.argParser);
exitCode = 0;
return null;
}
if (optionRoot['version'].valueAtCurrent() as bool) {
_printVersion(optionRoot.argParser);
exitCode = 0;
return null;
}

DartdocProgramOptionContext config;
try {
config = DartdocProgramOptionContext.fromDefaultContextLocation(
optionRoot, packageMetaProvider.resourceProvider);
} on DartdocOptionError catch (e) {
stderr.writeln(' fatal error: ${e.message}');
stderr.writeln('');
_printUsage(optionRoot.argParser);
exitCode = 64;
return null;
}
startLogging(
isJson: config.json,
isQuiet: config.quiet,
showProgress: config.showProgress,
);
return config;
}

/// Print help if we are passed the help option.
void _printHelp(ArgParser parser) {
print('Generate HTML documentation for Dart libraries.\n');
print(parser.usage);
}

/// Print usage information on invalid command lines.
void _printUsage(ArgParser parser) {
print('Usage: dartdoc [OPTIONS]\n');
print(parser.usage);
}

/// Print version information.
void _printVersion(ArgParser parser) {
print('dartdoc version: $dartdocVersion');
}
@Deprecated(
'Will be removed in a later version of DartDoc. Use /src/dartdoc_options.dart.')
export 'package:dartdoc/src/dartdoc_options.dart';
1 change: 0 additions & 1 deletion lib/src/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:convert';
import 'dart:io' show Platform, exitCode, stderr;

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/generator/empty_generator.dart';
Expand Down
150 changes: 149 additions & 1 deletion lib/src/dartdoc_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
///
library dartdoc.dartdoc_options;

import 'dart:io' show Platform, stdout;
import 'dart:io' show Platform, exitCode, stderr, stdout;

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:args/args.dart';
import 'package:dartdoc/src/dartdoc.dart' show dartdocVersion, programName;
import 'package:dartdoc/src/experiment_options.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/io_utils.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:dartdoc/src/source_linker.dart';
Expand Down Expand Up @@ -1340,6 +1343,151 @@ class DartdocOptionContext extends DartdocOptionContextBase
int.parse(optionSet['maxTotalSize'].valueAt(context) ?? '0');
}

/// Helper class that consolidates option contexts for instantiating generators.
class DartdocGeneratorOptionContext extends DartdocOptionContext {
DartdocGeneratorOptionContext(
super.optionSet, super.dir, super.resourceProvider);
DartdocGeneratorOptionContext.fromDefaultContextLocation(
super.optionSet, super.resourceProvider)
: super.fromDefaultContextLocation();

/// The joined contents of any 'header' files specified in options.
String get header =>
_joinCustomTextFiles(optionSet['header'].valueAt(context));

/// The joined contents of any 'footer' files specified in options.
String get footer =>
_joinCustomTextFiles(optionSet['footer'].valueAt(context));

/// The joined contents of any 'footer-text' files specified in options.
String get footerText =>
_joinCustomTextFiles(optionSet['footerText'].valueAt(context));

String _joinCustomTextFiles(Iterable<String> paths) => paths
.map((p) => resourceProvider.getFile(p).readAsStringSync())
.join('\n');

bool get prettyIndexJson => optionSet['prettyIndexJson'].valueAt(context);

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

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

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

// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
@override
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);

String? get resourcesDir => optionSet['resourcesDir'].valueAt(context);
}

class DartdocProgramOptionContext extends DartdocGeneratorOptionContext
with LoggingContext {
DartdocProgramOptionContext(
super.optionSet, super.dir, super.resourceProvider);

DartdocProgramOptionContext.fromDefaultContextLocation(
super.optionSet, super.resourceProvider)
: super.fromDefaultContextLocation();

/// Whether to generate docs or perform a dry run.
bool get generateDocs => optionSet['generateDocs'].valueAt(context);
bool get help => optionSet['help'].valueAt(context);
bool get version => optionSet['version'].valueAt(context);
}

List<DartdocOption<bool>> createDartdocProgramOptions(
PackageMetaProvider packageMetaProvider) {
var resourceProvider = packageMetaProvider.resourceProvider;
return [
DartdocOptionArgOnly<bool>('generateDocs', true, resourceProvider,
help:
'Generate docs into the output directory (or only display warnings '
'if false).',
negatable: true),
DartdocOptionArgOnly<bool>('help', false, resourceProvider,
abbr: 'h', help: 'Show command help.', negatable: false),
DartdocOptionArgOnly<bool>('version', false, resourceProvider,
help: 'Display the version for $programName.', negatable: false),
];
}

DartdocProgramOptionContext? parseOptions(
PackageMetaProvider packageMetaProvider,
List<String> arguments, {
// Additional options are given in google3.
OptionGenerator? additionalOptions,
}) {
var optionRoot = DartdocOptionRoot.fromOptionGenerators(
'dartdoc',
[
createDartdocOptions,
createDartdocProgramOptions,
createLoggingOptions,
createGeneratorOptions,
if (additionalOptions != null) additionalOptions,
],
packageMetaProvider);

try {
optionRoot.parseArguments(arguments);
} on FormatException catch (e) {
stderr.writeln(' fatal error: ${e.message}');
stderr.writeln('');
_printUsage(optionRoot.argParser);
// Do not use `exit()` as this bypasses `--pause-isolates-on-exit`.
exitCode = 64;
return null;
}
if (optionRoot['help'].valueAtCurrent() as bool) {
_printHelp(optionRoot.argParser);
exitCode = 0;
return null;
}
if (optionRoot['version'].valueAtCurrent() as bool) {
_printVersion(optionRoot.argParser);
exitCode = 0;
return null;
}

DartdocProgramOptionContext config;
try {
config = DartdocProgramOptionContext.fromDefaultContextLocation(
optionRoot, packageMetaProvider.resourceProvider);
} on DartdocOptionError catch (e) {
stderr.writeln(' fatal error: ${e.message}');
stderr.writeln('');
_printUsage(optionRoot.argParser);
exitCode = 64;
return null;
}
startLogging(
isJson: config.json,
isQuiet: config.quiet,
showProgress: config.showProgress,
);
return config;
}

/// Print help if we are passed the help option.
void _printHelp(ArgParser parser) {
print('Generate HTML documentation for Dart libraries.\n');
print(parser.usage);
}

/// Print usage information on invalid command lines.
void _printUsage(ArgParser parser) {
print('Usage: dartdoc [OPTIONS]\n');
print(parser.usage);
}

/// Print version information.
void _printVersion(ArgParser parser) {
print('dartdoc version: $dartdocVersion');
}

/// Instantiate dartdoc's configuration file and options parser with the
/// given command line arguments.
List<DartdocOption> createDartdocOptions(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generator/generator_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/generator/generator_utils.dart' as generator_util;
import 'package:dartdoc/src/generator/template_data.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generator/html_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

library dartdoc.html_generator;

import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/generator/generator_backend.dart';
import 'package:dartdoc/src/generator/generator_frontend.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generator/markdown_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/generator/generator_backend.dart';
import 'package:dartdoc/src/generator/generator_frontend.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generator/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
library dartdoc.templates;

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/failure.dart';
import 'package:dartdoc/src/generator/resource_loader.dart';
Expand Down
1 change: 0 additions & 1 deletion test/end2end/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ library dartdoc.dartdoc_test;
import 'dart:async';

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/options.dart';
import 'package:dartdoc/src/dartdoc.dart' show Dartdoc, DartdocResults;
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/failure.dart';
Expand Down
Loading
Loading