2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- import 'dart:async' show Future, StreamController ;
5
+ import 'dart:async' show Future;
6
6
import 'dart:convert' show JsonEncoder;
7
- import 'dart:io' show Directory, File;
7
+ import 'dart:io' show File;
8
8
9
9
import 'package:collection/collection.dart' show compareNatural;
10
10
import 'package:dartdoc/src/model_utils.dart' ;
@@ -19,29 +19,17 @@ import 'resources.g.dart' as resources;
19
19
import 'template_data.dart' ;
20
20
import 'templates.dart' ;
21
21
22
- class HtmlGeneratorInstance implements HtmlOptions {
22
+ typedef void FileWriter (String path, Object content, {bool allowOverwrite});
23
+
24
+ class HtmlGeneratorInstance {
23
25
final HtmlGeneratorOptions _options;
24
26
final Templates _templates;
25
27
final Package _package;
26
- final String _outputDirectoryPath;
27
28
final List <ModelElement > _documentedElements = < ModelElement > [];
28
- final StreamController <File > _onFileCreated;
29
-
30
- @override
31
- String get relCanonicalPrefix => _options.relCanonicalPrefix;
32
-
33
- @override
34
- String get toolVersion => _options.toolVersion;
35
-
36
- String get _faviconPath => _options.faviconPath;
37
- bool get _useCategories => _options.useCategories;
29
+ final FileWriter _writer;
38
30
39
- // Protect against bugs in canonicalization by tracking what files we
40
- // write.
41
- final Set <String > writtenFiles = new Set <String >();
42
-
43
- HtmlGeneratorInstance (this ._options, this ._templates, this ._package,
44
- this ._outputDirectoryPath, this ._onFileCreated);
31
+ HtmlGeneratorInstance (
32
+ this ._options, this ._templates, this ._package, this ._writer);
45
33
46
34
Future generate () async {
47
35
if (_package != null ) {
@@ -50,13 +38,11 @@ class HtmlGeneratorInstance implements HtmlOptions {
50
38
}
51
39
52
40
await _copyResources ();
53
- if (_faviconPath != null ) {
54
- var bytes = new File (_faviconPath ).readAsBytesSync ();
41
+ if (_options.faviconPath != null ) {
42
+ var bytes = new File (_options.faviconPath ).readAsBytesSync ();
55
43
// Allow overwrite of favicon.
56
- String filename =
57
- path.join (_outputDirectoryPath, 'static-assets' , 'favicon.png' );
58
- writtenFiles.remove (filename);
59
- _writeFile (filename, bytes);
44
+ _writer (path.join ('static-assets' , 'favicon.png' ), bytes,
45
+ allowOverwrite: true );
60
46
}
61
47
}
62
48
@@ -95,7 +81,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
95
81
});
96
82
97
83
String json = encoder.convert (indexItems);
98
- _writeFile (path.join (_outputDirectoryPath, 'index.json' ), '${json }\n ' );
84
+ _writer (path.join ('index.json' ), '${json }\n ' );
99
85
}
100
86
101
87
void _generateDocs () {
@@ -178,7 +164,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
178
164
}
179
165
180
166
void generatePackage () {
181
- TemplateData data = new PackageTemplateData (this , _package, _useCategories );
167
+ TemplateData data = new PackageTemplateData (_options , _package);
182
168
logInfo ('documenting ${_package .name }' );
183
169
184
170
_build ('index.html' , _templates.indexTemplate, data);
@@ -190,35 +176,35 @@ class HtmlGeneratorInstance implements HtmlOptions {
190
176
if (! lib.isAnonymous && ! lib.hasDocumentation) {
191
177
package.warnOnElement (lib, PackageWarning .noLibraryLevelDocs);
192
178
}
193
- TemplateData data =
194
- new LibraryTemplateData (this , package, lib, _useCategories);
179
+ TemplateData data = new LibraryTemplateData (_options, package, lib);
195
180
196
181
_build (path.join (lib.dirName, '${lib .fileName }' ),
197
182
_templates.libraryTemplate, data);
198
183
}
199
184
200
185
void generateClass (Package package, Library lib, Class clazz) {
201
- TemplateData data = new ClassTemplateData (this , package, lib, clazz);
186
+ TemplateData data = new ClassTemplateData (_options , package, lib, clazz);
202
187
_build (path.joinAll (clazz.href.split ('/' )), _templates.classTemplate, data);
203
188
}
204
189
205
190
void generateConstructor (
206
191
Package package, Library lib, Class clazz, Constructor constructor) {
207
192
TemplateData data =
208
- new ConstructorTemplateData (this , package, lib, clazz, constructor);
193
+ new ConstructorTemplateData (_options , package, lib, clazz, constructor);
209
194
210
195
_build (path.joinAll (constructor.href.split ('/' )),
211
196
_templates.constructorTemplate, data);
212
197
}
213
198
214
199
void generateEnum (Package package, Library lib, Enum eNum) {
215
- TemplateData data = new EnumTemplateData (this , package, lib, eNum);
200
+ TemplateData data = new EnumTemplateData (_options , package, lib, eNum);
216
201
217
202
_build (path.joinAll (eNum.href.split ('/' )), _templates.enumTemplate, data);
218
203
}
219
204
220
205
void generateFunction (Package package, Library lib, ModelFunction function) {
221
- TemplateData data = new FunctionTemplateData (this , package, lib, function);
206
+ TemplateData data =
207
+ new FunctionTemplateData (_options, package, lib, function);
222
208
223
209
_build (path.joinAll (function.href.split ('/' )), _templates.functionTemplate,
224
210
data);
@@ -227,7 +213,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
227
213
void generateMethod (
228
214
Package package, Library lib, Class clazz, Method method) {
229
215
TemplateData data =
230
- new MethodTemplateData (this , package, lib, clazz, method);
216
+ new MethodTemplateData (_options , package, lib, clazz, method);
231
217
232
218
_build (
233
219
path.joinAll (method.href.split ('/' )), _templates.methodTemplate, data);
@@ -236,7 +222,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
236
222
void generateConstant (
237
223
Package package, Library lib, Class clazz, Field property) {
238
224
TemplateData data =
239
- new ConstantTemplateData (this , package, lib, clazz, property);
225
+ new ConstantTemplateData (_options , package, lib, clazz, property);
240
226
241
227
_build (path.joinAll (property.href.split ('/' )), _templates.constantTemplate,
242
228
data);
@@ -245,7 +231,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
245
231
void generateProperty (
246
232
Package package, Library lib, Class clazz, Field property) {
247
233
TemplateData data =
248
- new PropertyTemplateData (this , package, lib, clazz, property);
234
+ new PropertyTemplateData (_options , package, lib, clazz, property);
249
235
250
236
_build (path.joinAll (property.href.split ('/' )), _templates.propertyTemplate,
251
237
data);
@@ -254,7 +240,7 @@ class HtmlGeneratorInstance implements HtmlOptions {
254
240
void generateTopLevelProperty (
255
241
Package package, Library lib, TopLevelVariable property) {
256
242
TemplateData data =
257
- new TopLevelPropertyTemplateData (this , package, lib, property);
243
+ new TopLevelPropertyTemplateData (_options , package, lib, property);
258
244
259
245
_build (path.joinAll (property.href.split ('/' )),
260
246
_templates.topLevelPropertyTemplate, data);
@@ -263,14 +249,15 @@ class HtmlGeneratorInstance implements HtmlOptions {
263
249
void generateTopLevelConstant (
264
250
Package package, Library lib, TopLevelVariable property) {
265
251
TemplateData data =
266
- new TopLevelConstTemplateData (this , package, lib, property);
252
+ new TopLevelConstTemplateData (_options , package, lib, property);
267
253
268
254
_build (path.joinAll (property.href.split ('/' )),
269
255
_templates.topLevelConstantTemplate, data);
270
256
}
271
257
272
258
void generateTypeDef (Package package, Library lib, Typedef typeDef) {
273
- TemplateData data = new TypedefTemplateData (this , package, lib, typeDef);
259
+ TemplateData data =
260
+ new TypedefTemplateData (_options, package, lib, typeDef);
274
261
275
262
_build (path.joinAll (typeDef.href.split ('/' )), _templates.typeDefTemplate,
276
263
data);
@@ -285,44 +272,16 @@ class HtmlGeneratorInstance implements HtmlOptions {
285
272
'encountered $resourcePath ' );
286
273
}
287
274
String destFileName = resourcePath.substring (prefix.length);
288
- _writeFile (path.join (_outputDirectoryPath, 'static-assets' , destFileName),
275
+ _writer (path.join ('static-assets' , destFileName),
289
276
await loader.loadAsBytes (resourcePath));
290
277
}
291
278
}
292
279
293
280
void _build (String filename, TemplateRenderer template, TemplateData data) {
294
- String fullName = path.join (_outputDirectoryPath, filename);
295
-
296
281
String content = template (data,
297
282
assumeNullNonExistingProperty: false , errorOnMissingProperty: true );
298
283
299
- _writeFile (fullName, content);
300
- if (data.self is ModelElement ) {
301
- _documentedElements.add (data.self);
302
- }
303
- }
304
-
305
- /// [content] must be either [String] or [List<int>] .
306
- void _writeFile (String filename, Object content) {
307
- // If you see this assert, we're probably being called to build non-canonical
308
- // docs somehow. Check data.self.isCanonical and callers for bugs.
309
- assert (! writtenFiles.contains (filename));
310
-
311
- File file = new File (filename);
312
- Directory parent = file.parent;
313
- if (! parent.existsSync ()) {
314
- parent.createSync (recursive: true );
315
- }
316
-
317
- if (content is String ) {
318
- file.writeAsStringSync (content);
319
- } else if (content is List <int >) {
320
- file.writeAsBytesSync (content);
321
- } else {
322
- throw new ArgumentError .value (
323
- content, 'content' , '`content` must be `String` or `List<int>`.' );
324
- }
325
- _onFileCreated.add (file);
326
- writtenFiles.add (filename);
284
+ _writer (filename, content);
285
+ if (data.self is ModelElement ) _documentedElements.add (data.self);
327
286
}
328
287
}
0 commit comments