Skip to content

Commit 1e78745

Browse files
committed
Centralize all file writing in HtmlGeneratorInstance
Everything gets logged. Everything gets checked for dupes. Much easier to add 'no-op' support to validate code structure without writing the output
1 parent b159cdc commit 1e78745

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

lib/src/html/html_generator_instance.dart

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:async' show Future, StreamController;
66
import 'dart:convert' show JsonEncoder;
77
import 'dart:io' show Directory, File;
8-
import 'dart:typed_data' show Uint8List;
98

109
import 'package:collection/collection.dart' show compareNatural;
1110
import 'package:path/path.dart' as path;
@@ -53,8 +52,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
5352

5453
await _copyResources();
5554
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);
5857
}
5958
}
6059

@@ -292,11 +291,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
292291
'encountered $resourcePath');
293292
}
294293
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));
300296
}
301297
}
302298

@@ -306,24 +302,34 @@ class HtmlGeneratorInstance implements HtmlOptions {
306302
String content = template(data,
307303
assumeNullNonExistingProperty: false, errorOnMissingProperty: true);
308304

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));
312305
_writeFile(fullName, content);
313-
writtenFiles.add(fullName);
314306
if (data.self is ModelElement) documentedElements.add(data.self);
315307
}
316308

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+
}
320323
_onFileCreated.add(file);
324+
writtenFiles.add(filename);
321325
}
322326
}
323327

324328
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+
}
328334
return file;
329335
}

0 commit comments

Comments
 (0)