Skip to content

Commit a1d8f30

Browse files
authored
Introduce basic glob support to option handling (#2365)
* Combine isDir/isFile * Add the glob directory munging support * Rename to OptionKind
1 parent ceac197 commit a1d8f30

File tree

4 files changed

+116
-55
lines changed

4 files changed

+116
-55
lines changed

lib/src/dartdoc_options.dart

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ class _YamlFileData {
433433
_YamlFileData(this.data, this.canonicalDirectoryPath);
434434
}
435435

436+
/// An enum to specify the multiple different kinds of data an option might
437+
/// represent.
438+
enum OptionKind {
439+
other, // Make no assumptions about the option data; it may be of any type
440+
// or semantic.
441+
file, // Option data references a filename or filenames with strings.
442+
dir, // Option data references a directory name or names with strings.
443+
glob, // Option data references globs with strings that may cover many
444+
// filenames and/or directories.
445+
}
446+
436447
/// Some DartdocOption subclasses need to keep track of where they
437448
/// got the value from; this class contains those intermediate results
438449
/// so that error messages can be more useful.
@@ -503,10 +514,15 @@ abstract class DartdocOption<T> {
503514
final String name;
504515

505516
/// Set to true if this option represents the name of a directory.
506-
final bool isDir;
517+
bool get isDir => optionIs == OptionKind.dir;
507518

508519
/// Set to true if this option represents the name of a file.
509-
final bool isFile;
520+
bool get isFile => optionIs == OptionKind.file;
521+
522+
/// Set to true if this option represents a glob.
523+
bool get isGlob => optionIs == OptionKind.glob;
524+
525+
final OptionKind optionIs;
510526

511527
/// Set to true if DartdocOption subclasses should validate that the
512528
/// directory or file exists. Does not imply validation of [defaultsTo],
@@ -515,11 +531,13 @@ abstract class DartdocOption<T> {
515531

516532
final ResourceProvider resourceProvider;
517533

518-
DartdocOption(this.name, this.defaultsTo, this.help, this.isDir, this.isFile,
534+
DartdocOption(this.name, this.defaultsTo, this.help, this.optionIs,
519535
this.mustExist, this._convertYamlToType, this.resourceProvider) {
520-
assert(!(isDir && isFile));
521-
if (isDir || isFile) assert(_isString || _isListString || _isMapString);
536+
if (isDir || isFile || isGlob) {
537+
assert(_isString || _isListString || _isMapString);
538+
}
522539
if (mustExist) {
540+
// Globs by definition don't have to exist.
523541
assert(isDir || isFile);
524542
}
525543
}
@@ -604,7 +622,7 @@ abstract class DartdocOption<T> {
604622
/// For a [List<String>] or [String] value, if [isDir] or [isFile] is set,
605623
/// resolve paths in value relative to canonicalPath.
606624
T _handlePathsInContext(_OptionValueWithContext<Object> valueWithContext) {
607-
if (valueWithContext?.value == null || !(isDir || isFile)) {
625+
if (valueWithContext?.value == null || !(isDir || isFile || isGlob)) {
608626
return valueWithContext?.value;
609627
}
610628
_validatePaths(valueWithContext);
@@ -715,11 +733,10 @@ class DartdocOptionFileSynth<T> extends DartdocOption<T>
715733
String name, this._compute, ResourceProvider resourceprovider,
716734
{bool mustExist = false,
717735
String help = '',
718-
bool isDir = false,
719-
bool isFile = false,
736+
OptionKind optionIs = OptionKind.other,
720737
bool parentDirOverridesChild,
721738
ConvertYamlToType<T> convertYamlToType})
722-
: super(name, null, help, isDir, isFile, mustExist, convertYamlToType,
739+
: super(name, null, help, optionIs, mustExist, convertYamlToType,
723740
resourceprovider) {
724741
_parentDirOverridesChild = parentDirOverridesChild;
725742
}
@@ -765,12 +782,10 @@ class DartdocOptionArgSynth<T> extends DartdocOption<T>
765782
bool mustExist = false,
766783
String help = '',
767784
bool hide = false,
768-
bool isDir = false,
769-
bool isFile = false,
785+
OptionKind optionIs = OptionKind.other,
770786
bool negatable = false,
771787
bool splitCommas})
772-
: super(name, null, help, isDir, isFile, mustExist, null,
773-
resourceProvider) {
788+
: super(name, null, help, optionIs, mustExist, null, resourceProvider) {
774789
_hide = hide;
775790
_negatable = negatable;
776791
_splitCommas = splitCommas;
@@ -818,10 +833,8 @@ class DartdocOptionSyntheticOnly<T> extends DartdocOption<T>
818833
String name, this._compute, ResourceProvider resourceProvider,
819834
{bool mustExist = false,
820835
String help = '',
821-
bool isDir = false,
822-
bool isFile = false})
823-
: super(
824-
name, null, help, isDir, isFile, mustExist, null, resourceProvider);
836+
OptionKind optionIs = OptionKind.other})
837+
: super(name, null, help, optionIs, mustExist, null, resourceProvider);
825838
}
826839

827840
abstract class DartdocSyntheticOption<T> implements DartdocOption<T> {
@@ -856,7 +869,8 @@ typedef OptionGenerator = Future<List<DartdocOption<Object>>> Function(
856869
/// option itself.
857870
class DartdocOptionSet extends DartdocOption<void> {
858871
DartdocOptionSet(String name, ResourceProvider resourceProvider)
859-
: super(name, null, null, false, false, false, null, resourceProvider);
872+
: super(
873+
name, null, null, OptionKind.other, false, null, resourceProvider);
860874

861875
/// Asynchronous factory that is the main entry point to initialize Dartdoc
862876
/// options for use.
@@ -908,11 +922,10 @@ class DartdocOptionArgOnly<T> extends DartdocOption<T>
908922
bool mustExist = false,
909923
String help = '',
910924
bool hide = false,
911-
bool isDir = false,
912-
bool isFile = false,
925+
OptionKind optionIs = OptionKind.other,
913926
bool negatable = false,
914927
bool splitCommas})
915-
: super(name, defaultsTo, help, isDir, isFile, mustExist, null,
928+
: super(name, defaultsTo, help, optionIs, mustExist, null,
916929
resourceProvider) {
917930
_hide = hide;
918931
_negatable = negatable;
@@ -949,12 +962,11 @@ class DartdocOptionArgFile<T> extends DartdocOption<T>
949962
bool mustExist = false,
950963
String help = '',
951964
bool hide = false,
952-
bool isDir = false,
953-
bool isFile = false,
965+
OptionKind optionIs = OptionKind.other,
954966
bool negatable = false,
955967
bool parentDirOverridesChild = false,
956968
bool splitCommas})
957-
: super(name, defaultsTo, help, isDir, isFile, mustExist, null,
969+
: super(name, defaultsTo, help, optionIs, mustExist, null,
958970
resourceProvider) {
959971
_abbr = abbr;
960972
_hide = hide;
@@ -1007,12 +1019,11 @@ class DartdocOptionFileOnly<T> extends DartdocOption<T>
10071019
String name, T defaultsTo, ResourceProvider resourceProvider,
10081020
{bool mustExist = false,
10091021
String help = '',
1010-
bool isDir = false,
1011-
bool isFile = false,
1022+
OptionKind optionIs = OptionKind.other,
10121023
bool parentDirOverridesChild = false,
10131024
ConvertYamlToType<T> convertYamlToType})
1014-
: super(name, defaultsTo, help, isDir, isFile, mustExist,
1015-
convertYamlToType, resourceProvider) {
1025+
: super(name, defaultsTo, help, optionIs, mustExist, convertYamlToType,
1026+
resourceProvider) {
10161027
_parentDirOverridesChild = parentDirOverridesChild;
10171028
}
10181029

@@ -1260,8 +1271,10 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
12601271

12611272
T retval;
12621273
// Unlike in _DartdocFileOption, we throw here on inputs being invalid
1263-
// rather than silently proceeding. TODO(jcollins-g): throw on input
1264-
// formatting for files too?
1274+
// rather than silently proceeding. This is because the user presumably
1275+
// typed something wrong on the command line and can therefore fix it.
1276+
// dartdoc_option.yaml files from other packages may not be fully in the
1277+
// user's control.
12651278
if (_isBool || _isListString || _isString) {
12661279
retval = _argResults[argName];
12671280
} else if (_isInt) {
@@ -1566,7 +1579,7 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
15661579
}, resourceProvider,
15671580
help: 'Remove text from libraries with the following names.'),
15681581
DartdocOptionArgFile<String>('examplePathPrefix', null, resourceProvider,
1569-
isDir: true,
1582+
optionIs: OptionKind.dir,
15701583
help: 'Prefix for @example paths.\n(defaults to the project root)',
15711584
mustExist: true),
15721585
DartdocOptionArgFile<List<String>>('exclude', [], resourceProvider,
@@ -1580,7 +1593,7 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
15801593
(DartdocSyntheticOption<String> option, Folder dir) =>
15811594
resolveTildePath(Platform.environment['FLUTTER_ROOT']),
15821595
resourceProvider,
1583-
isDir: true,
1596+
optionIs: OptionKind.dir,
15841597
help: 'Root of the Flutter SDK, specified from environment.',
15851598
mustExist: true),
15861599
DartdocOptionArgOnly<bool>('hideSdkText', false, resourceProvider,
@@ -1592,7 +1605,7 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
15921605
help: 'Library names to generate docs for.', splitCommas: true),
15931606
DartdocOptionArgFile<List<String>>(
15941607
'includeExternal', null, resourceProvider,
1595-
isFile: true,
1608+
optionIs: OptionKind.file,
15961609
help:
15971610
'Additional (external) dart files to include; use "dir/fileName", '
15981611
'as in lib/material.dart.',
@@ -1605,7 +1618,9 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
16051618
'HTML into dartdoc output.'),
16061619
DartdocOptionArgOnly<String>(
16071620
'input', resourceProvider.pathContext.current, resourceProvider,
1608-
isDir: true, help: 'Path to source directory', mustExist: true),
1621+
optionIs: OptionKind.dir,
1622+
help: 'Path to source directory',
1623+
mustExist: true),
16091624
DartdocOptionSyntheticOnly<String>('inputDir',
16101625
(DartdocSyntheticOption<String> option, Folder dir) {
16111626
if (option.parent['sdkDocs'].valueAt(dir)) {
@@ -1614,7 +1629,7 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
16141629
return option.parent['input'].valueAt(dir);
16151630
}, resourceProvider,
16161631
help: 'Path to source directory (with override if --sdk-docs)',
1617-
isDir: true,
1632+
optionIs: OptionKind.dir,
16181633
mustExist: true),
16191634
DartdocOptionSet('linkTo', resourceProvider)
16201635
..addAll([
@@ -1656,7 +1671,7 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
16561671
]),
16571672
DartdocOptionArgOnly<String>('output',
16581673
resourceProvider.pathContext.join('doc', 'api'), resourceProvider,
1659-
isDir: true, help: 'Path to output directory.'),
1674+
optionIs: OptionKind.dir, help: 'Path to output directory.'),
16601675
DartdocOptionSyntheticOnly<PackageMeta>(
16611676
'packageMeta',
16621677
(DartdocSyntheticOption<PackageMeta> option, Folder dir) {
@@ -1691,7 +1706,9 @@ Future<List<DartdocOption<Object>>> createDartdocOptions(
16911706
}
16921707
return packageMetaProvider.defaultSdkDir.path;
16931708
}, packageMetaProvider.resourceProvider,
1694-
help: 'Path to the SDK directory.', isDir: true, mustExist: true),
1709+
help: 'Path to the SDK directory.',
1710+
optionIs: OptionKind.dir,
1711+
mustExist: true),
16951712
DartdocOptionArgFile<bool>(
16961713
'showUndocumentedCategories', false, resourceProvider,
16971714
help: "Label categories that aren't documented", negatable: true),

lib/src/generator/generator.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Future<List<DartdocOption<Object>>> createGeneratorOptions(
5555
var resourceProvider = packageMetaProvider.resourceProvider;
5656
return [
5757
DartdocOptionArgFile<List<String>>('footer', [], resourceProvider,
58-
isFile: true,
58+
optionIs: OptionKind.file,
5959
help:
6060
'Paths to files with content to add to page footers, but possibly '
6161
'outside of dedicated footer elements for the generator (e.g. '
@@ -64,13 +64,13 @@ Future<List<DartdocOption<Object>>> createGeneratorOptions(
6464
mustExist: true,
6565
splitCommas: true),
6666
DartdocOptionArgFile<List<String>>('footerText', [], resourceProvider,
67-
isFile: true,
67+
optionIs: OptionKind.file,
6868
help: 'Paths to files with content to add to page footers (next to the '
6969
'package name and version).',
7070
mustExist: true,
7171
splitCommas: true),
7272
DartdocOptionArgFile<List<String>>('header', [], resourceProvider,
73-
isFile: true,
73+
optionIs: OptionKind.file,
7474
help: 'Paths to files with content to add to page headers.',
7575
splitCommas: true),
7676
DartdocOptionArgOnly<bool>('prettyIndexJson', false, resourceProvider,
@@ -79,7 +79,7 @@ Future<List<DartdocOption<Object>>> createGeneratorOptions(
7979
'larger, but it\'s also easier to diff.',
8080
negatable: false),
8181
DartdocOptionArgFile<String>('favicon', null, resourceProvider,
82-
isFile: true,
82+
optionIs: OptionKind.file,
8383
help: 'A path to a favicon for the generated docs.',
8484
mustExist: true),
8585
DartdocOptionArgOnly<String>('relCanonicalPrefix', null, resourceProvider,
@@ -88,7 +88,7 @@ Future<List<DartdocOption<Object>>> createGeneratorOptions(
8888
'Consider using if building many versions of the docs for public '
8989
'SEO; learn more at https://goo.gl/gktN6F.'),
9090
DartdocOptionArgOnly<String>('templatesDir', null, resourceProvider,
91-
isDir: true,
91+
optionIs: OptionKind.dir,
9292
mustExist: true,
9393
hide: true,
9494
help:

lib/src/source_linker.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ Future<List<DartdocOption<Object>>> createSourceLinkerOptions(
3636
DartdocOptionSet('linkToSource', resourceProvider)
3737
..addAll([
3838
DartdocOptionArgFile<List<String>>('excludes', [], resourceProvider,
39-
isDir: true,
39+
optionIs: OptionKind.dir,
4040
help:
4141
'A list of directories to exclude from linking to a source code repository.'),
4242
// TODO(jcollins-g): Use [DartdocOptionArgSynth], possibly in combination with a repository type and the root directory, and get revision number automatically
4343
DartdocOptionArgOnly<String>('revision', null, resourceProvider,
4444
help: 'Revision number to insert into the URI.'),
4545
DartdocOptionArgFile<String>('root', null, resourceProvider,
46-
isDir: true,
46+
optionIs: OptionKind.dir,
4747
help:
4848
'Path to a local directory that is the root of the repository we link to. All source code files under this directory will be linked.'),
4949
DartdocOptionArgFile<String>('uriTemplate', null, resourceProvider,

0 commit comments

Comments
 (0)