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,34 @@ 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
+ void _writeFile (String filename, Object content) {
310
+ // If you see this assert, we're probably being called to build non-canonical
311
+ // docs somehow. Check data.self.isCanonical and callers for bugs.
312
+ assert (! writtenFiles.contains (filename));
313
+
314
+ var file = _createOutputFile (filename);
315
+ if (content is String ) {
316
+ file.writeAsStringSync (content);
317
+ } else if (content is List <int >) {
318
+ file.writeAsBytesSync (content);
319
+ } else {
320
+ throw new ArgumentError .value (
321
+ content, 'content' , '`content` must be `String` or `List<int>`.' );
322
+ }
320
323
_onFileCreated.add (file);
324
+ writtenFiles.add (filename);
321
325
}
322
326
}
323
327
324
328
File _createOutputFile (String filename) {
325
- File file = new File (filename);
326
- Directory parent = file.parent;
327
- if (! parent.existsSync ()) parent.createSync (recursive: true );
329
+ var file = new File (filename);
330
+ var parent = file.parent;
331
+ if (! parent.existsSync ()) {
332
+ parent.createSync (recursive: true );
333
+ }
328
334
return file;
329
335
}
0 commit comments