Skip to content

Commit ff0d94b

Browse files
authored
Add a resources-dir option (#2857)
1 parent 699b26d commit ff0d94b

7 files changed

+44
-31
lines changed

lib/options.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ class DartdocGeneratorOptionContext extends DartdocOptionContext {
5151
String get relCanonicalPrefix =>
5252
optionSet['relCanonicalPrefix'].valueAt(context);
5353

54-
/// The 'templatesDir' Dartdoc option if one was specified; otherwise `null`.
54+
/// The 'templatesDir' dartdoc option if one was specified; otherwise `null`.
5555
String get templatesDir => optionSet['templatesDir'].valueAt(context);
5656

5757
// TODO(jdkoren): duplicated temporarily so that GeneratorContext is enough for configuration.
5858
@override
5959
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
60+
61+
/// The 'resourcesDir' dartdoc option if one was specified; otherwise `null`.
62+
String /*?*/ get resourcesDir => optionSet['resourcesDir'].valueAt(context);
6063
}
6164

6265
class DartdocProgramOptionContext extends DartdocGeneratorOptionContext

lib/src/dartdoc_options.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,8 @@ Future<List<DartdocOption>> createDartdocOptions(
15431543
help:
15441544
'A list of package names to place first when grouping libraries in '
15451545
'packages. Unmentioned packages are sorted after these.'),
1546+
DartdocOptionArgOnly<String>('resourcesDir', null, resourceProvider,
1547+
help: "An absolute path to dartdoc's resources directory.", hide: true),
15461548
DartdocOptionArgOnly<bool>('sdkDocs', false, resourceProvider,
15471549
help: 'Generate ONLY the docs for the Dart SDK.'),
15481550
DartdocOptionArgSynth<String>('sdkDir',

lib/src/generator/dartdoc_generator_backend.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
3838
@override
3939
final String customInnerFooterText;
4040

41+
final String /*?*/ resourcesDir;
42+
4143
DartdocGeneratorBackendOptions.fromContext(
4244
DartdocGeneratorOptionContext context)
4345
: relCanonicalPrefix = context.relCanonicalPrefix,
@@ -47,7 +49,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
4749
useBaseHref = context.useBaseHref,
4850
customHeaderContent = context.header,
4951
customFooterContent = context.footer,
50-
customInnerFooterText = context.footerText;
52+
customInnerFooterText = context.footerText,
53+
resourcesDir = context.resourcesDir;
5154

5255
DartdocGeneratorBackendOptions._defaults()
5356
: relCanonicalPrefix = null,
@@ -57,7 +60,8 @@ class DartdocGeneratorBackendOptions implements TemplateOptions {
5760
useBaseHref = false,
5861
customHeaderContent = '',
5962
customFooterContent = '',
60-
customInnerFooterText = '';
63+
customInnerFooterText = '',
64+
resourcesDir = null;
6165
}
6266

6367
class SidebarGenerator<T extends TemplateData> {

lib/src/generator/html_generator.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,28 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
5151
// Allow overwrite of favicon.
5252
var bytes = resourceProvider.getFile(options.favicon).readAsBytesSync();
5353
writer.writeBytes(
54-
resourceProvider.pathContext.join('static-assets', 'favicon.png'),
54+
_pathJoin('static-assets', 'favicon.png'),
5555
bytes,
5656
allowOverwrite: true,
5757
);
5858
}
5959
}
6060

6161
Future<void> _copyResources(FileWriter writer) async {
62-
for (var resourcePath in resources.resourceNames) {
63-
if (!resourcePath.startsWith(_dartdocResourcePrefix)) {
64-
throw StateError('Resource paths must start with '
65-
'$_dartdocResourcePrefix, encountered $resourcePath');
66-
}
67-
var destFileName = resourcePath.substring(_dartdocResourcePrefix.length);
68-
var destFilePath =
69-
resourceProvider.pathContext.join('static-assets', destFileName);
70-
writer.writeBytes(destFilePath,
71-
await resourceProvider.loadResourceAsBytes(resourcePath));
62+
var resourcesDir = options.resourcesDir ??
63+
(await resourceProvider.getResourceFolder(_dartdocResourcePrefix)).path;
64+
for (var resourceFileName in resources.resourceNames) {
65+
var destinationPath = _pathJoin('static-assets', resourceFileName);
66+
var sourcePath = _pathJoin(resourcesDir, resourceFileName);
67+
writer.writeBytes(
68+
destinationPath,
69+
resourceProvider.getFile(sourcePath).readAsBytesSync(),
70+
);
7271
}
7372
}
7473

75-
static const _dartdocResourcePrefix = 'package:dartdoc/resources/';
74+
String _pathJoin(String a, String b) =>
75+
resourceProvider.pathContext.join(a, b);
76+
77+
static const _dartdocResourcePrefix = 'package:dartdoc/resources';
7678
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// WARNING: This file is auto-generated. Do not edit.
22

33
const List<String> resourceNames = [
4-
'package:dartdoc/resources/favicon.png',
5-
'package:dartdoc/resources/github.css',
6-
'package:dartdoc/resources/highlight.pack.js',
7-
'package:dartdoc/resources/play_button.svg',
8-
'package:dartdoc/resources/readme.md',
9-
'package:dartdoc/resources/script.js',
10-
'package:dartdoc/resources/styles.css'
4+
'favicon.png',
5+
'github.css',
6+
'highlight.pack.js',
7+
'play_button.svg',
8+
'readme.md',
9+
'script.js',
10+
'styles.css',
1111
];

test/html_generator_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ void main() {
125125
.getFolder(pathContext.join(outputPath, 'static-assets'));
126126
expect(output, doesExist);
127127

128-
for (var resource in resourceNames.map((r) =>
129-
pathContext.relative(Uri.parse(r).path, from: 'dartdoc/resources'))) {
128+
for (var resource in resourceNames) {
130129
expect(resourceProvider.getFile(pathContext.join(output.path, resource)),
131130
doesExist);
132131
}

tool/builder.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ String _resourcesFile(Iterable<String> packagePaths) => '''
1212
// WARNING: This file is auto-generated. Do not edit.
1313
1414
const List<String> resourceNames = [
15-
${packagePaths.map((p) => " '$p'").join(',\n')}
15+
${packagePaths.map((p) => " '$p',").join('\n')}
1616
];
1717
''';
1818

1919
class ResourceBuilder implements Builder {
2020
final BuilderOptions builderOptions;
21+
2122
ResourceBuilder(this.builderOptions);
2223

23-
static final _allResources = Glob('lib/resources/**');
24+
static const _resourcesPath = 'lib/resources';
25+
2426
@override
2527
Future<void> build(BuildStep buildStep) async {
26-
var packagePaths = <String>[];
27-
await for (AssetId asset in buildStep.findAssets(_allResources)) {
28-
packagePaths.add(asset.uri.toString());
29-
}
30-
packagePaths.sort();
28+
var resourceAssets =
29+
await buildStep.findAssets(Glob('$_resourcesPath/**')).toList();
30+
var packagePaths = [
31+
for (var asset in resourceAssets)
32+
path.url.relative(asset.path, from: _resourcesPath),
33+
]..sort();
3134
await buildStep.writeAsString(
3235
AssetId(buildStep.inputId.package,
3336
path.url.join('lib', 'src', 'generator', 'html_resources.g.dart')),

0 commit comments

Comments
 (0)