5
5
import 'dart:async' show Future, StreamController;
6
6
import 'dart:convert' show JsonEncoder;
7
7
import 'dart:io' show Directory, File;
8
- import 'dart:typed_data' show Uint8List;
9
8
10
9
import 'package:collection/collection.dart' show compareNatural;
11
10
import 'package:path/path.dart' as path;
@@ -53,8 +52,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
53
52
54
53
await _copyResources ();
55
54
if (faviconPath != null ) {
56
- File file = new File (path. join (out.path, 'static-assets' , 'favicon.png' ) );
57
- file. writeAsBytesSync ( new File (faviconPath). readAsBytesSync () );
55
+ var bytes = new File (faviconPath). readAsBytesSync ( );
56
+ _writeFile (path. join (out.path, 'static-assets' , 'favicon.png' ), bytes );
58
57
}
59
58
}
60
59
@@ -292,11 +291,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
292
291
'encountered $resourcePath ' );
293
292
}
294
293
String destFileName = resourcePath.substring (prefix.length);
295
- File destFile =
296
- new File (path.join (out.path, 'static-assets' , destFileName))
297
- ..createSync (recursive: true );
298
- Uint8List resourceBytes = await loader.loadAsBytes (resourcePath);
299
- destFile.writeAsBytesSync (resourceBytes);
294
+ _writeFile (path.join (out.path, 'static-assets' , destFileName),
295
+ await loader.loadAsBytes (resourcePath));
300
296
}
301
297
}
302
298
@@ -306,24 +302,31 @@ class HtmlGeneratorInstance implements HtmlOptions {
306
302
String content = template (data,
307
303
assumeNullNonExistingProperty: false , errorOnMissingProperty: true );
308
304
309
- // If you see this assert, we're probably being called to build non-canonical
310
- // docs somehow. Check data.self.isCanonical and callers for bugs.
311
- assert (! writtenFiles.contains (fullName));
312
305
_writeFile (fullName, content);
313
- writtenFiles.add (fullName);
314
306
if (data.self is ModelElement ) documentedElements.add (data.self);
315
307
}
316
308
317
- void _writeFile (String filename, String content) {
318
- File file = _createOutputFile (filename);
319
- file.writeAsStringSync (content);
309
+ /// [content] must be either [String] or [List<int>] .
310
+ void _writeFile (String filename, Object content) {
311
+ // If you see this assert, we're probably being called to build non-canonical
312
+ // docs somehow. Check data.self.isCanonical and callers for bugs.
313
+ assert (! writtenFiles.contains (filename));
314
+
315
+ File file = new File (filename);
316
+ Directory parent = file.parent;
317
+ if (! parent.existsSync ()) {
318
+ parent.createSync (recursive: true );
319
+ }
320
+
321
+ if (content is String ) {
322
+ file.writeAsStringSync (content);
323
+ } else if (content is List <int >) {
324
+ file.writeAsBytesSync (content);
325
+ } else {
326
+ throw new ArgumentError .value (
327
+ content, 'content' , '`content` must be `String` or `List<int>`.' );
328
+ }
320
329
_onFileCreated.add (file);
330
+ writtenFiles.add (filename);
321
331
}
322
332
}
323
-
324
- File _createOutputFile (String filename) {
325
- File file = new File (filename);
326
- Directory parent = file.parent;
327
- if (! parent.existsSync ()) parent.createSync (recursive: true );
328
- return file;
329
- }
0 commit comments