Skip to content

Commit 5272c6a

Browse files
authored
Resolve SDK footer text URI in templates.dart (#2146)
* Resolve SDK footer text URI in templates.dart Moves resolution of the SDK footer text since we need to know the format first and we can't make an await call inside the _compute function of a DartdocSyntheticOption. * Fix generated resources.g.dart
1 parent 7710ca0 commit 5272c6a

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

lib/resources/sdk_footer_text.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
• [cc license](http://creativecommons.org/licenses/by-sa/4.0/)

lib/src/generator.dart

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ library dartdoc.generator;
77

88
import 'dart:async' show Future;
99
import 'dart:io' show Directory;
10-
import 'dart:isolate';
1110

1211
import 'package:dartdoc/src/dartdoc_options.dart';
1312
import 'package:dartdoc/src/model/model.dart' show PackageGraph;
14-
import 'package:dartdoc/src/package_meta.dart';
1513
import 'package:dartdoc/src/warnings.dart';
16-
import 'package:path/path.dart' as path;
1714

1815
abstract class FileWriter {
1916
/// All filenames written by this generator.
@@ -38,12 +35,10 @@ abstract class Generator {
3835
mixin GeneratorContext on DartdocOptionContextBase {
3936
List<String> get footer => optionSet['footer'].valueAt(context);
4037

41-
/// _footerText is only used to construct synthetic options.
42-
// ignore: unused_element
43-
List<String> get _footerText => optionSet['footerText'].valueAt(context);
38+
List<String> get footerText => optionSet['footerText'].valueAt(context);
4439

45-
List<String> get footerTextPaths =>
46-
optionSet['footerTextPaths'].valueAt(context);
40+
// Synthetic. TODO(jcollins-g): Eliminate special case for SDK and use config file.
41+
bool get addSdkFooter => optionSet['addSdkFooter'].valueAt(context);
4742

4843
List<String> get header => optionSet['header'].valueAt(context);
4944

@@ -60,19 +55,7 @@ mixin GeneratorContext on DartdocOptionContextBase {
6055
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
6156
}
6257

63-
Uri _sdkFooterCopyrightUri;
64-
65-
Future<void> _setSdkFooterCopyrightUri() async {
66-
if (_sdkFooterCopyrightUri == null) {
67-
// TODO(jdkoren): find a way to make this not specific to HTML, or have
68-
// alternatives for other supported formats.
69-
_sdkFooterCopyrightUri = await Isolate.resolvePackageUri(
70-
Uri.parse('package:dartdoc/resources/sdk_footer_text.html'));
71-
}
72-
}
73-
7458
Future<List<DartdocOption>> createGeneratorOptions() async {
75-
await _setSdkFooterCopyrightUri();
7659
return <DartdocOption>[
7760
DartdocOptionArgFile<List<String>>('footer', [],
7861
isFile: true,
@@ -89,23 +72,12 @@ Future<List<DartdocOption>> createGeneratorOptions() async {
8972
'package name and version).',
9073
mustExist: true,
9174
splitCommas: true),
92-
DartdocOptionSyntheticOnly<List<String>>(
93-
'footerTextPaths',
94-
(DartdocSyntheticOption<List<String>> option, Directory dir) {
95-
final List<String> footerTextPaths = <String>[];
96-
final PackageMeta topLevelPackageMeta =
97-
option.root['topLevelPackageMeta'].valueAt(dir);
98-
// TODO(jcollins-g): Eliminate special casing for SDK and use config file.
99-
if (topLevelPackageMeta.isSdk == true) {
100-
footerTextPaths
101-
.add(path.canonicalize(_sdkFooterCopyrightUri.toFilePath()));
102-
}
103-
footerTextPaths.addAll(option.parent['footerText'].valueAt(dir));
104-
return footerTextPaths;
75+
DartdocOptionSyntheticOnly<bool>(
76+
'addSdkFooter',
77+
(DartdocSyntheticOption<bool> option, Directory dir) {
78+
return option.root['topLevelPackageMeta'].valueAt(dir).isSdk;
10579
},
106-
isFile: true,
107-
help: 'paths to footer-text-files (adding special case for SDK)',
108-
mustExist: true,
80+
help: 'Whether the SDK footer text should be added (synthetic)',
10981
),
11082
DartdocOptionArgFile<List<String>>('header', [],
11183
isFile: true,

lib/src/html/resources.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const List<String> resource_names = [
1111
'package:dartdoc/resources/readme.md',
1212
'package:dartdoc/resources/script.js',
1313
'package:dartdoc/resources/sdk_footer_text.html',
14+
'package:dartdoc/resources/sdk_footer_text.md',
1415
'package:dartdoc/resources/styles.css',
1516
'package:dartdoc/resources/typeahead.bundle.min.js'
1617
];

lib/src/html/templates.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library dartdoc.templates;
66

77
import 'dart:async' show Future;
88
import 'dart:io' show File, Directory;
9+
import 'dart:isolate';
910

1011
import 'package:dartdoc/dartdoc.dart';
1112
import 'package:dartdoc/src/html/resource_loader.dart' as loader;
@@ -149,18 +150,27 @@ class Templates {
149150
final Template topLevelPropertyTemplate;
150151
final Template typeDefTemplate;
151152

152-
static Future<Templates> fromContext(DartdocGeneratorOptionContext context) {
153+
static Future<Templates> fromContext(
154+
DartdocGeneratorOptionContext context) async {
153155
String templatesDir = context.templatesDir;
156+
String format = context.format;
157+
List<String> footerTextPaths = context.footerText;
158+
if (context.addSdkFooter) {
159+
Uri sdkFooter = await Isolate.resolvePackageUri(
160+
Uri.parse('package:dartdoc/resources/sdk_footer_text.$format'));
161+
footerTextPaths.add(path.canonicalize(sdkFooter.toFilePath()));
162+
}
163+
154164
if (templatesDir != null) {
155-
return fromDirectory(Directory(templatesDir), context.format,
165+
return fromDirectory(Directory(templatesDir), format,
156166
headerPaths: context.header,
157167
footerPaths: context.footer,
158-
footerTextPaths: context.footerTextPaths);
168+
footerTextPaths: footerTextPaths);
159169
} else {
160-
return createDefault(context.format,
170+
return createDefault(format,
161171
headerPaths: context.header,
162172
footerPaths: context.footer,
163-
footerTextPaths: context.footerTextPaths);
173+
footerTextPaths: footerTextPaths);
164174
}
165175
}
166176

0 commit comments

Comments
 (0)