|
14 | 14 | /// |
15 | 15 | library dartdoc.dartdoc_options; |
16 | 16 |
|
17 | | -import 'dart:io' show Platform, stdout; |
| 17 | +import 'dart:io' show Platform, exitCode, stderr, stdout; |
18 | 18 |
|
19 | 19 | import 'package:analyzer/dart/element/element.dart'; |
20 | 20 | import 'package:analyzer/file_system/file_system.dart'; |
21 | 21 | import 'package:args/args.dart'; |
| 22 | +import 'package:dartdoc/src/dartdoc.dart' show dartdocVersion, programName; |
22 | 23 | import 'package:dartdoc/src/experiment_options.dart'; |
23 | 24 | import 'package:dartdoc/src/failure.dart'; |
| 25 | +import 'package:dartdoc/src/generator/generator.dart'; |
24 | 26 | import 'package:dartdoc/src/io_utils.dart'; |
| 27 | +import 'package:dartdoc/src/logging.dart'; |
25 | 28 | import 'package:dartdoc/src/model/model.dart'; |
26 | 29 | import 'package:dartdoc/src/package_meta.dart'; |
27 | 30 | import 'package:dartdoc/src/source_linker.dart'; |
@@ -1340,6 +1343,151 @@ class DartdocOptionContext extends DartdocOptionContextBase |
1340 | 1343 | int.parse(optionSet['maxTotalSize'].valueAt(context) ?? '0'); |
1341 | 1344 | } |
1342 | 1345 |
|
| 1346 | +/// Helper class that consolidates option contexts for instantiating generators. |
| 1347 | +class DartdocGeneratorOptionContext extends DartdocOptionContext { |
| 1348 | + DartdocGeneratorOptionContext( |
| 1349 | + super.optionSet, super.dir, super.resourceProvider); |
| 1350 | + DartdocGeneratorOptionContext.fromDefaultContextLocation( |
| 1351 | + super.optionSet, super.resourceProvider) |
| 1352 | + : super.fromDefaultContextLocation(); |
| 1353 | + |
| 1354 | + /// The joined contents of any 'header' files specified in options. |
| 1355 | + String get header => |
| 1356 | + _joinCustomTextFiles(optionSet['header'].valueAt(context)); |
| 1357 | + |
| 1358 | + /// The joined contents of any 'footer' files specified in options. |
| 1359 | + String get footer => |
| 1360 | + _joinCustomTextFiles(optionSet['footer'].valueAt(context)); |
| 1361 | + |
| 1362 | + /// The joined contents of any 'footer-text' files specified in options. |
| 1363 | + String get footerText => |
| 1364 | + _joinCustomTextFiles(optionSet['footerText'].valueAt(context)); |
| 1365 | + |
| 1366 | + String _joinCustomTextFiles(Iterable<String> paths) => paths |
| 1367 | + .map((p) => resourceProvider.getFile(p).readAsStringSync()) |
| 1368 | + .join('\n'); |
| 1369 | + |
| 1370 | + bool get prettyIndexJson => optionSet['prettyIndexJson'].valueAt(context); |
| 1371 | + |
| 1372 | + String? get favicon => optionSet['favicon'].valueAt(context); |
| 1373 | + |
| 1374 | + String? get relCanonicalPrefix => |
| 1375 | + optionSet['relCanonicalPrefix'].valueAt(context); |
| 1376 | + |
| 1377 | + String? get templatesDir => optionSet['templatesDir'].valueAt(context); |
| 1378 | + |
| 1379 | + // TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration. |
| 1380 | + @override |
| 1381 | + bool get useBaseHref => optionSet['useBaseHref'].valueAt(context); |
| 1382 | + |
| 1383 | + String? get resourcesDir => optionSet['resourcesDir'].valueAt(context); |
| 1384 | +} |
| 1385 | + |
| 1386 | +class DartdocProgramOptionContext extends DartdocGeneratorOptionContext |
| 1387 | + with LoggingContext { |
| 1388 | + DartdocProgramOptionContext( |
| 1389 | + super.optionSet, super.dir, super.resourceProvider); |
| 1390 | + |
| 1391 | + DartdocProgramOptionContext.fromDefaultContextLocation( |
| 1392 | + super.optionSet, super.resourceProvider) |
| 1393 | + : super.fromDefaultContextLocation(); |
| 1394 | + |
| 1395 | + /// Whether to generate docs or perform a dry run. |
| 1396 | + bool get generateDocs => optionSet['generateDocs'].valueAt(context); |
| 1397 | + bool get help => optionSet['help'].valueAt(context); |
| 1398 | + bool get version => optionSet['version'].valueAt(context); |
| 1399 | +} |
| 1400 | + |
| 1401 | +List<DartdocOption<bool>> createDartdocProgramOptions( |
| 1402 | + PackageMetaProvider packageMetaProvider) { |
| 1403 | + var resourceProvider = packageMetaProvider.resourceProvider; |
| 1404 | + return [ |
| 1405 | + DartdocOptionArgOnly<bool>('generateDocs', true, resourceProvider, |
| 1406 | + help: |
| 1407 | + 'Generate docs into the output directory (or only display warnings ' |
| 1408 | + 'if false).', |
| 1409 | + negatable: true), |
| 1410 | + DartdocOptionArgOnly<bool>('help', false, resourceProvider, |
| 1411 | + abbr: 'h', help: 'Show command help.', negatable: false), |
| 1412 | + DartdocOptionArgOnly<bool>('version', false, resourceProvider, |
| 1413 | + help: 'Display the version for $programName.', negatable: false), |
| 1414 | + ]; |
| 1415 | +} |
| 1416 | + |
| 1417 | +DartdocProgramOptionContext? parseOptions( |
| 1418 | + PackageMetaProvider packageMetaProvider, |
| 1419 | + List<String> arguments, { |
| 1420 | + // Additional options are given in google3. |
| 1421 | + OptionGenerator? additionalOptions, |
| 1422 | +}) { |
| 1423 | + var optionRoot = DartdocOptionRoot.fromOptionGenerators( |
| 1424 | + 'dartdoc', |
| 1425 | + [ |
| 1426 | + createDartdocOptions, |
| 1427 | + createDartdocProgramOptions, |
| 1428 | + createLoggingOptions, |
| 1429 | + createGeneratorOptions, |
| 1430 | + if (additionalOptions != null) additionalOptions, |
| 1431 | + ], |
| 1432 | + packageMetaProvider); |
| 1433 | + |
| 1434 | + try { |
| 1435 | + optionRoot.parseArguments(arguments); |
| 1436 | + } on FormatException catch (e) { |
| 1437 | + stderr.writeln(' fatal error: ${e.message}'); |
| 1438 | + stderr.writeln(''); |
| 1439 | + _printUsage(optionRoot.argParser); |
| 1440 | + // Do not use `exit()` as this bypasses `--pause-isolates-on-exit`. |
| 1441 | + exitCode = 64; |
| 1442 | + return null; |
| 1443 | + } |
| 1444 | + if (optionRoot['help'].valueAtCurrent() as bool) { |
| 1445 | + _printHelp(optionRoot.argParser); |
| 1446 | + exitCode = 0; |
| 1447 | + return null; |
| 1448 | + } |
| 1449 | + if (optionRoot['version'].valueAtCurrent() as bool) { |
| 1450 | + _printVersion(optionRoot.argParser); |
| 1451 | + exitCode = 0; |
| 1452 | + return null; |
| 1453 | + } |
| 1454 | + |
| 1455 | + DartdocProgramOptionContext config; |
| 1456 | + try { |
| 1457 | + config = DartdocProgramOptionContext.fromDefaultContextLocation( |
| 1458 | + optionRoot, packageMetaProvider.resourceProvider); |
| 1459 | + } on DartdocOptionError catch (e) { |
| 1460 | + stderr.writeln(' fatal error: ${e.message}'); |
| 1461 | + stderr.writeln(''); |
| 1462 | + _printUsage(optionRoot.argParser); |
| 1463 | + exitCode = 64; |
| 1464 | + return null; |
| 1465 | + } |
| 1466 | + startLogging( |
| 1467 | + isJson: config.json, |
| 1468 | + isQuiet: config.quiet, |
| 1469 | + showProgress: config.showProgress, |
| 1470 | + ); |
| 1471 | + return config; |
| 1472 | +} |
| 1473 | + |
| 1474 | +/// Print help if we are passed the help option. |
| 1475 | +void _printHelp(ArgParser parser) { |
| 1476 | + print('Generate HTML documentation for Dart libraries.\n'); |
| 1477 | + print(parser.usage); |
| 1478 | +} |
| 1479 | + |
| 1480 | +/// Print usage information on invalid command lines. |
| 1481 | +void _printUsage(ArgParser parser) { |
| 1482 | + print('Usage: dartdoc [OPTIONS]\n'); |
| 1483 | + print(parser.usage); |
| 1484 | +} |
| 1485 | + |
| 1486 | +/// Print version information. |
| 1487 | +void _printVersion(ArgParser parser) { |
| 1488 | + print('dartdoc version: $dartdocVersion'); |
| 1489 | +} |
| 1490 | + |
1343 | 1491 | /// Instantiate dartdoc's configuration file and options parser with the |
1344 | 1492 | /// given command line arguments. |
1345 | 1493 | List<DartdocOption> createDartdocOptions( |
|
0 commit comments